I\'m using a background service to run a background music for my app in all of it\'s activities! The issue is that when the app run, it works fine but when I close it it keeps o
In the onDestroy()
method of your MainActivity you have to stop the service.
@Override
protected void onDestroy() {
super.onDestroy();
if(isMyServiceRunning(Background_music.class))
{
stopService(new Intent(this, Background_music.class));
}
}
private boolean isMyServiceRunning(Class<?> serviceClass) {
ActivityManager manager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
for (ActivityManager.RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
if (serviceClass.getName().equals(service.service.getClassName())) {
return true;
}
}
return false;
}
Hope this helps you.