我正在构建一个应用程序,在某些事件上启动服务.该服务在每个应用程序的顶部都有一个叠加视图(如Facebook聊天头).我想停止服务并在点击导航软键按钮(后面,主页和最近的应用程序)时删除叠加视图.
我通过扩展RelativeLayout成功地制作了一个覆盖视图.我正在关注此视图以进行叠加视图.
Facebook正在为扩展和关闭聊天头做同样的事情.提前致谢.:)
编辑
我的问题与此处所述相同.
我遇到了和你一样的问题.因此,经过几天的研究,我发现下面的类检测(按下home和recentapps键的事件).
public class HardwareKeyWatcher { static final String TAG = "HardwareKeyWatcher"; private Context mContext; private IntentFilter mFilter; private OnHardwareKeysPressedListener mListener; private InnerReceiver mReceiver; public interface OnHardwareKeysPressedListener { public void onHomePressed(); public void onRecentAppsPressed(); } public HardwareKeyWatcher(Context context) { mContext = context; mFilter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS); mFilter.setPriority(1000); } public void setOnHardwareKeysPressedListenerListener(OnHardwareKeysPressedListener listener) { mListener = listener; mReceiver = new InnerReceiver(); } public void startWatch() { if (mReceiver != null) { mContext.registerReceiver(mReceiver, mFilter); } } public void stopWatch() { if (mReceiver != null) { mContext.unregisterReceiver(mReceiver); } } class InnerReceiver extends BroadcastReceiver { final String SYSTEM_DIALOG_REASON_KEY = "reason"; final String SYSTEM_DIALOG_REASON_RECENT_APPS = "recentapps"; final String SYSTEM_DIALOG_REASON_HOME_KEY = "homekey"; @Override public void onReceive(Context context, Intent intent) { String action = intent.getAction(); if (action.equals(Intent.ACTION_CLOSE_SYSTEM_DIALOGS)) { String reason = intent.getStringExtra(SYSTEM_DIALOG_REASON_KEY); if (reason != null) { Log.e(TAG, "action:" + action + ",reason:" + reason); if (mListener != null) { if (reason.equals(SYSTEM_DIALOG_REASON_HOME_KEY)) { mListener.onHomePressed(); } else if (reason.equals(SYSTEM_DIALOG_REASON_RECENT_APPS)) { mListener.onRecentAppsPressed(); } } } } } } }
在服务中的onCreate方法中使用以下类,如下所示.
@Override public void onCreate() { mHardwareKeyWatcher = new HardwareKeyWatcher(this); mHardwareKeyWatcher.setOnHardwareKeysPressedListenerListener(new HardwareKeyWatcher.OnHardwareKeysPressedListener() { @Override public void onHomePressed() { System.out.println("stop your service here"); } @Override public void onRecentAppsPressed() { System.out.println("stop your service here"); } }); mHardwareKeyWatcher.startWatch(); }
并且为了检测背压,您可以点击此链接(服务与覆盖 - 按下按钮按下).我测试了这个解决方案,它对我有用.
因此,您可以在onHomePressed()和onRecentAppsPressed()方法中调用stopService,然后覆盖服务类中的onDestroy()方法,如下所示.
@Override public void onDestroy() { super.onDestroy(); mWindowManager.removeView(mYourOverlayView); mHardwareKeyWatcher.stopWatch(); }
请尝试这些事情让我们知道......祝你好运.