![](/img/trans.png)
[英]How to know if my application is in foreground or background, android?
[英]How to know if the calling intent is running in background or foreground?
我正在Android中使用電話意圖,如下所示:
Intent callIntent = new Intent(Intent.ACTION_CALL);
callIntent.setData(Uri.parse("tel:"+Uri.encode(phone_num.trim())));
callIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(callIntent);
包名稱為com.android.incallui
。 我想知道調用GUI是在后台還是在前台運行。 因此,我創建了一個函數checkcallingGUI
,如果調用方GUI在前台運行,它將返回true;如果調用方電話在后台運行,則將返回false。 可以檢查嗎? 謝謝大家
您需要在TelephonyManager
呼叫狀態( IDLE
, OFFHOOK
, RINGING
)上注冊一個listener
,並使用最后收到的狀態來了解當前是否正在進行呼叫。
使用此方法注冊偵聽器: https : //developer.android.com/reference/android/telephony/TelephonyManager.html#listen(android.telephony.PhoneStateListener,int)
在此處閱讀有關可能的陳述及其含義的信息: https : //developer.android.com/reference/android/telephony/TelephonyManager.html#CALL_STATE_IDLE
並在此處查看完整的代碼: https : //stackoverflow.com/a/14537914/819355
編輯
您可以使用KeyguardManager服務來檢查設備當前是否被鎖定/安全,請參見以下方法: https : //developer.android.com/reference/android/app/KeyguardManager.html#isKeyguardLocked()
如果我沒記錯的話,如果通話狀態不是IDLE,並且啟用了鍵盤鎖,那么您將獲得全屏UI,如果沒有,則應在后台。
嘗試這樣的事情:
KeyguardManager kg = (KeyguardManager) context.getSystemService(Context.KEYGUARD_SERVICE);
if (kg.isKeyguardLocked()) {
// If telephony-state is also not IDLE, then the call UI should be full screen.
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.