【Demo验证】Service的onStartCommand返回值

一、引言

上一篇文章 【源码解析】Service的onStartCommand返回值 从源码的角度分析了Service的onStartCommand的返回值的意义。

这篇文章承接上面的文章得出的结论,以一个Demo来验证onStartCommand方法不同的返回值会呈现什么效果。

二、Demo代码

1、MainActivity:

/**
 * Created by hwldzh on 2018/10/16
 */
public class MainActivity extends Activity {
    private Intent intent;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_activity);
        findViewById(R.id.startService).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                intent = new Intent(MainActivity.this, MyService.class);
                startService(intent);
            }
        });
    }
}

2、MyService:

/**
 * Created by hwldzh on 2019/1/2
 */
public class MyService extends Service {
    @Override
    public void onCreate() {
        super.onCreate();
        Log.d("ABC", "MyService onCreate");
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                Integer.parseInt("haha"); //这里故意执行了一个异常
            }
        }, 5000);
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.d("ABC", "MyService onStartCommand");
        Log.d("ABC", "startId=" + startId);
        Log.d("ABC", "intent=" + intent);
        //这里的返回值可以修改为START_STICKY、START_NOT_STICKY、START_REDELIVER_INTENT
        return Service.START_STICKY_COMPATIBILITY;
    }

    @Override
    public boolean onUnbind(Intent intent) {
        Log.d("ABC", "MyService onUnbind");
        return super.onUnbind(intent);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.d("ABC", "MyService onDestroy");
    }
}

为了让Service进程能够被杀死,这里使用了一个延时执行的Runnable,在Runnable中故意执行了一个异常。

三、结论展示

在onStartCommand方法中分别返回START_STICKY_COMPATIBILITY、START_STICKY、START_NOT_STICKY、START_REDELIVER_INTENT这四个值,并打印出Log,得出下面的结论:

1、START_STICKY_COMPATIBILITY

D/ABC: MyService onCreate
D/ABC: MyService onStartCommand
D/ABC: startId=1
D/ABC: intent=Intent { cmp=com.mucfc.myapplication/.MyService }
D/ABC: MyService onCreate

当返回值为START_STICKY_COMPATIBILITY时,Service被杀死后会重启(执行了onCreate方法),但是onStartCommand方法没有被执行

2、START_STICKY

D/ABC: MyService onCreate
D/ABC: MyService onStartCommand
D/ABC: startId=1
D/ABC: intent=Intent { cmp=com.mucfc.myapplication/.MyService }
D/ABC: MyService onCreate
D/ABC: MyService onStartCommand
D/ABC: startId=2
D/ABC: intent=null

当返回值为START_STICKY时,Service被杀死后会重启(执行了onCreate方法),并且onStartCommand方法会执行。从startId可以看出是Service对象不是同一个,并且onStartCommand方法中的intent值为null

3、START_NOT_STICKY

D/ABC: MyService onCreate
D/ABC: MyService onStartCommand
D/ABC: startId=1
D/ABC: intent=Intent { cmp=com.mucfc.myapplication/.MyService }

当返回值为START_NOT_STICKY时,Service被杀死后并不会重启(Service的onCreate没有被第二次被执行)。

4、START_REDELIVER_INTENT

D/ABC: MyService onCreate
D/ABC: MyService onStartCommand
D/ABC: startId=1
D/ABC: intent=Intent { cmp=com.mucfc.myapplication/.MyService }
D/ABC: MyService onCreate
D/ABC: MyService onStartCommand
D/ABC: startId=1
D/ABC: intent=Intent { cmp=com.mucfc.myapplication/.MyService }

当返回值为START_REDELIVER_INTENT时,Service被杀死后会重启(执行了onCreate方法),并且onStartCommand方法会执行。从startId可以看出两个Service对象是同一个,并且onStartCommand方法中的intent和前一个intent值是一致的

从上述的Demo展示出来的Log,得出的结论和【源码解析】Service的onStartCommand返回值 这篇文章的结论的一致的,从而验证了onStartCommand返回值的结论的正确性。

你可能感兴趣的:(【Demo验证】Service的onStartCommand返回值)