gpt4 book ai didi

android - 每次结束电话都会回到我的 Activity ,即使我没有从我的应用程序 android 调用电话

转载 作者:行者123 更新时间:2023-11-30 03:20:49 24 4
gpt4 key购买 nike

我已经创建了一个应用程序,用户可以在其中点击按钮进行调用。

我发现下面的代码可以很好地调用电话并在电话结束时返回我的 Activity 。但是我在这个应用程序中遇到一个问题,那就是一旦我从我的应用程序调用电话并结束该电话,在完成整个循环后,我按下主页按钮或后退按钮。我会从我的电话目录中调用某个人,当结束通话时,它将返回到我的应用程序,而不是在电话目录中。

public void imgbtnCallPhone_Click(View view) {
EditText txtBusinessPhone = (EditText) findViewById(R.id.txtPhone);

try
{
final Intent callIntent = new Intent(android.content.Intent.ACTION_CALL);
callIntent.setData(Uri.parse("tel:"+ txtBusinessPhone.getText()));
startActivity(callIntent);
} catch (ActivityNotFoundException activityException) {
//Log.e("Calling a Phone Number", "Call failed", activityException);
}

}

private class PhoneCallListener extends PhoneStateListener {

private boolean isPhoneCalling = false;

String LOG_TAG = "LOGGING 123";

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

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

if (TelephonyManager.CALL_STATE_OFFHOOK == state) {
// active
//Log.i(LOG_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(LOG_TAG, "IDLE");

if (isPhoneCalling) {

//Log.i(LOG_TAG, "restart app");

// restart app
Intent i = getBaseContext().getPackageManager()
.getLaunchIntentForPackage(
getBaseContext().getPackageName());
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);

isPhoneCalling = false;
}

}
}
}

我想要一个代码来检查电话是否与我的申请相关,如果我的应用程序完成了电话,那么在结束电话后它会回来到我的应用程序 Activity ,否则不要返回到我的 Activity ,默认执行。

谢谢,

最佳答案

我认为您需要在通话前启动 PhoneCallListener,将您要调用的号码传递给 PhoneCallListener,然后启动 callIntent。

在您的 PhoneListener 中,您可以检查该号码是否与您从 Activity 传递的号码匹配。如果为真,请重新启动您的 Activity ,否则什么都不做。

编辑:

public void imgbtnCallPhone_Click(View view) {
EditText txtBusinessPhone = (EditText) findViewById(R.id.txtPhone);

// Get your PhoneCallListener and pass the number
PhoneCallListener mPhoneListener = new PhoneCallListener();
mPhoneListener.yourActivity = true;

// start listening
TelephonyManager tm = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
tm.listen(mPhoneListener, PhoneStateListener.LISTEN_CALL_STATE);

try
{
final Intent callIntent = new Intent(android.content.Intent.ACTION_CALL);
callIntent.setData(Uri.parse("tel:"+ txtBusinessPhone.getText()));
startActivity(callIntent);
} catch (ActivityNotFoundException activityException) {
//Log.e("Calling a Phone Number", "Call failed", activityException);
}

}

您的 PhoneCallListener:

private class PhoneCallListener extends PhoneStateListener {
private boolean isPhoneCalling = false;
public Boolean yourActivity = false;

String LOG_TAG = "LOGGING 123";

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

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

if (TelephonyManager.CALL_STATE_OFFHOOK == state) {
// active
//Log.i(LOG_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(LOG_TAG, "IDLE");

if (isPhoneCalling && yourActivity) {

//Log.i(LOG_TAG, "restart app");
// restart app
Intent i = getBaseContext().getPackageManager()
.getLaunchIntentForPackage(
getBaseContext().getPackageName());
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);

yourActivity = false;
isPhoneCalling = false;
}
}
}
}

我没有测试它,所以它可能包含一些错误。

关于android - 每次结束电话都会回到我的 Activity ,即使我没有从我的应用程序 android 调用电话,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19175145/

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