我已经创建了侦听IncomingCall
和Outgoing Calls
的BroadCastReceiver
。试图在BroadCastReceiver
中添加PhoneStateListener
和TelephonyManager
,这在模拟器和一些设备上工作得很好。
它不工作与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
为什么当我为Samsung ST 7652
调试时,它会在LogCat
中给出类似的东西,否则它会显示我完美的呼叫状态日志?
尝试
PhoneStateListener不能在某些设备上工作。但它也没有帮助我解决这个问题,总是给我CALL_STATE_IDLE
和入局号码null
。
在浏览了关于PhoneStateListeners
和android.intent.action.PHONE_STATE
的几个来源后,我找到了我正在寻找的东西,并且不管模型特定的障碍如何,它都像一个魅力。
BroadCastReceiver
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" />