0

I have NotificationService class extends Service and I launch the service whit method, but I don't know how to stop the current Service, I read some guide and example but with stopService() method I can't stop my service. This is the code of my class, how I can stop?

public class NotificationService extends Service {

private final long TIME_WAKE_UP = 6000;//60 * 60 * 1000;
private long timeStart;
private Esercizio es;

@Override
public void onCreate() {
    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this);
    Notification mNt = mBuilder.build();
    startForeground(2, mNt);
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    timeStart = intent.getExtras().getLong("timeStart");
    es = (Esercizio) intent.getExtras().get("esercizio");

    while(System.currentTimeMillis() < timeStart + TIME_WAKE_UP);

    atWork();

    stopForeground(true);
    stopSelf();

    return START_STICKY;
}

private void atWork() {
    Intent intent = new Intent(this, TransitionArchive.class);
    Bundle bundle = new Bundle();
    bundle.putString("notificationService","");
    bundle.putString("KEY_EXCERSIZE",es.getNameEx());
    intent.putExtras(bundle);

        NotificationCompat.Builder mBuilderOffline =
                new NotificationCompat.Builder(this)
                        .setSmallIcon(R.drawable.logo_app_android)
                        .setContentTitle(es.getNameEx())
                        .setContentText("C'è un nuovo contenuto per te!")
                        .setAutoCancel(true)
                        .setPriority(Notification.PRIORITY_MAX)
                        .setVisibility(NotificationCompat.VISIBILITY_PUBLIC);

        PendingIntent pendingIntent = PendingIntent.getActivity(this,
                1,
                intent,
                PendingIntent.FLAG_UPDATE_CURRENT);
        mBuilderOffline.setContentIntent(pendingIntent);

        //PendingIntent call the receive class

        Notification note = mBuilderOffline.build();
        note.defaults |= Notification.DEFAULT_SOUND;
        note.defaults |= Notification.DEFAULT_VIBRATE;

        NotificationManager mNotificationManager =
                (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        mNotificationManager.notify(1, note);
}

public static void startNotificationService(Context mContext, Esercizio es) {
    Intent mIntent = new Intent(mContext, NotificationService.class);
    mIntent.putExtra("esercizio", es);
    mIntent.putExtra("timeStart", System.currentTimeMillis());
    startWakefulService(mContext, mIntent);
}

@Override
@Nullable
public IBinder onBind(Intent intent) {
    return null;
}
Misa Grace
  • 23
  • 3

2 Answers2

0

It may not be the best way, but I used a public static variable as a flag with an IntentService. If the flag is set, perform any necessary cleanup, stop threads, exit loops, and let it flow to stopSelf() and stopForeground() naturally (that you already have).

How to check if a service is running on Android?

  • And how i can use this flag? i want to stop my current service from another class, not the NotificationService class – Misa Grace Jun 12 '17 at 19:48
  • U can post the code please? where i put this static variable? i can't stop my service from another class, help me please – Misa Grace Jun 12 '17 at 20:59
  • I updated the answer, please see the examples already provided by other users: [How to Check if a Service is Running on Android](https://stackoverflow.com/questions/600207/how-to-check-if-a-service-is-running-on-android). –  Jun 12 '17 at 23:43
  • Ok but i now when the service is started, but i don't know how to kill it – Misa Grace Jun 15 '17 at 19:38
  • From the [doc](https://developer.android.com/guide/components/services.html#Stopping), it says "a started service must manage its own lifecycle... The service must stop itself by calling stopSelf() or another component can stop it by calling stopService()" If you are using a boolean to disable the service from another class, then for any loops in the service -- test if the boolean is true and break out of the loops and then stopForeground() and stopSelf() will be stop the service. –  Jun 15 '17 at 20:00
  • If in my while i set if whit a static boolean and execute stopForeground() and stopSelf() my service don't sop the run, i think because my boolean is false, and boolean == true from another class don't work – Misa Grace Jun 15 '17 at 20:24
0

Try like this

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    timeStart = intent.getExtras().getLong("timeStart");
    es = (Esercizio) intent.getExtras().get("esercizio");

    while(System.currentTimeMillis() < timeStart + TIME_WAKE_UP);

    atWork();

    stopForeground(true);
    stopSelf();

    return START_NOT_STICKY;
}
VinayagaSundar
  • 1,673
  • 17
  • 18