Android中Services之异步IntentService
来源:互联网 发布:excel两张表数据匹配 编辑:程序博客网 时间:2024/06/05 08:31
Android中Services之异步IntentService
IntentService:异步处理服务,新开一个线程:handlerThread在线程中发消息,然后接受处理完成后,会清理线程,并且关掉服务。
IntentService有以下特点:
(1) 它创建了一个独立的工作线程来处理所有的通过onStartCommand()传递给服务的intents。
(2) 创建了一个工作队列,来逐个发送intent给onHandleIntent()。
(3) 不需要主动调用stopSelft()来结束服务。因为,在所有的intent被处理完后,系统会自动关闭服务。
(4) 默认实现的onBind()返回null
(5) 默认实现的onStartCommand()的目的是将intent插入到工作队列中
继承IntentService的类至少要实现两个函数:构造函数和onHandleIntent()函数。要覆盖IntentService的其它函数时,注意要通过super调用父类的对应的函数。
界面设置两按钮:
<Button android:id="@+id/btnStartIntentService" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="开始下载" > </Button> <Button android:id="@+id/btnStopIntentService" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="结束下载" > </Button>
声明IntentServiceSub继承IntentService
public class IntentServiceSub extends IntentService { private static final String TAG = "IntentServiceSub"; public IntentServiceSub() { super("IntentServiceSub"); Log.i(TAG, "=>IntentServiceSub"); } /* (non-Javadoc) * @see android.app.IntentService#onCreate() */ @Override public void onCreate() { Log.i(TAG, "=>onCreate"); super.onCreate(); } /* (non-Javadoc) * @see android.app.IntentService#onDestroy() */ @Override public void onDestroy() { Log.i(TAG, "=>onDestroy"); super.onDestroy(); } @Override protected void onHandleIntent(Intent arg0) { Log.i(TAG, "IntentService 线程:"+Thread.currentThread.getId()); Thread.sleep(2000); }
页面按钮事件
btnStartIntentService = (Button) this.findViewById(R.id.btnStartIntentService); btnStopIntentService = (Button) this.findViewById(R.id.btnStopIntentService);private OnClickListener listener = new OnClickListener() { @Override public void onClick(View v) { case R.id.btnStartIntentService: Log.i(TAG, "主线程ID:"+Thread.currentThread.getId()); if (mServiceIntent == null) mServiceIntent = new Intent(AndroidServiceActivity.this,IntentServiceSub.class); startService(mServiceIntent); break; case R.id.btnStopIntentService: Log.i(TAG, "btnStopIntentService"); if (mServiceIntent != null) { stopService(mServiceIntent); mServiceIntent = null; } break; } } };
0 0
- Android中Services之异步IntentService
- Android中Services之异步IntentService
- Android中Services之异步IntentService
- Android中Services之异步IntentService
- Android中Services之异步IntentService
- Android中Services之异步IntentService
- Android中Services之异步IntentService
- android异步任务之IntentService
- Android异步加载全解析之IntentService
- android中service之intentService
- Android之在IntentService中执行后台程序
- Android之IntentService
- android之IntentService
- Android 之IntentService学习
- android之intentservice
- Android开发之IntentService
- Android之IntentService
- android之IntentService
- 深度优先搜索2-Shredding Company(算法基础 第7周)
- C++ 多态 polymorphic 运行时期多态
- JavaScript ES6新的类继承特性学习笔记
- Pig 学习之 Join 、Group、sort、Union
- 4-4认识<img>标签,为网页插入图片
- Android中Services之异步IntentService
- Hadoop中Partition解析 和 storm的grouping
- Android系统系统升级过程分析之------update.zip包的制作
- 《Java集合》学习笔记
- 数据库插入的小问题(自我总结)
- 使用表单标签,与用户交互
- 测试 PIC16F877A 《==》 MAX6902
- 如何制作弹出窗口
- Lua入门教程 6.迭代器与泛型for