Remove or close your own Activity window from a Status Bar Notification Intent

蓝咒 提交于 2019-12-17 16:52:50

问题


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.

Thanks for your help.


回答1:


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 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:

unregisterReceiver(clearStackManager);

Calling it:

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

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.


I dunno if it works solution: I also found this: https://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.


UPDATE:

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

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




回答2:


Is this a single application with multiple activities, each of which is an alarm window?

If so, then how about registering the id of each activity in a singleton object that is shared by all activities of the application? The (first) activity that receives the delete intent from the status bar locks the singleton object, iterates over the collection of activities, sends each activity (except itself (activityId == this)) a delete intent and then exits itself. The other activities determine that the shared object is already locked and simply terminate themselves.

Use a synchronized method on the shared object to lock the object that returns true if the shared object was locked as a result of calling the lock() function, and false if the shared object was already locked by another activity. The calling activity can then determine whether it should simply die (result was false - already locked) or whether it needs to take the responsibility for telling the other activities to die (result was true - first time locked).

This way, the application is able to programmatically direct delete intents to its activities without relying on a broadcast mechanism that might take out innocent bystanders, or might miss some activities. The activity that receives the original delete intent can verify that the other activities have died prior to its demise to increase robustness.



来源:https://stackoverflow.com/questions/7486865/remove-or-close-your-own-activity-window-from-a-status-bar-notification-intent

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!