[英]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.