我有Service
正在運行。並在其onStartCommand
我做startforeground
,以避免系統查殺。如何從自己的前臺通知中停止服務
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());
}
但按下按鈕後,PendingIntent
不工作,我的activity
是沒有得到通過它停止。
有人可以告訴我,我在這裏做什麼或任何其他解決方案停止從自己做的前景notification
服務。
謝謝
你是一個傳奇! – Maysara