活动结束后通知消失

时间:2015-04-01 07:47:26

标签: android android-manifest android-notifications

我有一个名为A的活动,从broadcast receiver开始。活动A触发了通知,但在活动销毁(完成)时它会自动消失。但是我希望在用户点击或手动清除通知之前保留此通知。

活动如何从广播接收器

开始
Intent i = new Intent(context,A.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
i.putExtras(intent.getExtras());
context.startActivity(i);

通知

Intent notificationIntent = new Intent();
notificationIntent.setClass(context,B.class);
notificationIntent.setAction(Intent.ACTION_MAIN);
notificationIntent.addCategory(Intent.CATEGORY_LAUNCHER);
notificationIntent.setFlags( Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
Notification notification = new NotificationCompat.Builder(context)
        .setSmallIcon(R.drawable.notification_small)
        .setContentTitle(status)
        .setTicker(status)
        .setAutoCancel(false)
        .setContentText(message)
        .setDefaults(Notification.DEFAULT_SOUND)
        .setLargeIcon(
                Bitmap.createScaledBitmap(icon, 128, 128, false))
        .setContentIntent(pendingIntent)
        .build();

清单选项

<activity android:name="com.example.activity.A"
android:screenOrientation="portrait"
android:launchMode="singleTask"
android:taskAffinity=""
android:excludeFromRecents="true"/>

注意:我也尝试过singleInstance,但没有运气。

修改(固定)

我犯了一个愚蠢的错误。我调用了clearAll()而不是取消onDestroy()函数中的特定通知。

2 个答案:

答案 0 :(得分:3)

我认为您在活动被销毁时取消了应用中的所有通知。

答案 1 :(得分:1)

完全删除autocancel()并试试这个,

    public static final int NOTIFICATION_ID = 1;
    private NotificationManager mNotificationManager;
    NotificationCompat.Builder builder;


    mNotificationManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);   
    PendingIntent contentIntent = PendingIntent.getActivity(this, 0,new Intent(this, MenuActivity.class), PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_ONE_SHOT);

    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this).setSmallIcon(R.drawable.icon).setContentTitle(" ").
                                            setStyle(new NotificationCompat.BigTextStyle().bigText(bundle.get("").toString())).
                                            setContentText(bundle.get("").toString());
    mBuilder.setContentIntent(contentIntent);

    mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());