Android 监听Home

来源:互联网 发布:dijkstra算法图解 编辑:程序博客网 时间:2024/06/06 08:39

客制化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 @Overridepublic void onReceive(Context context, Intent intent) {// TODO Auto-generated method stubsuper.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 {@Overridepublic 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 DORuntime.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" />




0 0
原创粉丝点击