【发布时间】:2016-05-20 09:14:58
【问题描述】:
我们可以轻松检测用户是否正在通话。我需要添加一个过滤器,即用户是否对特定号码进行了任何呼叫。有可能吗?
【问题讨论】:
-
也许你可以查看我的代码here
标签: java android android-layout android-studio android-intent
我们可以轻松检测用户是否正在通话。我需要添加一个过滤器,即用户是否对特定号码进行了任何呼叫。有可能吗?
【问题讨论】:
标签: java android android-layout android-studio android-intent
创建一个 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;
}
} }
【讨论】: