我有一个"警报"应用程序,可以在任何给定时间触发几种不同的警报类型.当闹钟响起时,它会添加状态栏通知.当用户使用状态栏中的"全部清除"按钮时,我希望删除意图从屏幕中删除并关闭"警报活动"窗口.我怎样才能做到这一点?因为我的警报活动不是单一任务活动,所以可以同时创建多个活动窗口,因此我不能仅使用一些含有onNewIntent()函数运行的数据的Intent并关闭Activity本身.我需要找到一种从Activity外部杀死警报窗口的方法.
谢谢你的帮助.
Hacky但100%工作解决方案:您可以发送Broadcast所有活动正在等待并调用finish()它们.
private String action = "clear";
private String type = "content://whatever_you_like"; //You should read stuff about this because it's a hack..
Run Code Online (Sandbox Code Playgroud)
onCreate每个活动:
clearStackManager = new ClearStackManager();
registerReceiver(clearStackManager,
IntentFilter.create(action, type));
Run Code Online (Sandbox Code Playgroud)
然后定义它:
private final class ClearStackManager extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
finish();
}
}
Run Code Online (Sandbox Code Playgroud)
的onDestroy:
unregisterReceiver(clearStackManager);
Run Code Online (Sandbox Code Playgroud)
打电话给:
public void clearStack() {
Intent intent = new Intent(action);
intent.setType(type);
sendBroadcast(intent);
}
Run Code Online (Sandbox Code Playgroud)
开箱即用的解决方案:使用FLAG CLEAR_TOP(删除除一个之外的所有活动)按意图调用堆栈的第一个活动(如果它始终相同),然后onNewIntent完成最后一个活动.
我不知道它是否有效解决方案:我也发现了这个:https://stackoverflow.com/a/6403577/327011但我从未使用过动作,所以我不确定如果多个活动有相同的动作将会发生什么.
更新:
您应该使用LocalBroadcastManager而不是"全局"广播来避免发送全局广播.
http://developer.android.com/reference/android/support/v4/content/LocalBroadcastManager.html
| 归档时间: |
|
| 查看次数: |
2315 次 |
| 最近记录: |