Android 长按home键强制关闭应用后,在哪里执行退出应用?
16lz
2021-01-23
1、问题描述:在做一个应用的时候发送了一个广播,原本是打算在onDestory()方法中通过
if(mNotificationManager != null ) mNotificationManager.cancel(NOTIFICATION_ID);
取消该通知的,但是我按home键返回桌面后,再长按home键强行关闭应用,该应用的通知没有被销毁。打log发现,android强制关闭应用的时候有的时候走onDestory()方法,有的时候不走,个中原因我也不是很清楚。但可以知道的一点就是在onDestory()方法中取消notification是无效的。
2、解决方案:由于我的东西本身就是要在后台运行的,强制关闭应用的时候后台已经停止运行了,这不是我想要的结果。然后就得找到滑动强制关闭应用的地方在哪里,然后不关闭应用就解决了。
3、path:frameworks\base\packages\SystemUI\src\com\android\systemui\recent\RecentsPanelView.java
有added表示的部分就是我加上的,当强制关闭的是收音机则什么都不做,返回。
在强制关闭的时候会走这里,你可以写你想要做的动作代码。
public void handleSwipe(View view) { TaskDescription ad = ((ViewHolder) view.getTag()).taskDescription; if (ad == null) { Log.v(TAG, "Not able to find activity description for swiped task; view=" + view + " tag=" + view.getTag()); return; } if (DEBUG) Log.v(TAG, "Jettison " + ad.getLabel()); mRecentTaskDescriptions.remove(ad); mRecentTasksLoader.remove(ad); // Handled by widget containers to enable LayoutTransitions properly // mListAdapter.notifyDataSetChanged(); if (mRecentTaskDescriptions.size() == 0) { dismissAndGoBack(); } // Currently, either direction means the same thing, so ignore direction and remove // the task. final ActivityManager am = (ActivityManager) mContext.getSystemService(Context.ACTIVITY_SERVICE); if (am != null) { if(TextUtils.equals(ad.packageName, "com.broadcom.torch")){ Intent i = new Intent("android.intent.TorchReceiver"); mContext.sendBroadcast(i); }//20150206 addedif(TextUtils.equals(ad.packageName, "com.android.soundrecorder")){ return; }//added end am.removeTask(ad.persistentTaskId, ActivityManager.REMOVE_TASK_KILL_PROCESS); // Accessibility feedback setContentDescription( mContext.getString(R.string.accessibility_recents_item_dismissed, ad.getLabel())); sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED); setContentDescription(null); } }
希望我的博客能对你有用,写博客的用意是自己做一个记录和自己的知识能在某个时候帮到大家。 更多相关文章
- android系统裁剪方法
- DIY osc android 客户端 之 方法论
- Android 通知栏Notification的整合 全面学习
- Android webview和js互相调用实现方法
- 深入解析android log的分析方法(1)
- Android增量升级的方法和原理
- android ListView常见问题解决方法(滚动背景变黑,去除滑动时阴影,拖