简体   繁体   中英

PhoneStateListener not working on Samsung GT-S7562

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.

First thing you must have is the permission in your AndroidManifiest.xml file

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

In your activity you have to instantiate your BroadcastReceiver object

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);
}

I have an opensource project that can help you with your issues here

I have had a lot of problems to retrieve the Broadcast events on my huawei y300, they simply did not come. After hours I finally succeeded to detect calls via the PhoneStateListener.

My fault was that i did not put the receiver manifest inside the application tag:

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

My 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;
    }
}

}

Hope this helps you!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM