2

我有一个小问题。我有一个 AlarmManager,它向 BroadcastReceiver 发送意图,在onReceive该类的方法中,我将通知推送到 StatusBar ...这就像一个魅力,但是当用户点击通知时我需要打开一个活动,但是在我的代码中存在一些问题,在谷歌搜索和搜索之后我找不到答案......所以这是我推送通知的代码:

Intent intent = new Intent(Intent.ACTION_MAIN);
intent.setClass(this.context, NewCommit.class);     
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);

PendingIntent sender = PendingIntent.getBroadcast(this.context, 192839, intent, PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_ONE_SHOT);

NotificationCompat.Builder noti = new NotificationCompat.Builder(this.context)
    .setSmallIcon(R.drawable.status_bar_icon)
    .setContentTitle("My notification")
    .setContentText("Hello World!")
    .setContentIntent(sender);



noti.setDefaults(Notification.DEFAULT_SOUND | Notification.DEFAULT_VIBRATE);


NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(181818, noti.build());

任何的想法??这让我疯狂!

谢谢!

4

1 回答 1

4

我用一个有效的代码仔细检查了你的代码。我假设 NewCommit 是您的活动。

除了在意图中明确设置活动(类似于Intent intent = new Intent(c, NewCommit.class);

我认为主要的区别(和问题)是你打电话

PendingIntent.getBroadcast

代替

PendingIntent.getActivity

医生

“检索将启动新活动的 PendingIntent,例如调用 Context.startActivity(Intent)。请注意,活动将在现有活动的上下文之外启动,因此您必须在 Intent 中使用 Intent.FLAG_ACTIVITY_NEW_TASK 启动标志。 "

于 2013-02-21T02:38:57.493 回答