I want to stop service from activity . It stops when i don't close the app . But when i close the app and run the app again service doesn't stop.
Flow :
--> Click Switch ON
--> Show Notification / Location Listener
--> Kill App
--> Notification still remains (Service is running means)
--> Open app again and
--> Click Switch OFF
--> Service doesn't stop and notification persists
Service Class
public class locationService extends Service {
NotificationCompat.Builder builder;
NotificationManager notificationManager;
public static final String serviceTag = "LocationServiceTag";
@Nullable
@Override
public IBinder onBind(Intent intent) {
return mBinder;
}
@Override
public boolean onUnbind(Intent intent) {
return super.onUnbind(intent);
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
SharedPreferences sharedPref = getSharedPreferences("AppPref",Context.MODE_PRIVATE);
userCurrentRoute= sharedPref.getString("RouteNo","");
mDatabase = FirebaseDatabase.getInstance().getReference();
notificationManager = (NotificationManager) this.getSystemService(NOTIFICATION_SERVICE);
Intent repeating_intent =new Intent(this,MainActivity.class);
PendingIntent pendingIntent= PendingIntent.getActivity(this,10,repeating_intent,PendingIntent.FLAG_UPDATE_CURRENT);
builder = new NotificationCompat.Builder(this)
.setContentIntent(pendingIntent)
.setSmallIcon(R.drawable.bus_small)
.setContentTitle("BusBuzz")
.setContentText("Sharing Location to all")
.setAutoCancel(false)
.setOnlyAlertOnce(true)
.setOngoing(true)
;
notificationManager.notify(10,builder.build());
provider = new LocationGooglePlayServicesProvider();
provider.setCheckLocationSettings(true);
return START_NOT_STICKY;
}
@Override
public void onCreate() {
super.onCreate();
}
@Override
public void onDestroy() {
notificationManager.cancel(10);
Toast.makeText(this, "App Closed Location Sharing Stopped", Toast.LENGTH_SHORT).show();
SmartLocation.with(this).location().stop();
super.onDestroy();
}
}
Activity
if (isChecked) {
startService(new Intent(MainActivity.this,locationService.class).addCategory(locationService.serviceTag));
}
else
{
stopService(new Intent(MainActivity.this,locationService.class).addCategory(locationService.serviceTag));
}
You wrote:
--> Click Switch ON
--> Show Notification / Location Listener
--> Kill App
--> Notification still remains (Service is running means)
Just because the notification exists, does not mean that the Service
is still running. Actually, it is dead. You killed the App, which kills the Service
, and since you return START_NOT_STICKY
from onStartCommand()
, Android will not restart the Service
until your App makes another explicit call to startService()
. Since the App was killed, onDestroy()
was never called, so the notification was never removed.
You can verify this by using adb shell dumpsys activity services
to see if your Service
is running after you kill your App.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.