Sim*_*der 16 service notifications android kill
我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());
}
Run Code Online (Sandbox Code Playgroud)
但按下按钮后,PendingIntent
工作不正常,我activity
不会被它拦住.
有人可以告诉我,我在这里做错了什么或任何其他解决方案来停止notification
自我制作的前台服务.
谢谢
Sim*_*der 24
为了像我这样的其他发现者,回答我自己的问题.
问题出现在下面
PendingIntent pStopSelf = PendingIntent.getService(this, 0, stopSelf,0);
Run Code Online (Sandbox Code Playgroud)
这个0到底是问题的原因.我用PendingIntent.FLAG_CANCEL_CURRENT替换了它,现在它可以工作了.
更正的代码是:
PendingIntent pStopSelf = PendingIntent.getService(this, 0, stopSelf,PendingIntent.FLAG_CANCEL_CURRENT);
Run Code Online (Sandbox Code Playgroud)
有关详细说明,请查看FLAG_CANCEL_CURRENT或FLAG_UPDATE_CURRENT.
归档时间: |
|
查看次数: |
9183 次 |
最近记录: |