如何防止在Android中启动多个startId

时间:2018-03-08 21:19:27

标签: android android-studio android-service

我正在使用以下代码,当我点击按钮时,我会在服务中获得多个startId创作。有人可以告诉我如何防止在Android中启动多个startId吗?

@Override
public int onStartCommand(Intent intent, int flags, final int startId) {
handler.postDelayed(new Runnable() {
public void run() {
System.out.println(startId+ " == startId");
handler.postDelayed(this, 8000);
}
}, 8000); 

return super.onStartCommand(intent, flags, startId);}

这是我得到的输出:

03-09 02:39:14.128 18298-18298/com.simple.project I/System.out: 1 == startId

03-09 02:39:14.268 18298-18298/com.simple.project I/System.out: 2 == startId

03-09 02:39:14.449 18298-18298/com.simple.project I/System.out: 3 == startId

03-09 02:39:14.719 18298-18298/com.simple.project I/System.out: 4 == startId

03-09 02:39:15.059 18298-18298/com.simple.project I/System.out: 5 == startId

03-09 02:39:15.219 18298-18298/com.simple.project I/System.out: 6 == startId

03-09 02:39:15.409 18298-18298/com.simple.project I/System.out: 7 == startId

03-09 02:39:15.580 18298-18298/com.simple.project I/System.out: 8 == startId

03-09 02:39:15.740 18298-18298/com.simple.project I/System.out: 9 == startId

03-09 02:39:15.900 18298-18298/com.simple.project I/System.out: 10 == startId

1 个答案:

答案 0 :(得分:0)

startId正好是一个唯一整数,表示要启动的特定请求。 每次调用时它都会增加到startService()