现在的位置: 首页 > 综合 > 正文

Service自启动注意的事项

2019年06月06日 ⁄ 综合 ⁄ 共 610字 ⁄ 字号 评论关闭

   @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;就可以了。

抱歉!评论已关闭.