Home点击监听及示例

Posted 离氺的鱼

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Home点击监听及示例相关的知识,希望对你有一定的参考价值。

/** 
 * Home键监听封装 
 */
 public class HomeWatcher  

    static final String TAG = "HomeWatcher";  
    private Context mContext;  
    private IntentFilter mFilter;  
    private OnHomePressedListener mListener;  
    private InnerRecevier mRecevier;  

        // 回调接口  
        public interface OnHomePressedListener  
        public void onHomePressed();   //点击
        public void onHomeLongPressed();  //长按
      

    public HomeWatcher(Context context)  
        mContext = context;  
        mFilter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
      

    /** 
     * 设置监听  
     * @param listener 
     * /
    public void setOnHomePressedListener(OnHomePressedListener listener)  
        mListener = listener;  
        mRecevier = new InnerRecevier();  
      

    /** 
     * 开始监听,注册广播 
     */
     public void startWatch()  
        if (mRecevier != null)   
            mContext.registerReceiver(mRecevier, mFilter);  
          
      

    /** 
     * 停止监听,注销广播 
     */
     public voids topWatch()  
        if (mRecevier != null)   
            mContext.unregisterReceiver(mRecevier);  
          
      

     class InnerRecevier extends BroadcastReceiver  
        final String SYSTEM_DIALOG_REASON_KEY = "reason";  
        final String SYSTEM_DIALOG_REASON_GLOBAL_ACTIONS = "globalactions";  
        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))   
                            // 短按home键  
                            mListener.onHomePressed();  
                         elseif (reason  
                                .equals(SYSTEM_DIALOG_REASON_RECENT_APPS))   
                            // 长按home键  
                            mListener.onHomeLongPressed();  
                          
                      
                  
              
          
      
使用方式:
public class MainActivity extends Activity  
    private static final String TAG = "MainActivity";  
    private HomeWatcher mHomeWatcher;  

    @Override
    protected void onCreate(Bundle savedInstanceState)  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.activity_main);  

        mHomeWatcher = new HomeWatcher(this);  
        mHomeWatcher.setOnHomePressedListener(new OnHomePressedListener()   
            @Override
            public void onHomePressed()  
                Log.e(TAG, "onHomePressed");  //这个点击不一定每次都有,有的时候有,有的时候没有
              

            @Override
             public void onHomeLongPressed()  
                Log.e(TAG, "onHomeLongPressed");  
              
        );  
        mHomeWatcher.startWatch();  
      

    @Override
        protected void onPause()  
        super.onPause();  
        mHomeWatcher.stopWatch();// 在onPause中停止监听,不然会报错的。  
      

以上是关于Home点击监听及示例的主要内容,如果未能解决你的问题,请参考以下文章

Android TV开发中所有的遥控器按键监听及注意事项,新增home键监听

vue路由监听及路由守卫

iOS 监听Home键及锁屏键

Action Bar 的 Home 按钮的 onClick 监听器

springboot集成kafka详细步骤(发送及监听消息示例)

求助:android两个及以上控件点击事件同时监听响应如何实现