<activity android:name="com.woyou.activity.HomeActivity" android:launchMode="singleInstance" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.DEFAULT" /> <category android:name="android.intent.category.HOME" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity>
private BroadcastReceiver mHomeKeyEventReceiver = new BroadcastReceiver() {
String SYSTEM_REASON = "reason";
String SYSTEM_HOME_KEY = "homekey";
String SYSTEM_HOME_KEY_LONG = "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_REASON);
if (TextUtils.equals(reason, SYSTEM_HOME_KEY)) {
//表示按了home键,程序到了后台
Toast.makeText(getApplicationContext(), "home", 1).show();
}else if(TextUtils.equals(reason, SYSTEM_HOME_KEY_LONG)){
//表示长按home键,显示最近使用的程序列表
}
}
}
};
//注册广播 registerReceiver(mHomeKeyEventReceiver, new IntentFilter( Intent.ACTION_CLOSE_SYSTEM_DIALOGS));
package com.example.homedemo;
import android.os.Bundle;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.text.TextUtils;
import android.view.Menu;
import android.widget.Toast;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//注册广播
registerReceiver(mHomeKeyEventReceiver, new IntentFilter(
Intent.ACTION_CLOSE_SYSTEM_DIALOGS));
}
/**
* 监听是否点击了home键将客户端推到后台
*/
private BroadcastReceiver mHomeKeyEventReceiver = new BroadcastReceiver() {
String SYSTEM_REASON = "reason";
String SYSTEM_HOME_KEY = "homekey";
String SYSTEM_HOME_KEY_LONG = "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_REASON);
if (TextUtils.equals(reason, SYSTEM_HOME_KEY)) {
//表示按了home键,程序到了后台
Toast.makeText(getApplicationContext(), "home", 1).show();
}else if(TextUtils.equals(reason, SYSTEM_HOME_KEY_LONG)){
//表示长按home键,显示最近使用的程序列表
}
}
}
};
}
<uses-permission android:name="android.permission.DISABLE_KEYGUARD"/>
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(KeyEvent.KEYCODE_HOME==keyCode){
//写要执行的动作或者任务
android.os.Process.killProcess(android.os.Process.myPid());
}
return super.onKeyDown(keyCode, event);
}
@Override
public void onAttachedToWindow(){
this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD);
super.onAttachedToWindow();
}
if (keyCode == KeyEvent.KEYCODE_HOME) {
//在这里发送一个广播出去
}
private boolean isTesting = true;
class CatchLogThread extends Thread {
@Override
public void run() {
Process mLogcatProc = null;
BufferedReader reader = null;
String line;
while (isTesting) {
try {
// 获取logcat日志信息
mLogcatProc = Runtime.getRuntime().exec(new String[] { "logcat", "ActivityManager:I *:S" });
reader = new BufferedReader(new InputStreamReader(mLogcatProc.getInputStream()));
while ((line = reader.readLine()) != null) {
if (line.indexOf("android.intent.category.HOME") > 0) {
isTesting = false;
handler.sendMessage(handler.obtainMessage());
Runtime.getRuntime().exec("logcat -c");//删除日志
break;
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
};
Handler handler = new Handler() {
public void handleMessage(android.os.Message msg) {
Log.i(TAG,"Home key press");
//do something here
};
};
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有