删除或从状态栏通知意向关闭自己的活动窗口 [英] Remove or close your own Activity window from a Status Bar Notification Intent

查看:87
本文介绍了删除或从状态栏通知意向关闭自己的活动窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个报警应用与可能在任何给定的时间被触发几种不同类型的报警。当警报响起时,加上一个状态栏通知。当用户使用全部清除按钮,在状态栏,我想删除意图删除并从屏幕关闭报警活动窗口。我怎样才能做到这一点?因为我的闹钟活动不是一项单一的活动,多活动窗口可以一次创建,所以我不能只用一个Intent了一些数据,该onNewIntent()函数将运行并关闭该活动本身。我需要找到一种方法,从活动之外杀死报警窗口。

I have an "alarm" app with several different alarm types that may be triggered at any given time. When the alarm goes off it adds a Status Bar Notification. When the user uses the "Clear All" button in the Status Bar, I want the Delete Intent to remove and close the Alarm Activity window from the screen. How can I achieve this? Because my Alarm Activity is NOT a Single Task activity, multiple activity windows can be created at once so I cannot just use an Intent with some data that the onNewIntent() function will run and close the Activity itself. I need to find a way to kill the alarm window from outside of the Activity.

感谢您的帮助。

推荐答案

哈克,但100%的工作液:您可以发送广播的所有的活动都在等待并呼吁它们完成()

Hacky but 100% working solution: You can send a Broadcast that all activities are waiting for and that calls finish() on them.

private String action = "clear";
private String type = "content://whatever_you_like"; //You should read stuff about this because it's a hack..

的onCreate每个活动的:

onCreate of each Activity:

  clearStackManager = new ClearStackManager();
            registerReceiver(clearStackManager,
                    IntentFilter.create(action, type));

然后定义它:

Then define it:

 private final class ClearStackManager extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        finish();
    }
}

的onDestroy:

onDestroy:

unregisterReceiver(clearStackManager);

调用它:

 public void clearStack() {
    Intent intent = new Intent(action);
    intent.setType(type);
    sendBroadcast(intent);
}


开箱即用解决方案:拨打由意向协议栈的第一个活动(如果它总是相同的)与FLAG CLEAR_TOP(去除除了一个所有活动),然后在onNewIntent完成最后之一。


Out of the box solution: Call by intent the first activity of the stack (if it's always the same) with FLAG CLEAR_TOP (removing all activities except that one) and then on onNewIntent finish the last one.

我不知道,如果它的工作原理解决方案:我也发现了这一点: http://stackoverflow.com/ A /三十二万七千零一十一分之六百四十〇万三千五百七十七,但我从未使用过的动作,所以我不知道会发生什么,如果多个活动具有相同的作用。

I dunno if it works solution: I also found this: http://stackoverflow.com/a/6403577/327011 but i never worked with actions, so i'm not sure what will happen if multiple activities have the same action.

更新

您应该使用LocalBroadcastManager而不是全球广播,以避免发出全球广播。

You should use LocalBroadcastManager instead of "global" Broadcasts to avoid sending global broadcasts.

http://developer.android.com/reference/android/support/v4/content/LocalBroadcastManager.html

这篇关于删除或从状态栏通知意向关闭自己的活动窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆