Android Service自启动注意事项分析

时间:2022-06-22 06:45:57

本文实例分析了android service自启动注意事项。分享给大家供大家参考,具体如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
@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;就可以了。

希望本文所述对大家android程序设计有所帮助。