PhoneStateListener无法使用Samsung GT-S7562

时间:2014-01-05 06:02:02

标签: android

我正在测试三星GT-S7562上的PhoneStateListener(双sim-4.0),然后STATE总是返回0并且INCOMING NUMBER总是返回空。虽然它在我的另一台设备SAMSUNG 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文件中添加了Persmission。

<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>

3 个答案:

答案 0 :(得分:9)

PhoneStateListener无法在许多设备上运行,特别是在HUAWEI和ZTE上。 你可以试试这个:

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>

但我不确定这个广播是否一直​​有效。我同时使用这两种方式。它工作正常。祝你好运。

答案 1 :(得分:1)

您必须拥有的第一件事是AndroidManifiest.xml文件中的权限

<uses-permission android:name="android.permission.READ_PHONE_STATE" />

在您的活动中,您必须实例化您的BroadcastReceiver对象

public class MainActivity extends Activity{

private IncomingCall service;

@Override
protected void onCreate(Bundle savedInstanceState) {

     service = new IncomingCall();

     //Finally you have to register your receiver
     IntentFilter intent= new IntentFilter("android.intent.action.PHONE_STATE");
     intent.setPriority(999);
     registerReceiver(service, intent);
}

我有一个开源项目,可以帮助您解决问题here

答案 2 :(得分:0)

我在huawei y300上检索广播事件时遇到了很多问题,他们根本就没有来。几个小时后,我终于通过PhoneStateListener成功检测到了调用。

我的错是我没有把接收器清单放在应用程序标签中:

<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
<uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS"/>
<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".MainActivity"
        android:label="@string/app_name"
        android:theme="@style/AppTheme.NoActionBar" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <receiver android:name=".ServiceReceiver" android:enabled="true">
        <intent-filter>
            <action android:name="android.intent.action.PHONE_STATE" />
            <action android:name="android.intent.action.NEW_OUTGOING_CALL" />
        </intent-filter>
    </receiver>
</application>

我的PhoneStateListenerClass:

import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.webkit.WebView;

public class MyPhoneStateListener extends PhoneStateListener {

public static Boolean phoneRinging = false;

@Override
public void onCallStateChanged(int state, String incomingNumber) {

    switch (state) {
        case TelephonyManager.CALL_STATE_IDLE:
            Log.d("DEBUG", "IDLE");
            phoneRinging = false;
            break;
        case TelephonyManager.CALL_STATE_OFFHOOK:
            Log.d("DEBUG", "OFFHOOK");
            phoneRinging = false;
            break;
        case TelephonyManager.CALL_STATE_RINGING:
            Log.d("DEBUG", "RINGING");
            phoneRinging = true;

            break;
    }
}

}

希望这能帮到你!