gpt4 book ai didi

android - 处理 Android 应用程序在来电时暂停并在通话结束后恢复

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:52:17 25 4
gpt4 key购买 nike

我想在手机收到来电时暂停我的 android 应用程序。通话结束后,我希望我的应用程序自动恢复。

这将如何在 Android 应用程序中实现?

最佳答案

您必须为 PhoneState 实现一个监听器。我是在私有(private)类里面做的:

private class PhoneCallListener extends PhoneStateListener {

private boolean isPhoneCalling = false;

// needed for logging
String TAG = "PhoneCallListener";

@Override
public void onCallStateChanged(int state, String incomingNumber) {

if (TelephonyManager.CALL_STATE_RINGING == state) {
// phone ringing
Log.i(TAG, "RINGING, number: " + incomingNumber);
}

if (TelephonyManager.CALL_STATE_OFFHOOK == state) {
// active
Log.i(TAG, "OFFHOOK");

isPhoneCalling = true;
}

if (TelephonyManager.CALL_STATE_IDLE == state) {
// run when class initial and phone call ended,
// need detect flag from CALL_STATE_OFFHOOK
Log.i(TAG, "IDLE");

if (isPhoneCalling) {

Log.i(TAG, "restart app");

// restart call application
Intent i = getBaseContext().getPackageManager()
.getLaunchIntentForPackage(
getBaseContext().getPackageName());
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK
| Intent.FLAG_ACTIVITY_CLEAR_TOP
| Intent.FLAG_ACTIVITY_SINGLE_TOP);
startActivity(i);

isPhoneCalling = false;
}

}


}
}

并且您需要将权限添加到Manifest-File

<uses-permission android:name="android.permission.READ_PHONE_STATE" />

关于android - 处理 Android 应用程序在来电时暂停并在通话结束后恢复,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9955016/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com