當前位置:首頁 » 安卓系統 » 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();

熱點內容
唱吧上傳卡住 發布:2025-01-08 12:52:23 瀏覽:530
烤雞存儲 發布:2025-01-08 12:50:16 瀏覽:792
android獲取電量 發布:2025-01-08 12:44:42 瀏覽:760
samp伺服器如何設置ip直播 發布:2025-01-08 12:35:20 瀏覽:122
5七的演算法 發布:2025-01-08 12:30:59 瀏覽:446
linuxsvn配置 發布:2025-01-08 12:19:11 瀏覽:380
蘋果扁頭耳機如何在安卓手機上用 發布:2025-01-08 12:17:37 瀏覽:109
為什麼解綁卡還要密碼 發布:2025-01-08 12:13:51 瀏覽:73
反編譯得到源代碼的比例 發布:2025-01-08 12:12:36 瀏覽:619
蘋果手機7z怎麼解壓 發布:2025-01-08 12:11:55 瀏覽:121