Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.9k views
in Technique[技术] by (71.8m points)

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

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...