Here is my code -
public class BackgroundService extends Service {
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
super.onStartCommand(intent, flags, startId);
new ServiceThread(startId);
return START_NOT_STICKY;
}
class ServiceThread extends Thread {
private int startId;
ServiceThread(int startId) {
this.startId = startId;
}
@Override
public void run() {
super.run();
try {
Thread.sleep((long) Math.random());
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
stopSelf(startId);
}
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
}
According to this “Stop service ” link , I can / should call stopSelf with the received “startId”.
, onStartCommand() , , , ( ). , stopSelf (int), , . , stopSelf (int), (startId onStartCommand()), . , , stopSelf (int), , .
: , stopSelf "startId", - . startId , ? "" ?
"", , , .