yyy269954107的专栏

Service组件

身为四大组件之一的Service在程序中使用频率是比较高的,主要用来处理一些不需要UI的耗时的后台操作,或者在后台对外提供接口,供其他组件调用。Service的实现是比较典型的C/S模式,后文介绍用法时会有体会。

两种常见的Service

IntentService:适合同一时间只处理一个任务,代码少,使用简单 是Service类的子类,默认会开启一个工作线程,你需要覆盖onHandleIntent方法,用来处理startService传过来的Intent,在一个生命周期内,如果多次调用startService只会进一次onCreate(),但是会进对应次数的onHandleIntent,在同一时间只能处理一个Intent,也就是说不管调用多少次startService只有一个工作线程在工作,其余的排队等待,一旦所有的Intent处理完成,自动调用onDestroy方法,无须手动调用stopService或者stopSelf

IntentService的示例

{String TAG = “MyIntentService”;String ACTION_ONE = “action_one”;String ACTION_TWO = “action_two”;/*** A constructor is required, and must call the super IntentService(String)* constructor with a name for the worker thread.*/public MyIntentService() {super(“MyIntentService”);}(Intent intent) {if (intent != null) {final String action = intent.getAction();if (ACTION_ONE.equals(action)) {handleActionOne();} else if (ACTION_TWO.equals(action)) {handleActionTwo();}}}(){for (int i = 0;i<3;i++){Log.i(TAG,”handleActionOne”);try {Thread.sleep(1000);} catch (InterruptedException e) {e.printStackTrace();}}}(){for (int i = 0;i<3;i++){Log.i(TAG,”handleActionTwo”);try {Thread.sleep(1000);} catch (InterruptedException e) {e.printStackTrace();}}}() {super.onCreate();Log.i(TAG,”onCreate”);}() {super.onDestroy();Log.i(TAG,”onDestroy”);}}

可以通过如下方法调用多次同时调用

Intent intent = new Intent(this,MyIntentService.class); intent.setAction(MyIntentService.ACTION_ONE);//intent.setAction(MyIntentService.ACTION_TWO);startService(intent);

调用后我们会发现IntentService不需要我们另起线程来执行耗时的操作,同时你无论触发多少次ACTION_ONE或者ACTION_TWO动作,它们都会按触发的顺序顺序执行,且任务一旦执行完成无需我们调用stopService或者stopSelf就会自己进入onDestory

Service:适合并发请求,代码较多,较复杂,更灵活 需要手动调用stopService或者执行完任务后调用stopSelf ,即使Service销毁了,其中的线程如果没有执行完会继续执行,如果不掉用的话即使启动该Service的activity销毁了,该Service仍然存在,系统内存不够时会销毁后台的service,service存在时间越长,被销毁的可能性越大

Service的示例

{String TAG = “MyService”;public MyService() {}@Overridepublic IBinder onBind(Intent intent) {;}() {super.onCreate();Log.i(TAG,”onCreate”);}(Intent intent, int flags, int startId) {MyServiceThread thread = new MyServiceThread(startId);thread.start();// If we get killed, after returning from here, restartreturn START_STICKY;}() {super.onDestroy();Log.i(TAG,”onDestroy”);}class MyServiceThread extends Thread{int startId = 0;public MyServiceThread(int startId){this.startId = startId;}() {for(int i = 0;i<3;i++){Log.i(TAG,”my startId:”+startId);try {sleep(1000);} catch (InterruptedException e) {e.printStackTrace();}}//根据startId停止,能保证最近一次的请求执行完才销毁stopSelf(startId);}}}

我们可以通过startService多次调用,但运行完需要显式的调用stopSelf来结束自己,在这里例子中我们可以看到在Service中需要我们自己启动线程来执行耗时的任务,可以同时并行的执行多个任务,还能看到startId的用处,可以用来保证最近的一次请求执行完才onDestory

service使用方式

startService

可以直接看上面的两个例子

bindService

继承binder类

如果只是想利用Service作为你应用的一个后台工作线程的话,这是首选的实现方式,除非你的Service需要被其他应用使用或者跨进程通讯。

先看Service代码

{String TAG = “MyLocalService”;IBinder mBinder = new LocalBinder();Random mGenerator = new Random();{MyLocalService getService(){// Return this instance of LocalService so clients can call public methodsreturn MyLocalService.this;}}@Overridepublic IBinder onBind(Intent intent) {return mBinder;}(){return mGenerator.nextInt(100);}(Intent intent, int flags, int startId) {Log.i(TAG,”onStartCommand”);return super.onStartCommand(intent, flags, startId);}(Intent intent) {Log.i(TAG,”onUnbind”);return super.onUnbind(intent);}(Intent intent) {Log.i(TAG,”onRebind”);super.onRebind(intent);}() {Log.i(TAG,”onCreate”);super.onCreate();}() {Log.i(TAG,”onDestroy”);super.onDestroy();}}喜欢真实的人,要做真实的人,所以从来不会想要刻意模仿任何人。

yyy269954107的专栏

相关文章:

你感兴趣的文章:

标签云: