Service works for me. and in onStartCommand I do startforeground to avoid system killing.
public int onStartCommand(Intent intent, int flags, int startId) { if (ACTION_STOP_SERVICE.equals(intent.getAction())) { Log.d(TAG,"called to cancel service"); manager.cancel(NOTIFCATION_ID); stopSelf(); } NotificationCompat.Builder builder = new NotificationCompat.Builder(this); builder.setContentTitle("abc"); builder.setContentText("Press below button to stoP."); builder.setPriority(NotificationCompat.PRIORITY_HIGH); builder.setSmallIcon(R.drawable.ic_launcher); Intent stopSelf = new Intent(this, SameService.class); stopSelf.setAction(this.ACTION_STOP_SERVICE); PendingIntent pStopSelf = PendingIntent.getService(this, 0, stopSelf,0); builder.addAction(R.drawable.ic_launcher, "Stop", pStopSelf); manager.notify(NOTIFCATION_ID, builder.build()); }
but after clicking the button, PendingIntent does not work, and my activity does not stop there.
Can someone please tell me what I'm doing here or some other solution to stop the service from the notification front- notification made by myself.
thanks
source share