0123456789 0123456789 - 2 years ago 188
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"
android:versionName="1.0" >

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

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

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


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

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 Source

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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download