PhoneStateListener不工作的三星GT-S7562 [英] PhoneStateListener not working on Samsung GT-S7562
问题描述
我在三星GT-S7562测试PhoneStateListener(双SIM- 4.0),当时的国家总是返回0,来电号码总是返回空。虽然它工作正常,我的另一台设备三星GT-S5570(单SIM- 2.2)上。这里是我的code。
进口android.content.BroadcastReceiver;
进口android.content.Context;
进口android.content.Intent;
进口android.telephony.PhoneStateListener;
进口android.telephony.TelephonyManager;
进口android.util.Log;
进口android.widget.Toast;公共类IncomingCall扩展广播接收器{
上下文pcontext; 公共无效的onReceive(上下文的背景下,意图意图){
pcontext =背景; 尝试{
TelephonyManager TMGR =(TelephonyManager)上下文
.getSystemService(Context.TELEPHONY_SERVICE); MyPhoneStateListener PhoneListener =新MyPhoneStateListener(); tmgr.listen(PhoneListener,PhoneStateListener.LISTEN_CALL_STATE); }赶上(例外五){
Log.e(手机接收错误,+ E);
} } 私有类MyPhoneStateListener扩展PhoneStateListener {
公共无效onCallStateChanged(INT状态,弦乐incomingNumber){
开关(州){
案例TelephonyManager.CALL_STATE_IDLE:
Toast.makeText(
pcontext,
CALL_STATE_IDLE \\ nIncomming号:
+ incomingNumber,Toast.LENGTH_SHORT).show();
打破;
案例TelephonyManager.CALL_STATE_OFFHOOK:
Toast.makeText(
pcontext,
CALL_STATE_OFFHOOK \\ nIncomming号码:
+ incomingNumber,Toast.LENGTH_SHORT).show();
打破;
案例TelephonyManager.CALL_STATE_RINGING:
Toast.makeText(
pcontext,
CALL_STATE_RINGING \\ nIncomming号:
+ incomingNumber,Toast.LENGTH_SHORT).show();
打破;
默认:{
Toast.makeText(pcontext,
默认\\ nIncomming数:+ incomingNumber,
Toast.LENGTH_SHORT).show();
}
}
}
}
}
此外,在menifest文件添加Persmission。
<使用许可权的android:NAME =android.permission.READ_PHONE_STATE/><接收机器人:名字=com.example.caller.ServiceReceiver>
&所述;意图滤光器>
<作用机器人:名字=android.intent.action.PHONE_STATE/>
&所述; /意图滤光器>
< /接收器>
PhoneStateListener不能多设备特别是对华为和中兴的合作。
你可以试试这个:
公共类PhoneStateReceiver扩展广播接收器{
私有静态最后弦乐TAG = PhoneStateReceiver.class.getSimpleName();
私人静态字符串incoming_number = NULL;@覆盖
公共无效的onReceive(上下文的背景下,意图意图){
如果(intent.getAction()。等于(Intent.ACTION_NEW_OUTGOING_CALL)){
字符串phoneNumber的= intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER); //呼出
Log.i(TAG,咻+ phoneNumber的);
}其他{
TelephonyManager TM =(TelephonyManager)context.getSystemService(Service.TELEPHONY_SERVICE); 开关(tm.getCallState()){
案例TelephonyManager.CALL_STATE_RINGING://来电
incomingFlag = TRUE;
incoming_number = intent.getStringExtra(incoming_number);
LogUtils.d(TAG,振铃:+ incoming_number);
打破;
案例TelephonyManager.CALL_STATE_OFFHOOK:
如果(incomingFlag){
LogUtils.d(TAG,来袭接受:+ incoming_number);
}
打破; 案例TelephonyManager.CALL_STATE_IDLE:
如果(incomingFlag){//挂断 LogUtils.d(TAG,来电IDLE,数:+ incoming_number);
}
打破;
}
}
}
清单:
<接收机器人:receiver.PhoneStateReceiverNAME =机器人:出口=假>
&所述;意图滤光器>
<作用机器人:名字=android.intent.action.PHONE_STATE/>
&所述; /意图滤光器>
< /接收器>
但我不知道这是否广播始终有效。我用的两路同时。它工作正常。祝你好运。
I am testing PhoneStateListener on Samsung GT-S7562(dual sim- 4.0) then STATE always returns 0 and INCOMING NUMBER always returns empty. While it works fine on my another device SAMSUNG GT-S5570(single sim- 2.2). Here is my code.
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();
}
}
}
}
}
Also added Persmission in menifest file.
<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 can not work on many device especially on HUAWEI and ZTE. You can try this:
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;
}
}
}
manifest:
<receiver android:name=".receiver.PhoneStateReceiver" android:exported="false" >
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE" />
</intent-filter>
</receiver>
But I'm NOT sure if this broadcast always works. I use the two way at the same time. It works fine. Good luck.
这篇关于PhoneStateListener不工作的三星GT-S7562的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!