我需要我的应用程序来查找何时正在进行通话。
它应该适用于何时呼叫另一部电话以及何时接听电话。我只需要我的应用程序就确切地知道连接何时开始以及何时停止(不是拨号,响铃等)。
这有可能吗?
答案 0 :(得分:1)
您可以使用广播类来实现此目的:
public class PhoneStateBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
TelephonyManager telephonyManager = (TelephonyManager) context
.getSystemService(Context.TELEPHONY_SERVICE);
telephonyManager.listen(new CustomPhoneStateListener(context),
PhoneStateListener.LISTEN_CALL_STATE);
}
}
class CustomPhoneStateListener extends PhoneStateListener {
// private static final String TAG = "PhoneStateChanged";
Context context; // Context to make Toast if required
public CustomPhoneStateListener(Context context) {
super();
this.context = context;
}
@Override
public void onCallStateChanged(int state, String incomingNumber) {
super.onCallStateChanged(state, incomingNumber);
switch (state) {
case TelephonyManager.CALL_STATE_IDLE:
//WHEN PHONE WILL BE IDLE
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
// when Off hook i.e in call
break;
case TelephonyManager.CALL_STATE_RINGING:
// when Ringing
break;
default:
break;
}
}
}
希望这会对你有所帮助。