Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
248 views
in Technique[技术] by (71.8m points)

android - Not able to get the TelephonyManager.CALL_STATE_RINGING

I added this is my manifest file -

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

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

Then my service class is like this -

public class ServiceReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
    MyPhoneStateListener phoneListener = new MyPhoneStateListener();
    TelephonyManager telephony = (TelephonyManager) context
            .getSystemService(Context.TELEPHONY_SERVICE);
    telephony.listen(phoneListener, PhoneStateListener.LISTEN_CALL_STATE);
}

}

and my PhoneStateListener is -

public class MyPhoneStateListener extends PhoneStateListener {

public void onCallStateChanged(int state, String incomingNumber) {
    Log.i("telephony-example", "State changed: " + stateName(state));
}

String stateName(int state) {
    switch (state) {
    case TelephonyManager.CALL_STATE_IDLE:
        Log.d("DEBUG", "***********IDLE********");
        return "Idle";
    case TelephonyManager.CALL_STATE_OFFHOOK:
        Log.d("DEBUG", "***********OFFHOOK********");
        return "Off hook";
    case TelephonyManager.CALL_STATE_RINGING:
        Log.d("DEBUG", "***********RINGING********");
        return "Ringing";
    }
    return Integer.toString(state);
}

}

I am able to see the IDLE State.

But When I call i dont get the Ringing state. Why?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I think you are mixing up two approaches to get phone state. If you use the intent-filter and broadcast receiver, then in the receiver no need to call the TelephonyManager's listen(). Just check the received intent like this :

public void onReceive(Context context, Intent intent) {
    String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE);
    String number = intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER);
    if (TelephonyManager.EXTRA_STATE_RINGING.equals(state))
    {
        Log.d("MPR", "Its Ringing [" + number + "]");
    }
    if (TelephonyManager.EXTRA_STATE_IDLE.equals(state))
    {
        Log.d("MPR", "Its Idle");
    }
    if (TelephonyManager.EXTRA_STATE_OFFHOOK.equals(state))
    {
        Log.d("MPR", "Its OffHook");
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

56.9k users

...