I am stucked at this situation about android services. When start the application, in "onCreate" method called startService to start myService. And when the application destroyed, called stopService to stop myService. The problem is: When minimize app, and start app again via widget, the application start the activity, and start new instance of myService. Expected solution for stop last myService every time application start - only 1 instance of mySerive running ?
Asked
Active
Viewed 41 times
2 Answers
0
Try to check if the service is running before starting it again.
if(startService(someIntent) != null) {
Toast.makeText(getBaseContext(), "Service is already running", Toast.LENGTH_SHORT).show();
}
else {
Toast.makeText(getBaseContext(), "There is no service running, starting service..", Toast.LENGTH_SHORT).show();
}
For further information look here.

Lukas
- 812
- 1
- 14
- 43
-
My mistake. In myService called LocationChangedListener and the ListenerCallback log the Location. When start the app via Widget, the service actually restart but the LocationListener was not removed - still loged the location. By removing the LocationListener at onDestroy() of myService there no more log. – DoanND Nov 13 '20 at 09:30
0
You just need to make a function in this function to start the service but before starting service, stop the service and call this function where you want to start the service. It's not a proper answer but it works great.
public void startsrvc() {
stopService(someIntent);
startService(someIntent);
}

Son Truong
- 13,661
- 5
- 32
- 58

BugsCreator
- 433
- 3
- 10
-
My mistake. In myService called LocationChangedListener and the ListenerCallback log the Location. When start the app via Widget, the service actually restart but the LocationListener was not removed - still loged the location. By removing the LocationListener at onDestroy() of myService there no more log. – DoanND Nov 13 '20 at 09:30