简体   繁体   English

如何通过单击通知来关闭我的应用程序的任何活动?

[英]How to close any activity of my application by clicking on a notification?

When I click on a notification apply the following: 当我单击通知时,请执行以下操作:

intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);

In all "startActivity" of the app I applied the next flag: 在应用程序的所有“ startActivity”中,我应用了下一个标志:

intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);

The startActivity my notification does the following: Call activity "Splash" and is called "Main". 我的startActivity通知执行以下操作:将活动称为“ Splash”,并称为“ Main”。

Casulidad If I was in "Main" pulse notification, closes the current (working properly). Casulidad如果我处于“主”脉冲通知中,则关闭电流(正常工作)。 But if I am in the activity "News" and pulse the notification, I have 2 activities open, the new "Main" and the former "News". 但是,如果我处于“新闻”活动中并发出通知,则我有2个活动处于打开状态,即新的“主要”和前一个“新闻”。

How to close any activity of my application by clicking on a notification? 如何通过单击通知来关闭我的应用程序的任何活动?

You could set a PendingIntent in the notification that would be caught by the Activity in a broadcastReceiver. 您可以在Activity在广播接收器中捕获的通知中设置PendingIntent。 Then in the broadcastReceiver of the activity, call finish(); 然后在活动的broadcastReceiver中,调用finish(); This should close your activity. 这应该关闭您的活动。

ie Put this in your activity 即把它放在你的活动中

BroadcastReceiver mReceiver = new BroadcastReceiver() {

    @Override
    public void onReceive(Context context, Intent intent) {
        finish();
    }

};

This in your onCreate() 这在您的onCreate()

IntentFilter filter = new IntentFilter("android.intent.CLOSE_ACTIVITY");
registerReceiver(mReceiver, filter);

And then your PendingIntent in your notification should have action of "android.intent.CLOSE_ACTIVITY" and for safety a package of your activity's package. 然后,您在通知中的PendingIntent应该具有"android.intent.CLOSE_ACTIVITY"作用,并且为了安全起见,您的活动包装中应包含包装。

This is done by 这是通过

Intent intent = new Intent("android.intent.CLOSE_ACTIVITY");
PendingIntent pIntent = PendingIntent.getBroadcast(context, 0 , intent, 0);

Then add it to your notification by using the setContentIntent(pIntent) when building the notification with the Notification.Builder. 然后,在使用Notification.Builder构建通知时,通过使用setContentIntent(pIntent)将其添加到通知中。

I used a singleton with a static flag exitApp that was false at start of the application and it is set to true in the activity that returns from the notification. 我使用了带有静态标志exitApp的单例,该静态标志在应用程序启动时为false,并且在从通知返回的活动中将其设置为true。

This flag is checked in each of onResume() of all activities and if it is true the activity calls it's finish() method.. 在所有活动的每个onResume()中都会检查此标志,如果为true,则该活动将调用它的finish()方法。

This way, even if the notification is activated some few activities down the road, each activity finish() and the onResume() of the parent activity is called which in turns also finish() until the mainActivity finish() and the application is terminated. 这样,即使通知被激活了,接下来的一些活动,每个活动的finish()和父活动的onResume()都被调用,这反过来也可以完成(),直到mainActivity finish()和应用程序终止。

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

相关问题 单击通知时如何关闭我的应用程序? - How to close my application when clicking on a notification? 如何关闭活动并通过单击通知再次显示相同的活动 - how to close the activity and again show the same activity again by clicking the notification 单击通知后启动应用程序的活动 - Launching an activity of the application on clicking the notification 关闭活动,然后在单击通知时将其打开 - close activity before opening it on clicking notification 如何从我的Android应用程序中的任何活动使用“onBackButtonpressed”关闭应用程序 - how can i close an application using “onBackButtonpressed” from any activity in my android application 单击后如何关闭通知 - How to close notification after clicking 如何通过点击通知去活动? - How to go to an activity by clicking on notification? 通过单击我的应用程序中的通知来启动应用程序/活动 - Start App/Activity by clicking Notification in my App 单击下载通知启动我的活动 - Launching my activity on clicking on download notification 当我的应用程序运行时,如何在不单击通知的情况下使用GCM通知内容更新当前活动? - While my app is running, how can I update the current activity with the contents a GCM notification, without clicking on the notification?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM