我正在三星GT-S7562(双SIM-4.0)上测试phoneStateListener,然后state总是返回0,传入号码总是返回空。虽然它在我的另一个设备三星GT-S5570(单SIM-2.2)上运行良好。这是我的密码。
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.widget.Toast;
public class IncomingCall extends BroadcastReceiver {
Context pcontext;
public void onReceive(Context context, Intent intent) {
pcontext = context;
try {
TelephonyManager tmgr = (TelephonyManager) context
.getSystemService(Context.TELEPHONY_SERVICE);
MyPhoneStateListener PhoneListener = new MyPhoneStateListener();
tmgr.listen(PhoneListener, PhoneStateListener.LISTEN_CALL_STATE);
} catch (Exception e) {
Log.e("Phone Receive Error", " " + e);
}
}
private class MyPhoneStateListener extends PhoneStateListener {
public void onCallStateChanged(int state, String incomingNumber) {
switch (state) {
case TelephonyManager.CALL_STATE_IDLE:
Toast.makeText(
pcontext,
"CALL_STATE_IDLE.\nIncomming Number : "
+ incomingNumber, Toast.LENGTH_SHORT).show();
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
Toast.makeText(
pcontext,
"CALL_STATE_OFFHOOK.\nIncomming Number : "
+ incomingNumber, Toast.LENGTH_SHORT).show();
break;
case TelephonyManager.CALL_STATE_RINGING:
Toast.makeText(
pcontext,
"CALL_STATE_RINGING.\nIncomming Number : "
+ incomingNumber, Toast.LENGTH_SHORT).show();
break;
default: {
Toast.makeText(pcontext,
"Default.\nIncomming Number : " + incomingNumber,
Toast.LENGTH_SHORT).show();
}
}
}
}
}
在menifest文件中还添加了permission。
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<receiver android:name="com.example.caller.ServiceReceiver" >
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE" />
</intent-filter>
</receiver>
最佳答案
PhoneStateListener不能在很多设备上工作,特别是在华为和中兴。
你可以试试这个:
public class PhoneStateReceiver extends BroadcastReceiver {
private static final String TAG = PhoneStateReceiver.class.getSimpleName();
private static String incoming_number = null;
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(Intent.ACTION_NEW_OUTGOING_CALL)) {
String phoneNumber = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER); // outgoing call
Log.i(TAG, "call OUT:" + phoneNumber);
} else {
TelephonyManager tm = (TelephonyManager) context.getSystemService(Service.TELEPHONY_SERVICE);
switch (tm.getCallState()) {
case TelephonyManager.CALL_STATE_RINGING: // incoming call
incomingFlag = true;
incoming_number = intent.getStringExtra("incoming_number");
LogUtils.d(TAG, "RINGING :" + incoming_number);
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
if (incomingFlag) {
LogUtils.d(TAG, "incoming ACCEPT :" + incoming_number);
}
break;
case TelephonyManager.CALL_STATE_IDLE:
if (incomingFlag) { // hang up
LogUtils.d(TAG, "incoming IDLE, number:" + incoming_number);
}
break;
}
}
}
显示:
<receiver android:name=".receiver.PhoneStateReceiver" android:exported="false" >
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE" />
</intent-filter>
</receiver>
但我不确定这个广播是否一直有效。我同时使用这两种方式。它工作得很好。祝你好运。
关于android - PhoneStateListener在三星GT-S7562上不起作用,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/20930684/