Android利用廣播監聽按下HOME和電源鍵

來源:互聯網
上載者:User

MainActivity如下:

package cc.testhome;import cc.testhome.HomeKeyObserver.OnHomeKeyListener;import cc.testhome.PowerKeyObserver.OnPowerKeyListener;import android.os.Bundle;import android.app.Activity;/** * Demo描述: * 利用廣播監聽Home鍵的按下和長按Home鍵 * 利用廣播監聽電源鍵的按下(關閉螢幕) *  * 參考資料: * 1 http://blog.csdn.net/q445697127/article/details/8432513 * 2 http://blog.csdn.net/watt520/article/details/18959897 * 3 http://blog.csdn.net/lfdfhl/article/details/9903693 *   Thank you very much */public class MainActivity extends Activity {private HomeKeyObserver mHomeKeyObserver;private PowerKeyObserver mPowerKeyObserver;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);init();}private void init() {mHomeKeyObserver = new HomeKeyObserver(this);mHomeKeyObserver.setHomeKeyListener(new OnHomeKeyListener() {@Overridepublic void onHomeKeyPressed() {System.out.println("----> 按下Home鍵");}@Overridepublic void onHomeKeyLongPressed() {System.out.println("----> 長按Home鍵");}});mHomeKeyObserver.startListen(); //////////////////////////////////////////mPowerKeyObserver = new PowerKeyObserver(this);mPowerKeyObserver.setHomeKeyListener(new OnPowerKeyListener() {@Overridepublic void onPowerKeyPressed() {   System.out.println("----> 按下電源鍵");}});mPowerKeyObserver.startListen();}@Overrideprotected void onDestroy() {super.onDestroy();mHomeKeyObserver.stopListen(); //////////////////////////////////////////mPowerKeyObserver.stopListen();}}


HomeKeyObserver如下:

package cc.testhome;import android.content.BroadcastReceiver;import android.content.Context;import android.content.Intent;import android.content.IntentFilter;public class HomeKeyObserver {    private Context mContext;    private IntentFilter mIntentFilter;    private OnHomeKeyListener mOnHomeKeyListener;    private HomeKeyBroadcastReceiver mHomeKeyBroadcastReceiver;public HomeKeyObserver(Context context) {this.mContext = context;}//註冊廣播接收者public void startListen(){mIntentFilter=new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);mHomeKeyBroadcastReceiver=new HomeKeyBroadcastReceiver();    mContext.registerReceiver(mHomeKeyBroadcastReceiver, mIntentFilter);System.out.println("----> 開始監聽");}//取消廣播接收者public void stopListen(){if (mHomeKeyBroadcastReceiver!=null) {mContext.unregisterReceiver(mHomeKeyBroadcastReceiver);System.out.println("----> 停止監聽");}}// 對外暴露介面public void setHomeKeyListener(OnHomeKeyListener homeKeyListener) {mOnHomeKeyListener = homeKeyListener;}// 回調介面public interface OnHomeKeyListener {public void onHomeKeyPressed();public void onHomeKeyLongPressed();}//廣播接收者class HomeKeyBroadcastReceiver extends BroadcastReceiver{final String SYSTEM_DIALOG_REASON_KEY = "reason";//按下Home鍵final String SYSTEM_DIALOG_REASON_HOME_KEY = "homekey";//長按Home鍵final String SYSTEM_DIALOG_REASON_RECENT_APPS = "recentapps";@Overridepublic 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 && mOnHomeKeyListener != null) {if (reason.equals(SYSTEM_DIALOG_REASON_HOME_KEY)) {mOnHomeKeyListener.onHomeKeyPressed();} else if (reason.equals(SYSTEM_DIALOG_REASON_RECENT_APPS)) {mOnHomeKeyListener.onHomeKeyLongPressed();}}}}}     }

PowerKeyObserver如下:

package cc.testhome;import android.content.BroadcastReceiver;import android.content.Context;import android.content.Intent;import android.content.IntentFilter;public class PowerKeyObserver {    private Context mContext;    private IntentFilter mIntentFilter;    private OnPowerKeyListener mOnPowerKeyListener;    private PowerKeyBroadcastReceiver mPowerKeyBroadcastReceiver;public PowerKeyObserver(Context context) {this.mContext = context;}//註冊廣播接收者public void startListen(){mIntentFilter=new IntentFilter(Intent.ACTION_SCREEN_OFF);mPowerKeyBroadcastReceiver=new PowerKeyBroadcastReceiver();    mContext.registerReceiver(mPowerKeyBroadcastReceiver, mIntentFilter);System.out.println("----> 開始監聽");}//取消廣播接收者public void stopListen(){if (mPowerKeyBroadcastReceiver!=null) {mContext.unregisterReceiver(mPowerKeyBroadcastReceiver);System.out.println("----> 停止監聽");}}// 對外暴露介面public void setHomeKeyListener(OnPowerKeyListener powerKeyListener) {mOnPowerKeyListener = powerKeyListener;}// 回調介面public interface OnPowerKeyListener {public void onPowerKeyPressed();}//廣播接收者class PowerKeyBroadcastReceiver extends BroadcastReceiver{@Overridepublic void onReceive(Context context, Intent intent) {String action = intent.getAction();if (action.equals(Intent.ACTION_SCREEN_OFF)) {mOnPowerKeyListener.onPowerKeyPressed();}}}     }


main.xml如下:

    



聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.