当前位置:首页 » 安卓系统 » android监听home

android监听home

发布时间: 2025-01-07 08:18:25

① 如何在Android的service里进行按键的监听

app中对于HOME键广播事件的接收

首先,自定义一个BroadcastReceiver:

[java] view plain
class HomeKeyEventBroadCastReceiver extends BroadcastReceiver {

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) {
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)) {
// home key处理点

} else if (reason.equals(SYSTEM_RECENT_APPS)) {
// long home key处理点
}
}
}
}
}
其次,生成且注册之
[java] view plain
receiver = new HomeKeyEventBroadCastReceiver();
registerReceiver(receiver, new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS));
这样,我们就可以在app中进行HOME键的处理了!

② android 在fragment中怎么监听返回键,home键

android 在fragment中监听返回键和home键的方法:
思路:
1、定义OnBackPressedListener监听接口
2、创建基础监听类,实现OnBackPressedListener接口
3、在BaseActivity中创建方法调用
4、重写onBackPressed方法和onHomePress方法
核心代码如下:
@Override
public void onBackPressed() {
FragmentManager fragmentManager =getSupportFragmentManager();
if (((C_fragment) getSupportFragmentManager().findFragmentByTag("xyz")) != null && ((C_fragment) getSupportFragmentManager().findFragmentByTag("xyz")).isVisible()) {
Fragment fragment = new D_Fragment();
fragmentManager.beginTransaction().replace(R.id.container, fragment).commit();
getSupportActionBar().setTitle("D fragment ");
} else {
super.onBackPressed();
}
}

@Override
public void onHomePressed() {
if (onBackPressedListener != null)
onBackPressedListener.doBack();
else
super.onBackPressed();

热点内容
工作站服务器的视频ip怎么看 发布:2025-01-08 13:09:15 浏览:652
唱吧上传卡住 发布:2025-01-08 12:52:23 浏览:531
烤鸡存储 发布:2025-01-08 12:50:16 浏览:793
android获取电量 发布:2025-01-08 12:44:42 浏览:761
samp服务器如何设置ip直播 发布:2025-01-08 12:35:20 浏览:123
5七的算法 发布:2025-01-08 12:30:59 浏览:447
linuxsvn配置 发布:2025-01-08 12:19:11 浏览:381
苹果扁头耳机如何在安卓手机上用 发布:2025-01-08 12:17:37 浏览:110
为什么解绑卡还要密码 发布:2025-01-08 12:13:51 浏览:74
反编译得到源代码的比例 发布:2025-01-08 12:12:36 浏览:620