从通知栏打开时的两个活动实例

时间:2012-07-25 02:17:01

标签: android android-activity notifications

1.从通知栏开始 2.按回家返回目的地 3.从app图标打开

问题:2个SampleTabsDefault实例,需要退出两次。

    Intent intent = new Intent(_context, SampleTabsDefault.class);
    intent.setFlags(/*Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP |*/ Intent.FLAG_ACTIVITY_NEW_TASK);
    PendingIntent activity = PendingIntent.getActivity(_context, 0, intent, 0);
    notification.contentIntent = activity;

2 个答案:

答案 0 :(得分:3)

使用以下方法传递通知构建器的内容意图

private  PendingIntent getEmptyPendingIntent(Context context) {
    Intent resultIntent = new Intent(context, InitializationActivity.class);
    resultIntent.setAction(Long.toString(System.currentTimeMillis())); //adding unique identification for each intent
    resultIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
    resultIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
    stackBuilder.addParentStack(InitializationActivity.class);
    stackBuilder.addNextIntent(resultIntent);
    stackBuilder.getIntentCount();
    PendingIntent resultPendingIntent =
            stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);

    return resultPendingIntent;
}

使用以下方法发送通知

private void sendNotification(String title, String message) {
    Intent intent = new Intent(this, InitializationActivity.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    String desc = "";
    if (!TextUtils.isEmpty(message)) {
        try {
            JSONObject jsonObject = new JSONObject(message);
            desc = jsonObject.optString("payload");
                        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 , intent,
            PendingIntent.FLAG_ONE_SHOT);
    Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
            .setSmallIcon(R.mipmap.ic_launcher)getResources().getDrawable(R.drawable.notify_large)).getBitmap())
            .setContentTitle(title)
            .setContentText(desc)
            .setAutoCancel(true)
            .setSound(defaultSoundUri)
            .setContentIntent(getEmptyPendingIntent(this));

    NotificationManager notificationManager =
            (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

    notificationManager.notify(0, notificationBuilder.build());
}

答案 1 :(得分:2)

在活动点头的AndroidManifest.xml文件中尝试以下代码。

<activity name="SampleTabsDefault"
android:clearTaskOnLaunch="true"
android:finishOnTaskLaunch="true"
>
.....
</activity>

请阅读两个属性说明here

希望这能解决您的问题