为通知的待定​​意图设置不同的活动

编程入门 行业动态 更新时间:2024-10-11 21:24:30
本文介绍了为通知的待定​​意图设置不同的活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

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.此Activity在onCreate()中执行以下操作:

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;

更多推荐

为通知的待定​​意图设置不同的活动

本文发布于:2023-11-27 11:25:39,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1637863.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:意图   待定   通知

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!