如何通过单击通知来关闭我的应用程序的任何活动? [英] 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);
在应用程序的所有"startActivity"中,我应用了下一个标志:
In all "startActivity" of the app I applied the next flag:
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
我的startActivity通知执行以下操作: 调用活动"Splash",并称为"Main".
The startActivity my notification does the following: Call activity "Splash" and is called "Main".
Casulidad如果我处于主"脉冲通知中,则将其关闭(正常工作). 但是,如果我处于新闻"活动中并发出通知,则我有2个活动处于打开状态,即新的主要"活动和前一个新闻".
Casulidad If I was in "Main" pulse notification, closes the current (working properly). But if I am in the activity "News" and pulse the notification, I have 2 activities open, the new "Main" and the former "News".
如何通过单击通知来关闭我的应用程序的任何活动?
How to close any activity of my application by clicking on a notification?
推荐答案
您可以在通知中设置PendingIntent,该通知将被Activity在广播接收器中捕获.然后在活动的broadcastReceiver中,调用finish();
这应该会关闭您的活动.
You could set a PendingIntent in the notification that would be caught by the Activity in a broadcastReceiver. Then in the broadcastReceiver of the activity, call finish();
This should close your activity.
即 把它放在你的活动中
i.e. Put this in your activity
BroadcastReceiver mReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
finish();
}
};
这在您的onCreate()
IntentFilter filter = new IntentFilter("android.intent.CLOSE_ACTIVITY");
registerReceiver(mReceiver, filter);
然后,您在通知中的PendingIntent应该具有"android.intent.CLOSE_ACTIVITY"
的作用,并且为了安全起见,您的活动包装必须是包装.
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.
这是通过
Intent intent = new Intent("android.intent.CLOSE_ACTIVITY");
PendingIntent pIntent = PendingIntent.getBroadcast(context, 0 , intent, 0);
然后在使用Notification.Builder构建通知时,使用setContentIntent(pIntent)
将其添加到您的通知中.
Then add it to your notification by using the setContentIntent(pIntent)
when building the notification with the Notification.Builder.
这篇关于如何通过单击通知来关闭我的应用程序的任何活动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!