Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android利用廣播監聽按下HOME和電源鍵

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

編輯:關於Android編程

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;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		init();
	}
	
	private void init() {
		mHomeKeyObserver = new HomeKeyObserver(this);
		mHomeKeyObserver.setHomeKeyListener(new OnHomeKeyListener() {
			@Override
			public void onHomeKeyPressed() {
				System.out.println("----> 按下Home鍵");
			}

			@Override
			public void onHomeKeyLongPressed() {
				System.out.println("----> 長按Home鍵");
			}
		});
		mHomeKeyObserver.startListen();
 
		//////////////////////////////////////////
		
		mPowerKeyObserver = new PowerKeyObserver(this);
		mPowerKeyObserver.setHomeKeyListener(new OnPowerKeyListener() {
			@Override
			public void onPowerKeyPressed() {
			   System.out.println("----> 按下電源鍵");
			}
		});
		mPowerKeyObserver.startListen();
	}

	@Override
	protected 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";
		@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 && 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{
		@Override
		public void onReceive(Context context, Intent intent) {
			String action = intent.getAction();
			if (action.equals(Intent.ACTION_SCREEN_OFF)) {
				mOnPowerKeyListener.onPowerKeyPressed();
			}
		}
	}
     
}


main.xml如下:



    



  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved