Android 监听Home

客制化Launcher ,很多时候会用到Home Key;Android 目前使用onKeyDown 不能监听到Home key

方法一:

public class HomeReceiver extends BootReceiver {

	static final String SYSTEM_REASON = "reason"; 
	static final String SYSTEM_HOME_KEY = "homekey";//home key 
	static final String SYSTEM_RECENT_APPS = "recentapps";//long home key 
	
	@Override
	public void onReceive(Context context, Intent intent) {
		// TODO Auto-generated method stub
		super.onReceive(context, intent);
		
		String action = intent.getAction(); 
		if (action.equals(Intent.ACTION_CLOSE_SYSTEM_DIALOGS)) { 
			
			
			String reason = intent.getStringExtra(SYSTEM_REASON); 
			if (reason != null) { 
				if (reason.equals(SYSTEM_HOME_KEY)) { //click  home key 
					//HandlerMessage.homePress(mHandler2);
				
				} else if (reason.equals(SYSTEM_RECENT_APPS)) { <span style="font-family: Arial, Helvetica, sans-serif;">//long click  home key </span>
				
				} 
			} 
		} 
	
		
	}

	
}
在AndroidManifest.xml 文件中注册:

<receiver android:name="com.org.HomeReceiver" >
            <intent-filter>
                <action android:name="android.intent.action.CLOSE_SYSTEM_DIALOGS" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </receiver>
方法二:

private boolean isTesting = true;

	class CatchLogThread extends Thread {
		@Override
		public void run() {
			Process mLogcatProc = null;
			BufferedReader reader = null;
			String line;
			while (isTesting) {
				try {
					mLogcatProc = Runtime.getRuntime().exec(new String[] { "logcat", "ActivityManager:I *:S" });
					reader = new BufferedReader(new InputStreamReader(mLogcatProc.getInputStream()));
					while ((line = reader.readLine()) != null) {
						if(line.contains("android.intent.category.HOME")){
<span style="white-space:pre">							</span>//TO DO
							Runtime.getRuntime().exec("logcat -c");
						}
						/*if (line.indexOf("android.intent.category.HOME") > 0) {
							//isTesting = false;
							mHandler.sendMessage(mHandler.obtainMessage());
							Runtime.getRuntime().exec("logcat -c");//删除日志
							break;
						}*/
					}
					Thread.sleep(10);
				} catch (Exception e) {
					e.printStackTrace();
				}
				
				
			}
		}
	};

需要在AndroidManifest.xml获取权限

<uses-permission android:name="android.permission.READ_LOGS" />




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值