活动结束后,通知消失

我有一个名为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()函数中的特定通知。


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


完全删除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());
链接地址: http://www.djcxy.com/p/84381.html

上一篇: Notification disappear after activity finish

下一篇: fluid on xs devices only