为通知的待定​​意图设置不同的活动 [英] Set different activities for pending intent of notification

查看:80
本文介绍了为通知的待定​​意图设置不同的活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前面临的问题是为两个不同的活动设置待处理操作以进行通知.

I am currently facing the problem of setting pending action for two different activities to notification.

我有一个ParentActivity和一个ChildActivity.我想在通知单击(如果当前正在运行或暂停)时打开ChildActivity,否则请启动ParentActivity.

I have a ParentActivity and a ChildActivity. I want open ChildActivity on notification click if currently it is running or paused, otherwise start ParentActivity.

我尝试过这个:

 .........

    Intent resultIntent = new Intent(this, ChildActivity.class);
    TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
    stackBuilder.addParentStack(ParentActivity.class);
    stackBuilder.addNextIntent(resultIntent);
    PendingIntent resultPendingIntent =
            stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
    .............

上面没有为我工作.每次ChildActivity从通知点击开始.

Above is not working for me. Everytime ChildActivity is starting on notification click.

也正如法鲁克回答的那样,我不要这个.通过检查ChildActivity的当前状态来创建通知的暂挂意图将不起作用. 假设在运行ChildActivity时创建了通知,但在创建通知后,用户终止了该应用程序.因此,在终止该应用程序之后,如果用户单击通知,则ChildActivity将启动.我不要我想如果ChildActivity没有运行或暂停,则应该启动ParentActivity.

And also as Faruk answered, I dont want this. Creating a notification's pending intent by checking ChildActivity's current state will not work. Suppose notification created when ChildActivity was running but after creating the notification, user killed the app. So after killing the app, If user will click on notification then ChildActivity will start. I don't want that. I want if ChildActivity is not running or paused then ParentActivity should be started.

我该如何实现? 请帮忙.

How can I achieve this? Please help.

推荐答案

Notification启动简单的调度Activity.此ActivityonCreate()中执行以下操作:

Have your Notification launch a simple dispatch Activity. This Activity does the following in onCreate():

super.onCreate(...);
if (ChildActivity.running) {
    // ChildActivity is running, so redirect to it
    Intent childIntent = new Intent(this, ChildActivity.class);
    // Add necessary flags, maybe FLAG_ACTIVITY_CLEAR_TOP, it depends what the rest of your app looks like
    childIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
    startActivity(childIntent);
} else {
    // Child is not running, so redirect to parent
    Intent parentIntent = new Intent(this, ParentIntent.class);
    // Add necessary flags, maybe FLAG_ACTIVITY_CLEAR_TOP, it depends what the rest of your app looks like
    parentIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
    startActivity(parentIntent);
}
finish();

ChildActivity中执行以下操作:

public static boolean running; // Set when this Activity is active

ChildActivity.onCreate()中添加此内容:

running = true;

ChildActivity.onDestroy()中添加此内容:

running = false;

这篇关于为通知的待定​​意图设置不同的活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆