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;
}