我们可以轻松检测用户是否正在通话。我需要添加一个过滤器,即用户是否对特定号码进行了任何调用。有可能吗?
答案 0 :(得分:0)
创建一个BroadcastReceiver类并在清单文件中注册它。并检查拨出电话意图。
前:
public class DialCallListener extends BroadcastReceiver {
private static final String OUTGOING_CALL_INTENT = "android.intent.action.NEW_OUTGOING_CALL";
//Inside onReceive method, check the call state
@Override
public void onReceive(Context context, Intent intent) {
int callState = TelephonyManager.CALL_STATE_IDLE;
try {
String stateStr = intent.getExtras().getString(TelephonyManager.EXTRA_STATE);
//Check if call state is OFFHook
if (stateStr.equals(TelephonyManager.EXTRA_STATE_OFFHOOK)) {
callState = TelephonyManager.CALL_STATE_OFFHOOK;
//Call the listener interface when call state change. And handle this state change inside PhoneListener class.
MyPhoneStateListener PhoneListener = new MyPhoneStateListener(context);
PhoneListener.onCallStateChanged(callState, number);
}
}catch (Exception err) {
err.printStackTrace();
}
} }
//如果呼叫状态改变(拨打任何号码)
,现在创建一个监听器private class MyPhoneStateListener extends PhoneStateListener implements ICallReceiver {
public void onCallStateChanged(int state, String dialing_number) {
switch (state) {
case TelephonyManager.CALL_STATE_OFFHOOK:
//Handle Call dial
mPresenter.handleCallOffHook(incomingNumber);
break;
}
} }