Android下監聽Home鍵

來源:互聯網
上載者:User

標籤:android   style   blog   http   color   os   io   art   

 

網上看到demo,親測了以下機器和裝置,均測試通過:

測試通過的手機:1.華為榮耀3(Android 4.2.2)2.小米2s(Android 4.1.1)3.聯想的手機 (Android2.3.5)4.Google Nexus 5模擬器(Android 4.4.2)

 

核心代碼:

 

import android.content.BroadcastReceiver;import android.content.Context;import android.content.Intent;import android.content.IntentFilter;import android.util.Log;public class HomeWatcher {    static final String TAG = "wxl";    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 void stopWatch() {        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();                        } else if (reason                                .equals(SYSTEM_DIALOG_REASON_RECENT_APPS)) {                            // 長按home鍵                            mListener.onHomeLongPressed();                        }                    }                }            }        }    }}

 

 

調用方式:

 

public class MainActivity extends Activity {    private static final String TAG = "wxl";    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() {                Toast.makeText(MainActivity.this, "onHomePressed",                        Toast.LENGTH_LONG).show();            }            @Override            public void onHomeLongPressed() {                Toast.makeText(MainActivity.this, "onHomeLongPressed",                        Toast.LENGTH_LONG).show();            }        });        mHomeWatcher.startWatch();    }    /**     * 重新進入或開啟螢幕燈     */    protected void onRestart() {        Log.d(TAG, "onRestart");        mHomeWatcher.startWatch();        super.onRestart();    }    /**     * 按Home鍵或關閉螢幕燈     */    protected void onPause() {        super.onPause();        Log.d(TAG, "onPause");        mHomeWatcher.stopWatch();// 在onPause中停止監聽,不然會報錯的。    }}

 

demo:http://files.cnblogs.com/hsx514/ApkDemo.zip

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.