Search code examples
androidandroid-broadcast

BroadcastReceiver doesn't work on Android 2.x


I'm developing an Android app that uses a BroadcastReceiver to do some work when the phone boots up. On my Nexus 5 running Android 4.4.4 KitKat it works fine, but a friend of mine tested it in a phone running Android 2.3.6 and it didn't worked. My code is as follows:

public class PowerConnectionReceiver extends BroadcastReceiver {

private static final int NOTIFICATION_ID = 1;

@Override
public void onReceive(Context context, Intent intent) {

    NotificationCompat.Builder notifBuilder = new NotificationCompat.Builder(context)
            .setSmallIcon(R.drawable.ic_launcher)
            .setContentTitle("My app")
            .setContentText("My notification...")
            .setTicker("My awesome notification");

    NotificationManager notifManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    notifManager.notify(NOTIFICATION_ID, notifBuilder.build());
}

}

In the BroadcastReceiver, I simply show a notification. Then, I register the BroadcastReceiver on the AndroidManifest.xml:

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

  ...More code...

   <application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >

    ...More code...

   <receiver
        android:name=".activities.PowerConnectionReceiver"
        android:enabled="true" >
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED" />
        </intent-filter>
    </receiver>

    ... More code...
     </application>

What can I do to make my app work on Android 2.x?

Thanks in advance.


Solution

  • Try setting the permission outside of the receiver as well:

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

    As well as setting the installation location to internalOnly:

    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
    ...
    android:installLocation="internalOnly"
    >
    

    Finally, try modifying the receiver code as such if the above still doesn't work:

    <receiver android:name=".activities.PowerConnectionReceiver"
            android:enabled="true"
            android:permission="android.permission.RECEIVE_BOOT_COMPLETED">
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED" />
                <action android:name="android.intent.action.QUICKBOOT_POWERON" />
            </intent-filter>
        </receiver>