Android Programming: Pushing the Limits -- Chapter 6: Services and Background Tasks
2016-06-30 17:13
501 查看
什么时候使用Service服务类型开启服务后台运行服务通信附加资源
MyActivity.java
http://android-developers.blogspot.se/2010/02/service-api-changes-starting-with.html
Dianne Hackborn at
http://android-developers.blogspot.se/2010/04/multitaskingandroid-way.html
什么时候使用Service:
public class MyActivity extends Activity implements ServiceConnection, MyLocalService.Callback { private MyLocalService mService; /** * Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main_activity); } @Override protected void onResume() { super.onResume(); Intent bindIntent = new Intent(this, MyLocalService.class); bindService(bindIntent, this, BIND_AUTO_CREATE); } @Override protected void onPause() { super.onPause(); if (mService != null) { mService.setCallback(null); // Important to avoid memory leaks unbindService(this); } } public void onTriggerLongRunningOperation(View view) { if(mService != null) { mService.performLongRunningOperation(new MyComplexDataObject()); } } @Override public void onOperationProgress(int progress) { // TODO Update user interface with progress.. } @Override public void onOperationCompleted(MyComplexResult complexResult) { // TODO Show result to user... } @Override public void onServiceConnected(ComponentName componentName, IBinder iBinder) { mService = ((MyLocalService.LocalBinder) iBinder).getService(); mService.setCallback(this); // Once we have a reference to the service, we can update the UI and // enable buttons that should otherwise be disabled. findViewById(R.id.trigger_operation_button).setEnabled(true); } @Override public void onServiceDisconnected(ComponentName componentName) { // Disable the button as we are loosing the reference to the service. findViewById(R.id.trigger_operation_button).setEnabled(false); mService = null; } }
MyActivity.java
附加资源:
Google’s changes to the Service API athttp://android-developers.blogspot.se/2010/02/service-api-changes-starting-with.html
Dianne Hackborn at
http://android-developers.blogspot.se/2010/04/multitaskingandroid-way.html
相关文章推荐
- ListView的两种监听某一项的单击事件的方法
- Android Studio 快捷键
- Android Studio设置行宽、格式化断行
- android之控件布局的几个小案例
- Android中常见的度量单位【xdpi&nb…
- Android推送技术研究
- Android Studio 字体大小和背景色的设置
- Android平台语言支持状态
- Android应用在未启动的情况下无法收到指定广播的问题总结
- Android自定义折线图 不用三方库
- Android 自定义RadioButton实现
- android自定义控件(三)圆环交替等待效果
- 【原创】NIO框架入门(四):Android与MINA2、Netty4的跨平台UDP双向通信实战
- android 应用性能优化
- 反编译Android APK及防止APK程序被反编译
- ListView item错位 由于Item复用时高度不一致导致ListView 显示错误
- Monkeyrunner学习之任意节点控件获取及获取对应控件的相对坐标实现对控件的操作
- Android中Intent的各种常见作用
- Android ComponentName的用法
- 4000 对Android中convertView的深入理解