1

我有某种短信应用程序。因此,每次手机收到新消息时,它都应该有一个通知,点击它就会启动活动。现在,当收到 1 条消息时,它会通知,在状态栏中删除并且不启动活动。但是当收到 2 条或更多消息时,第一个通知无法在单击时启动,而其余的(第 2 个,第 3 个通知...)可以。以下是我的代码。


Intent newIntent = new Intent(context, PreviewActivity.class);
newIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK+0);
newIntent.setAction(strFxPrimaryKey);

Bundle newBundle = intent.getExtras(); newBundle.putInt(GlobalConstants.PRIMARY_ID, Integer.parseInt(strFxPrimaryKey)); newIntent.putExtras(newBundle);

int icon = R.drawable.notification_fx; CharSequence tickerText = context.getString(R.string.fx_received); long when = System.currentTimeMillis();

Notification newNotification = new Notification(icon, tickerText, when); newNotification.flags |= Notification.FLAG_AUTO_CANCEL; //| Notification.FLAG_ONGOING_EVENT;

PendingIntent contentIntent = PendingIntent.getActivity(context, Integer.parseInt(strFxPrimaryKey), newIntent, 0); newNotification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);

String newNotificationService = Context.NOTIFICATION_SERVICE; NotificationManager newNotificationManager = (NotificationManager) context.getSystemService(newNotificationService); newNotificationManager.notify(Integer.parseInt(strFxPrimaryKey), newNotification);

context.startActivity(newIntent); context.removeStickyBroadcast(intent);

根据 stackoverflow 中的答案,要创建一个独特的意图,它应该具有独特的操作,这就是我将主键设置为操作的原因。我还将请求代码设置为主键以具有唯一的待处理意图。我的代码中是否遗漏了什么?谢谢。

编辑 顺便说一句,每当我删除context.startActivity(newIntent); ,它工作正常。谁能告诉我为什么?谢谢。

4

0 回答 0