0123456789 0123456789 - 11 days ago 6
Android Question

BroadcastReceiver.onReceive is not called for PHONE_STATE

Following this tutorial: http://www.vogella.de/articles/AndroidServices/article.html#receiver I created my own project. Manifest:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="alex.broadcast.sample"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk android:minSdkVersion="13" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >

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

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


Code:

public class MyPhoneReceiver extends BroadcastReceiver {
final String logTag = "BroadcastReceiverSample";

@Override
public void onReceive(Context context, Intent intent) {
Bundle extras = intent.getExtras();
if (extras != null) {
String state = extras.getString(TelephonyManager.EXTRA_STATE);

Log.i(logTag, "Call state: " + state);

if (state.equals(TelephonyManager.EXTRA_STATE_RINGING)) {
String phoneNumber = extras.getString(TelephonyManager.EXTRA_INCOMING_NUMBER);
Log.i(logTag, "Phone number: " + phoneNumber);
}
}
}
}


Running this sample on Android simulator, I see that it is successfully installed. However, onReceive function is never called. I make incoming call using:

telnet localhost 5554
gsm call 12345678


Emulator shows incoming call, but onReceive is not called.

Answer

Shouldn't it be:

<receiver android:name=".MyPhoneReceiver">
                        ^ note the dot

Also, the location of the permission is wrong, it should be a child of <manifest> not of <Application>.

Comments