Android 长按home键强制关闭应用后,在哪里执行退出应用?

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 added
			if(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);
        }
    }

希望我的博客能对你有用,写博客的用意是自己做一个记录和自己的知识能在某个时候帮到大家。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值