我有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