當前位置:首頁 » 安卓系統 » 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-07-11 15:29:22 瀏覽:662
asynctask上傳 發布:2025-07-11 15:20:17 瀏覽:590
編程語言的圖標 發布:2025-07-11 15:18:04 瀏覽:417
分式編譯器 發布:2025-07-11 15:15:05 瀏覽:71
烤生蚝的調料怎麼配置 發布:2025-07-11 15:15:02 瀏覽:550
優酷視頻上傳解析度 發布:2025-07-11 15:15:00 瀏覽:603
超時代加密提取工具 發布:2025-07-11 15:13:37 瀏覽:962
重慶金稅盤接入伺服器連接地址 發布:2025-07-11 15:02:10 瀏覽:356
手機陰陽師腳本輔助 發布:2025-07-11 14:43:13 瀏覽:17
php通知 發布:2025-07-11 14:28:53 瀏覽:168