Broadcast Receiver not working when registered via manifest

nexus_2006 picture nexus_2006 · Jun 19, 2013 · Viewed 7.5k times · Source

Trying to setup a simple broadcast receiver in the manifest of an Android app, to detect when the phone is ringing and start a service. Its not receiving the broadcast when a call comes in, no log output, nada. I tried the android.intent.action.PHONE_STATE action and also the TelephonyManager.ACTION_PHONE_STATE_CHANGED in the manifest, neither did anything for me.

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <receiver
        android:name="com.berrmal.calllog.CallReceiver"
        android:enabled="true" >
        <intent-filter>
            <action android:name="android.intent.action.PHONE_STATE" />
        </intent-filter>
    </receiver>

and the Receiver:

public class CallReceiver extends BroadcastReceiver {

public void onReceive(Context c, Intent i) {
    Log.d("callreceiver", "onReceive()");
    String state = i.getStringExtra(TelephonyManager.EXTRA_STATE);
    Log.d("callreceiver", state);
    if (state.equals(TelephonyManager.CALL_STATE_RINGING)) {
        Intent serviceStartIntent = new Intent(c, RecordService.class);
        serviceStartIntent.putExtra("number", i.getStringExtra("EXTRA_INCOMING_NUMBER"));
        c.startService(serviceStartIntent);
    }
}
}

I looked around, and in this thread: Incoming call broadcast receiver not working (Android 4.1) they said that as of android 4.1 the system no longer sends broadcasts to app components without an activity...is this true?

Answer

CommonsWare picture CommonsWare · Jun 19, 2013

Incoming call broadcast receiver not working (Android 4.1) they said that as of android 4.1 the system no longer sends broadcasts to app components without an activity...is this true?

That has been the case since Android 3.1. Something must explicitly run one of your components before your manifest-registered BroadcastReceivers will work, and the typical solution for this is to have an activity (the same one that provides app configuration, help, license agreements, etc.) that the user launches.

Please read the Android 3.1 release notes for more.