IntentService和Service的使用方法基本上是一样的,只不过IntentService更像是一次性的Service,不会长期存在后台。在执行完onHandleIntent方法后就会调用停止方法,结束掉Service。详细的情况可以看一下源码!!!
IntentService也是继承于Service,在IntentService中通过一个HandlerThread来新建一个线程,然后再线程里处理耗时的操作,每执行一个耗时操作都必须重新start这个IntentService。相对于普通的Service来说,它的功能更专一,就是处理耗时操作!!!
在使用start一个普通的Service的时候,Service和Activity运行在同一个进程,那么在service中进行耗时的操作就很有可能引起应用程序的ANR。当然,如果在Service里面new一个新的线程,在子线程里执行耗时操作就另当别论了,在这里就不说这些了!!!主要还是说IntentService!!!
还是看一下源码吧:
IntentService的相关源码:
private final class ServiceHandler extends Handler {
public ServiceHandler(Looper looper) {
super(looper);
}
@Override
public void handleMessage(Message msg) {
onHandleIntent((Intent)msg.obj);
stopSelf(msg.arg1);//从这里就可以看出,执行完耗时操作后,service就被干掉了
}
}
@Override
public void onCreate() {
super.onCreate();
HandlerThread thread = new HandlerThread("IntentService[" + mName + "]");
thread.start();
mServiceLooper = thread.getLooper();
//创建一个Handler,这个时候looper已经运行起来了,在HandlerThread的run方法中。
mServiceHandler = new ServiceHandler(mServiceLooper);
}
@Override
public void onStart(@Nullable Intent intent, int startId) {
Message msg = mServiceHandler.obtainMessage();
msg.arg1 = startId;
msg.obj = intent;
mServiceHandler.sendMessage(msg);
}
@Override
public int onStartCommand(@Nullable Intent intent, int flags, int startId) {
onStart(intent, startId);
return mRedelivery ? START_REDELIVER_INTENT : START_NOT_STICKY;
}
基本代码就这么多,代码也很容易理解在这里就不做多余的说明了!!!
这是我写的一个小demo,用来验证IntentService是否执行之后就被干掉了和各自运行的线程Id.
三个Java文件
MainActivity.java
package com.example.myapplication;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.i("zyq","MainActivity:Thread.id="+Thread.currentThread().getId());
}
public void startCustomService(View view){
Intent intent = new Intent(MainActivity.this,CustomService.class);
startService(intent);
}
public void startCustomIntentService(View view){
Intent intent = new Intent(MainActivity.this,CustomIntentService.class);
startService(intent);
}
}
CustomService.java:
package com.example.myapplication;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.support.annotation.Nullable;
import android.util.Log;
/**
* Created by zhuyuqiang on 2017/3/20.
*/
public class CustomService extends Service {
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
Log.i("zyq","CustomService:onCreate:Thread.id="+Thread.currentThread().getId());
super.onCreate();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
// try {
// Thread.sleep(1000*100);
// } catch (InterruptedException e) {
// e.printStackTrace();
// }
Log.i("zyq","CustomService:onStartCommand:Thread.id="+Thread.currentThread().getId());
return super.onStartCommand(intent, flags, startId);
}
@Override
public void onDestroy() {
super.onDestroy();
}
}
CustomIntentService.java:
package com.example.myapplication;
import android.app.IntentService;
import android.content.Intent;
import android.support.annotation.Nullable;
import android.util.Log;
/**
* Created by zhuyuqiang on 2017/3/20.
*/
public class CustomIntentService extends IntentService {
public CustomIntentService(){
this("CustomIntentService");
}
public CustomIntentService(String name) {
super(name);
}
@Override
public void onCreate() {
Log.i("zyq","CustomIntentService:Thread.id="+Thread.currentThread().getId());
super.onCreate();
}
@Override
public int onStartCommand(@Nullable Intent intent, int flags, int startId) {
Log.i("zyq","CustomIntentService:onStartCommand:Thread.id="+Thread.currentThread().getId());
return super.onStartCommand(intent, flags, startId);
}
@Override
public void onDestroy() {
super.onDestroy();
}
@Override
protected void onHandleIntent(@Nullable Intent intent) {
// try {
// Thread.sleep(1000*100);
// } catch (InterruptedException e) {
// e.printStackTrace();
// }
Log.i("zyq","CustomIntentService:handler:Thread.id="+Thread.currentThread().getId());
}
}
在看一下运行的Log:
I/zyq (29743): MainActivity:Thread.id=1
I/zyq (29743): CustomService:onCreate:Thread.id=1
I/zyq (29743): CustomService:onStartCommand:Thread.id=1
I/zyq (29743): CustomIntentService:Thread.id=1
I/zyq (29743): CustomIntentService:onStartCommand:Thread.id=1
I/zyq (29743): CustomIntentService:handler:Thread.id=3672
I/zyq (29743): CustomService:onStartCommand:Thread.id=1
I/zyq (29743): CustomIntentService:Thread.id=1
I/zyq (29743): CustomIntentService:onStartCommand:Thread.id=1
I/zyq (29743): CustomIntentService:handler:Thread.id=3673
根据log就可以看出,每次启动IntentService都会重走onCreate方法,而正常的Service则不会走,说明在起动IntentService的时候,他已经被干掉了(起码不是活跃状态了)!!!
关于Service和IntentService的区别,目前只是看出这些,以后有什么新的发现还会接着补充的,谢谢!!!!