我使用此代码调用:
Uri callUri = Uri.parse("tel://911");
Intent callIntent = new Intent(Intent.ACTION_CALL,callUri);
callIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_NO_USER_ACTION);
startActivity(callIntent);
我希望在用户关闭通话时执行某些操作
先谢谢
答案 0 :(得分:2)
首先,在清单中注册以下权限:
<uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS" />
然后,一个名为OutCallLogger的BroadcastReceiver每当拨打电话时都会捕获NEW_OUTGOING_CALL
事件
在清单中注册您的接收器
<receiver android:name=".listener.OutCallLogger">
<intent-filter>
<action android:name="android.intent.action.NEW_OUTGOING_CALL" />
</intent-filter>
</receiver>
接下来,实现OutCallLogger。设置一个名为noCallListenerYet的布尔值,以避免在调用onReceive()时将新的PhoneStateListener附加到TelephonyManager。
public class OutCallLogger extends BroadcastReceiver {
private static boolean noCallListenerYet = true;
@Override
public void onReceive(final Context context, Intent intent) {
number = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
if (noCallListenerYet) {
final TelephonyManager tm = (TelephonyManager) context.getSystemService(
Context.TELEPHONY_SERVICE);
tm.listen(new PhoneStateListener() {
@Override
public void onCallStateChanged(int state, String incomingNumber) {
switch (state) {
case TelephonyManager.CALL_STATE_RINGING:
Log.d(This.LOG_TAG, "RINGING");
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
Log.d(This.LOG_TAG, "OFFHOOK");
break;
case TelephonyManager.CALL_STATE_IDLE:
Log.d(This.LOG_TAG, "IDLE");
break;
default:
Log.d(This.LOG_TAG, "Default: " + state);
break;
}
}
}, PhoneStateListener.LISTEN_CALL_STATE);
noCallListenerYet = false;
}
}
当用户按结束通话时,会调用TelephonyManager.CALL_STATE_IDLE:
状态。
希望它会对你有所帮助。