0

我创建了一个创建通知的广播接收器,通过单击一个通知启动了一个活动,该活动正在启动但我无法查看它,我正在使用以下代码:

private NotificationManager notificationManager;
    @Override
public void onReceive(Context context, Intent intent) {
    if (notificationManager == null) {
        notificationManager = (NotificationManager) context
                .getSystemService(Context.NOTIFICATION_SERVICE);
    }
    NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
            .setTicker("Its Ticker")
            .setSmallIcon(R.drawable.ic_launcher)
            .setContentTitle("Notification Title")
            .setContentText("Its Content")
            .setAutoCancel(true)
            .setContentIntent(PendingIntent.getActivity(context,0,new Intent(context, NotificationDialog.class).addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK),0));
    notificationManager.notify("interstitial_tag", 0, builder.getNotification());

}

注意:NotificationDialog.java是我的活动,此活动在我调试但未查看时正在运行。尝试了几个小时但找不到任何答案,任何建议将不胜感激。

4

1 回答 1

0

使用FLAG_ACTIVITY_CLEAR_TOP而不是FLAG_ACTIVITY_CLEAR_TASK

private NotificationManager notificationManager;
    @Override
public void onReceive(Context context, Intent intent) {
    if (notificationManager == null) {
        notificationManager = (NotificationManager) context
                .getSystemService(Context.NOTIFICATION_SERVICE);
    }
    NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
            .setTicker("Its Ticker")
            .setSmallIcon(R.drawable.ic_launcher)
            .setContentTitle("Notification Title")
            .setContentText("Its Content")
            .setAutoCancel(true)
            .setContentIntent(PendingIntent.getActivity(context,0,new Intent(context, NotificationDialog.class).addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP),0));
    notificationManager.notify("interstitial_tag", 0, builder.getNotification());

}
于 2012-07-12T10:25:52.813 回答