【发布时间】:2015-05-24 10:26:06
【问题描述】:
我有一个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 自己制作。
谢谢
【问题讨论】:
标签: android service notifications kill