簡體   English   中英

"如何防止通知清除后台堆棧中的所有活動"

[英]How to prevent Notification from clearing all activities in backstack

我正在開發一個帶有 GCM 推送通知的 android 應用程序,其中每當通知打開頁面時,它都會清除后台堆棧中的所有活動。

Intent intent = new Intent(this, MainActivity.class);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
stackBuilder.addParentStack(MainActivity.class);
stackBuilder.addNextIntent(intent);
long[] vibrate = {0, 100, 200, 300};
Uri notification = RingtoneManager
        .getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
PendingIntent contentIntent = stackBuilder.getPendingIntent(0,
        PendingIntent.FLAG_UPDATE_CURRENT
                | PendingIntent.FLAG_ONE_SHOT);
Bitmap largIcon = BitmapFactory.decodeResource(this.getResources(),
        R.mipmap.ic_launcher);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
        this).setSmallIcon(R.mipmap.small_icon_notification)
        .setContentTitle(getResources().getString(R.string.app_name))
        .setLargeIcon(largIcon)
        .setSound(notification)
        .setStyle(new NotificationCompat.BigTextStyle().bigText(msg))
        .setContentText(msg).      setSound(notification)
        .setStyle((new NotificationCompat.BigTextStyle()).bigText(msg)) .setAutoCancel(true);
mBuilder.setVibrate(vibrate);
mBuilder.setContentIntent(contentIntent);
mNotificationManager.notify((int) System.currentTimeMillis(), mBuilder.build());

為防止Notification清除后退堆棧中的所有活動,必須設置特殊活動PendingIntent。 特殊的活動不需要后退棧。 使用清單來設置活動任務選項,並通過調用getActivity()創建PendingIntent。

PendingIntent指定將來要執行的操作。 它使您可以將將來的Intent傳遞給另一個應用程序,並允許該應用程序執行該Intent,就像它具有與您的應用程序相同的權限一樣,而在最終調用該Intent時您的應用程序是否仍然存在

通過將PendingIntent賦予另一個應用程序,即授予它執行指定的操作的權限,就好像另一個應用程序是您自己一樣(具有相同的權限和身份)。

以下代碼片段演示了該過程:

// Instantiate a Builder object.
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);

// Creates an Intent for the Activity
Intent notifyIntent =
    new Intent(new ComponentName(this, ResultActivity.class));

// Sets the Activity to start in a new, empty task
notifyIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | 
    Intent.FLAG_ACTIVITY_CLEAR_TASK);

// Creates the PendingIntent
PendingIntent notifyIntent = PendingIntent.getActivity(this,0,notifyIntent,PendingIntent.FLAG_UPDATE_CURRENT);

// Puts the PendingIntent into the notification builder
builder.setContentIntent(notifyIntent);

// Notifications are issued by sending them to the
// NotificationManager system service.
NotificationManager mNotificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

// Builds an anonymous Notification object from the builder, and
// passes it to the NotificationManager
mNotificationManager.notify(id, builder.build());

這是一個有用的鏈接,如何設置特殊活動的待定意圖: http : //developer.android.com/training/notify-user/navigation.html

PendingIntent contentIntent = stackBuilder.getPendingIntent(0,
    PendingIntent.FLAG_UPDATE_CURRENT
            | PendingIntent.FLAG_ONE_SHOT);

PendingIntent contentIntent = stackBuilder.getPendingIntent(99,
    PendingIntent.FLAG_UPDATE_CURRENT
            | PendingIntent.FLAG_ONE_SHOT);

只需更改 requestCode 即可。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM