简体   繁体   English

从通知中打开活动

[英]Opening an activity from a notification

I have an activity called "A" with two buttons. 我有一个名为“ A”的活动,带有两个按钮。 Each button opens other new activities "B" and "C", the activity "A" is in onPause state. 每个按钮打开其他新活动“ B”和“ C”,活动“ A”处于onPause状态。 The activity "C" has a viewpager with 3 pages 活动“ C”有一个包含3页的viewpager

This application can receive notifications. 该应用程序可以接收通知。

Clicking on a notification received, open the activity "C". 单击收到的通知,打开活动“ C”。

This functions correctly in the following way: 这可以通过以下方式正常运行:

//...
    NotificationManager notificationManager =(NotificationManager) getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE); 
...
Intent intent =new Intent(getApplicationContext(), activityC.class);
        intent.putExtra("hello", notificationData);

This is my problem: 这是我的问题:

I want that when you click the notification received, if my application is showing the activity "A" or "B" or the application is in background, to close and open the activity "C" 我希望当您单击收到的通知时,如果我的应用程序显示活动“ A”或“ B”,或者该应用程序处于后台,请关闭并打开活动“ C”

If the application is showing the activity "C" leads to a page of the ViewPager ( pager.setCurrentItem (2); ) 如果应用程序显示活动“ C”,则指向页面的ViewPager( pager.setCurrentItem (2);

Several days I've been reading, I've tried several ways, using android:launchMode="singleTop" defining FLAG_ACTIVITY_NEW_TASK, FLAG_ACTIVITY_CLEAR_TOP and FLAG_ACTIVITY_SINGLE_TOP ...but always without success 我读了几天的书,尝试了几种方法,使用android:launchMode="singleTop"定义android:launchMode="singleTop" FLAG_ACTIVITY_NEW_TASK, FLAG_ACTIVITY_CLEAR_TOP and FLAG_ACTIVITY_SINGLE_TOP ...但始终没有成功

I'm going crazy with this ... 我为此而发疯...

Someone could help me? 有人可以帮我吗?

Thanks in advance! 提前致谢!

Rather than launching Activity from C, send a broadcast when notification is clicked which shall be received by your Activity A and then A shall launch your Activity C. 单击通知后,广播将发送给您的活动A,然后A将启动您的活动C,而不是从C启动活动。

You can try setting FLAG_ACTIVITY_SINGLE_TOP for activity C so that if C is already on top , it shall receive the intent. 您可以尝试为活动C设置FLAG_ACTIVITY_SINGLE_TOP,这样,如果C已经在最上面,它将收到该意图。

what you need to do is define where you were at the end. 您需要做的就是定义最后的位置。 here is how i would handle this: 这是我将如何处理:

Make a class and extend it from Application class 创建一个类并从Application类扩展它

define a variable here, it can be boolean, enum,int ,etc... when you enter any of those activities, change that parameter. 在这里定义一个变量,可以是boolean,enum,int等,当您输入任何这些活动时,请更改该参数。

in OnDestroy of each activity reset that particular variable to its default value. 在每个活动的OnDestroy中,将该特定变量重置为其默认值。

In your service before making the intent, check which state you were at last and make your intent based on that. 在进行意图之前,在服务中,请检查您最终处于哪个状态,并根据此意图进行意图。

public class MY_APPLCIATION extends Application {
    public static Boolean ActivityAIsRunning=false;
    public static Boolean ActivityBIsRunning=false;
}

override onResume and onDestroy of both activities : 覆盖两个活动的onResume和onDestroy:

public class ActivityA extends AppCompatActivity {
    // ......
    @Override
    protected void onDestroy() {
        super.onDestroy();
        MY_APPLCIATION.ActivityAIsRunning=false;
    }

    @Override
    protected void onResume() {
        super.onResume();
        MY_APPLCIATION.ActivityAIsRunning=true;
    }
}

ActivityB : 活动B:

public class ActivityB extends AppCompatActivity {
    // ......
    @Override
    protected void onDestroy() {
        super.onDestroy();
        MY_APPLCIATION.ActivityBIsRunning=false;
    }

    @Override
    protected void onResume() {
        super.onResume();
        MY_APPLCIATION.ActivityBIsRunning=true;
    }
}

now there is an important note which you should pay attention to : 现在有一个重要的注意事项,您应该注意:

You should define the intent when you are creating the notification ! 创建通知时,应定义意图!

you should have another activity ( let's called it MY_NOTIFICATION_ACTIVITY ). 您应该进行另一项活动(我们将其称为MY_NOTIFICATION_ACTIVITY)。 and the notification should always go to this activity. 并且通知应始终转到该活动。 now in that activity check which boolean is set and then call the desired activity (ActivityA or ActivityB or ActivityC). 现在在该活动中检查设置了哪个布尔值,然后调用所需的活动(ActivityA或ActivityB或ActivityC)。 Do not forget that in your manifest you should make ActivityB singleTop too !!! 不要忘记,在清单中也应该使ActivityB为singleTop !!!

*** ANOTHER SIMPLE METHOD ***另一种简单的方法

another simple method is that you update the notification in OnResume of each activity. 另一个简单的方法是您在OnResume中更新每个活动的通知。 by entering each activity you change the intent to that desired activity and make the same notification and update the last notification. 通过输入每个活动,您可以将意图更改为所需的活动,并发出相同的通知并更新上一个通知。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM