我有一个名为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,但没有运气。
答案 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());