Android代码删除通知,如何使通知无法删除/不可删除

即使用户点击它或点击全部清除,我也想让我的Android通知停留…

现在它有时停留,并且有时被删除,我不确定是什么导致它。

这是我的通知代码:

@TargetApi(Build.VERSION_CODES.JELLY_BEAN) public static void createNotification() { NotificationManager notificationManager = (NotificationManager)context.getSystemService(NOTIFICATION_SERVICE); NotificationCompat.Builder builder = new NotificationCompat.Builder(context) .setContentTitle("Wolftech Field Agent") .setContentText("Getting Status") .setSmallIcon(R.drawable.ic_launcher) .setOngoing(true) .setAutoCancel(false); Intent intent = new Intent(context, FieldAgent.class); intent.setAction(Intent.ACTION_MAIN); intent.addCategory(Intent.CATEGORY_LAUNCHER); TaskStackBuilder stackBuilder = TaskStackBuilder.create(context); stackBuilder.addParentStack(FieldAgent.class); stackBuilder.addNextIntent(intent); PendingIntent pendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT); builder.setContentIntent(pendingIntent); notificationManager.notify(NOTIFICATION_ID, builder.build()); } public static void updateNotificationText(String inString) { NotificationManager notificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE); NotificationCompat.Builder builder = new NotificationCompat.Builder(context) .setContentText(inString) .setContentTitle("Wolftech Field Agent") .setSmallIcon(R.drawable.ic_launcher) .setOngoing(true) .setAutoCancel(false); Intent intent = new Intent(context, FieldAgent.class); intent.setAction(Intent.ACTION_MAIN); intent.addCategory(Intent.CATEGORY_LAUNCHER); TaskStackBuilder stackBuilder = TaskStackBuilder.create(context); stackBuilder.addParentStack(FieldAgent.class); stackBuilder.addNextIntent(intent); PendingIntent pendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT); builder.setContentIntent(pendingIntent); notificationManager.notify(NOTIFICATION_ID, builder.build()); } public static void cancelNotification() { NotificationManager notificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE); notificationManager.cancel(NOTIFICATION_ID); }

我认为您正在寻找持续通知,在这种情况下,如果您使用NotificationCompat.Builder ,您可以使用:

NotificationCompat.Builder mNotifyBuilder = new NotificationCompat.Builder(this); mNotifyBuilder.setOngoing(true);

我相信你正在寻找这面旗帜:

Notification.FLAG_NO_CLEAR

将该标志添加到您的通知中。

你试试

PendingIntent pendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT); notificationManager.cancel(pendingIntent);

哦,我意识到我发布的代码实际上很好。

发生的事情是,当我点击通知时,它再次调用onCreate(),然后在一个随机间隔后调用onDestroy()。

在onDestroy()中,我有了cancelNotification()方法,所以如果在onCreate()之后调用onDestroy(),它就会删除我的通知。

这让我想到了一个新问题:为什么在我遵循我在这里find的关于如何阻止这种情况发生的每一个答案之后,它会破坏并重新创建我的活动?

您可以在此处find该问题如何恢复通知而不重新创建活动? 如果你想帮我解决一下……

我也有这个问题。 我的解决方案是在创建意图的函数上添加额外function

private void addNotification(String headLine, String info, Context context) { mBuilder = new NotificationCompat.Builder(context) .setSmallIcon(R.drawable.icon) //R.drawable.icon .setContentTitle(headLine) .setContentText(info) .setOngoing(true) .setAutoCancel(false); // Creates an explicit intent for an Activity in your app Intent resultIntent = new Intent(context, MainActivity.class); resultIntent.putExtra("FROM_NOTIF", true); // The stack builder object will contain an artificial back stack for the // started Activity. // This ensures that navigating backward from the Activity leads out of // your application to the Home screen. TaskStackBuilder stackBuilder = TaskStackBuilder.create(context); // Adds the back stack for the Intent (but not the Intent itself) // stackBuilder.addParentStack(MainActivity.class); //Adds the Intent that starts the Activity to the top of the stack stackBuilder.addNextIntent(resultIntent); PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT); mBuilder.setContentIntent(resultPendingIntent); mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); // mId allows you to update the notification later on. // mBuilder.setf |= Notification.FLAG_ONGOING_EVENT | Notification.FLAG_NO_CLEAR; mNotificationManager.notify(mNotification_id, mBuilder.build()); }

并在MainActivity中:

@Override protected void onNewIntent(Intent intent) { super.onNewIntent(intent); if (intent.getBooleanExtra("FROM_NOTIF", false)) { addNotification(...) ... call the function that adds the notification again ..}

所以事情是用户按下它后通知会关闭,但是你可以通过onNewIntent()开始通知活动,检查那里的意图,如果你发现它来自通知,请设置它再次。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值