我创建了BroadCastReceiver
,用于收听IncomingCall
和Outgoing Calls
。试图在PhoneStateListener
中添加TelephonyManager
BroadCastReceiver
,该版本在模拟器和某些设备上运行良好。
它不适用于Samsung ST 7652
。始终返回CALL_STATE_IDLE
和来电号null
。为什么会这样?
如下:
public class ReceiverInComingCall extends BroadcastReceiver {
public void onReceive(Context context, Intent intent) {
TelephonyManager phone = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
MyPhoneStateListener PhoneListener = new MyPhoneStateListener();
phone.listen(PhoneListener, PhoneStateListener.LISTEN_CALL_STATE);
}
private class MyPhoneStateListener extends PhoneStateListener{
@Override
public void onCallStateChanged(int state, String incomingNumber) {
super.onCallStateChanged(state, incomingNumber);
switch(state){
case TelephonyManager.CALL_STATE_RINGING:
Log.i("CALL","CALL_STATE_RINGING");
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
Log.i("CALL","CALL_STATE_OFFHOOK");
break;
case TelephonyManager.CALL_STATE_IDLE:
Log.i("CALL","CALL_STATE_IDLE");
break;
}
}
}
}
困扰我:
E/MSimTelephonyManager(621): MSimTelephonyManager sRegistryMsim != null
为LogCat
调试Samsung ST 7652
时,为什么它会在CALL_STATE_IDLE
中提供类似的内容,否则会向我显示完整的呼叫状态记录?
的尝试 的
PhoneStateListener not working with some devices。但它并没有帮助我解决方案总是给我null
和来电号码{{1}}。
答案 0 :(得分:3)
在浏览了几个有关PhoneStateListeners
和android.intent.action.PHONE_STATE
的消息来源后,发现了我正在寻找的内容,并且无论模型特定的障碍如何,它都像魅力一样。
<强> 广播接收器 强>
public class PhoneStateReceiver extends BroadcastReceiver {
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
String str = intent.getAction();
if ("android.intent.action.PHONE_STATE".equals(str))
inComing(context, intent);
if ("android.intent.action.NEW_OUTGOING_CALL".equals(str))
outGoing(context, intent);
}
private void inComing(Context context, Intent intent){
String callState = intent.getStringExtra("state");
if ("RINGING".equals(callState)){
Log.i(TAG, "RINGING SENDS BUSY");
}else if ("OFFHOOK".equals(callState)){
Log.i(TAG, "OFFHOOK SENDS BUSY");
}else if("IDLE".equals(callState)){
Log.i(TAG, "IDLE SENDS AVAILABLE");
}
}
private void trueCallerOutgoing(Context context, Intent intent)
{
String callState = intent.getStringExtra("state");
if ("RINGING".equals(callState)){
Log.i(TAG, "RINGING SENDS BUSY");
}else if ("OFFHOOK".equals(callState)){
Log.i(TAG, "OFFHOOK SENDS BUSY");
}else if("IDLE".equals(callState)){
Log.i(TAG, "IDLE SENDS AVAILABLE");
}
}
}
<强> 清单 强>
<receiver android:name="PhoneStateReceiver" >
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE" />
<action android:name="android.intent.action.NEW_OUTGOING_CALL" />
</intent-filter>
</receiver>
有权限
<uses-permission android:name="android.permission.READ_PHONE_STATE" />