Service自启动注意的事项

来源:互联网 发布:win10安装mac os x 编辑:程序博客网 时间:2024/05/19 09:12
    @Override
    public void onStart(Intent intent, int startId) {
        super.onStart(intent, startId);


        if(intent == null)

            return;

}


    @Override
    public void onDestroy() {
        super.onDestroy();

        Intent intent = new Intent(SensorService.this, SensorService.class);
        startService(intent);
    }


    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
//        flags = Service.START_STICKY;
        flags = Service.START_REDELIVER_INTENT;
        return super.onStartCommand(intent, flags, startId);
    }


PS:重点就在这个 flags ,开始时用Service.START_STICKY; Service 经常会自动FC查了好久才发现再次自启动时候onStart里intent为null,改为flags = Service.START_REDELIVER_INTENT;就可以了。