Amar Mishra Amar Mishra - 4 months ago 71
Android Question

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>

Answer

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.