我正在启动一项 Activity 来调用电话,但是当我按下“结束通话”按钮时,它不会返回到我的 Activity 。您能告诉我如何启动一个通话 Activity ,当按下“结束通话”按钮时该 Activity 会返回给我吗?这就是我打电话的方式:
String url = "tel:3334444";
Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse(url));
请您参考如下方法:
使用 PhoneStateListener 查看通话何时结束。您很可能需要触发监听器操作以等待调用开始(等到再次从 PHONE_STATE_OFFHOOK 更改为 PHONE_STATE_IDLE),然后编写一些代码以使您的应用程序恢复到 IDLE 状态。
您可能需要在服务中运行监听器,以确保其保持正常运行并重新启动您的应用程序。一些示例代码:
EndCallListener callListener = new EndCallListener();
TelephonyManager mTM = (TelephonyManager)this.getSystemService(Context.TELEPHONY_SERVICE);
mTM.listen(callListener, PhoneStateListener.LISTEN_CALL_STATE);
监听器定义:
private class EndCallListener extends PhoneStateListener {
@Override
public void onCallStateChanged(int state, String incomingNumber) {
if(TelephonyManager.CALL_STATE_RINGING == state) {
Log.i(LOG_TAG, "RINGING, number: " + incomingNumber);
}
if(TelephonyManager.CALL_STATE_OFFHOOK == state) {
//wait for phone to go offhook (probably set a boolean flag) so you know your app initiated the call.
Log.i(LOG_TAG, "OFFHOOK");
}
if(TelephonyManager.CALL_STATE_IDLE == state) {
//when this state occurs, and your flag is set, restart your app
Log.i(LOG_TAG, "IDLE");
}
}
}
在您的 Manifest.xml
文件中添加以下权限:
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>