Kot*_*tuc 5 java notifications android
我创建了一个扩展Service的类,并作为前台服务运行.我希望我的服务通知是持久的(即不能通过刷卡删除).但是,我的通知可以通过滑动来解除.
服务文档说明:...前台服务必须提供状态栏的通知,该通知位于"正在进行"标题下.这意味着除非服务被停止或从前台移除,否则不能解除通知...
我确实设置了断点来检查onDestroy()或stopSelf()是否被击中,但事实并非如此.该服务正在前台模式下运行,但我可以通过滑动来关闭该通知.
我发现了很多关于相反情况的问题,在服务停止后无法解除通知,但没有发现任何类似于我的问题的问题.
该服务通过Intent启动并初始化如下:
@Override
public void onCreate()
{
super.onCreate();
initialize();
}
private void initialize()
{
Notification n = get_service_notification();
startForeground(10, n);
LocalBroadcastManager.getInstance(this).registerReceiver(mMessageReceiver, new IntentFilter(BroadcastCodes.service_broadcast_intent_name));
}
@Override
public int onStartCommand(Intent intent, int flags, int startId)
{
startServing();
return START_STICKY;
}
Run Code Online (Sandbox Code Playgroud)
其中startServing()订阅位置更新
通知是这样构建的
private Notification get_service_notification()
{
Intent intent = new Intent(this, LoginActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder b = new NotificationCompat.Builder(this);
b.setAutoCancel(false)
.setWhen(System.currentTimeMillis())
.setSmallIcon(R.drawable.app_icon)
.setTicker("some text")
.setContentTitle("some text")
.setContentText("some text")
.setContentIntent(contentIntent)
.setContentInfo("")
.setOngoing(true);
Notification res = b.build();
res.flags |= Notification.FLAG_ONGOING_EVENT | Notification.FLAG_NO_CLEAR;
return res;
}
Run Code Online (Sandbox Code Playgroud)
我试过玩过标志和setOngoing/setAutoCancel,但结果保持不变.
SDK版本配置为minSdkVersion 18,targetSdkVersion 25
我真的很感激有关这个问题的任何意见 - 这可能只是一些愚蠢的错误,但我已经花了超过几个小时试图自己研究......遗憾的是无济于事.
编辑1
我还没弄明白这个问题.我决定尝试一个肮脏的黑客,但仍然没有运气.
我创建了一个PendingIntent并通过setDeleteIntent()注册它,这个想法是在刷一下这个通知后显示一个新的通知.不幸的是,我无法使其工作(滑动时意图永远不会发生).
我还想澄清一下,通知可以通过通知窗格中的clear-all按钮(垃圾邮件图标)轻扫,但不会受到影响(即不会被删除).
编辑2
如上所述,当刷卡解除时,我无法使用setDeleteIntent()重新创建通知.
现在我解决了一个问题 - 我的服务做了一些定期任务.我正在打电话
notificationManager.notify(10, service_notification);
Run Code Online (Sandbox Code Playgroud)
当我的任务运行时,即使被刷掉,我的通知也会在一段时间后重新创建.
我仍然有一种强烈的感觉,我只是误读了文档,因为我的测试设备(荣誉API23)上有几个通知无法刷掉.
编辑3
我在不同的设备(Lenovo API23)上尝试了相同的代码,并且通知按预期工作(不能被解雇且无法刷卡).我的问题似乎是设备特定的.