1

My startup receiver is not working on Android 10. Why?

50 points reward for a solution.

AndroidManifest.xml

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" tools:ignore="ProtectedPermissions"/>
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>


    <receiver android:name="ServiceStarter"
        android:enabled="true"
        android:exported="true"
        android:permission="android.permission.RECEIVE_BOOT_COMPLETED">
        <intent-filter>
            <category android:name="android.intent.category.DEFAULT" />
            <acenter code heretion android:name="android.intent.action.BOOT_COMPLETED" />
            <action android:name="android.intent.action.ACTION_BOOT_COMPLETED" />
            <action android:name="android.intent.action.REBOOT" />
            <action android:name="android.intent.action.QUICKBOOT_POWERON" />
            <action android:name="com.htc.intent.action.QUICKBOOT_POWERON" />
            <action android:name="android.intent.action.ACTION_SHUTDOWN" />
        </intent-filter>
    </receiver>

ServiceStarter.Java

public class ServiceStarter extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
    SharedPreferences mSharedPref = context.getSharedPreferences("general", MODE_PRIVATE);
    mSharedPref.edit().putInt("Boot", 1).apply();
}
}

MainActivity.java

int boot = mSharedPref.getInt("Boot", 0);
Toast.makeText(this, "Boot: " + String.valueOf(boot), Toast.LENGTH_LONG).show();

When I open my app, "Boot" always 0. onReceive() doesn't get called.

Ashraf Alshahawy
  • 1,139
  • 1
  • 14
  • 38
  • Try this answer: https://stackoverflow.com/questions/11325920/how-to-test-boot-completed-broadcast-receiver-in-emulator You can send an onBootCompleted signal via adb and test if the signal is beeing received. – Dominik Wuttke Jul 02 '21 at 19:21
  • why do you put it in getSharedPreferences? you can start MainActivity.java – hungcuiga1 Jul 03 '21 at 01:03
  • @hungcuiga1 I used the shared pref to store a value to check it later to see if onReceive is called or not. I don't want to launch MainActivity. I used the receiver to launch a background service. – Ashraf Alshahawy Jul 03 '21 at 03:04

1 Answers1

0

Finally I found the solution. Since Android 24 you need to add directBootAware attribute to the receiver to receive the BOOT_COMPLETED intent, in case the phone is locked by a PIN, Pattern etc.

android:directBootAware="true"

And onReceive would be

@Override
public void onReceive(Context context, Intent intent) {
    //Toast.makeText(context, "onReceive Boot", Toast.LENGTH_LONG).show();

    SharedPreferences mSharedPref = context.getSharedPreferences("general", MODE_PRIVATE);


    if(intent != null && intent.getAction() != null){
        if (Intent.ACTION_BOOT_COMPLETED.equals(intent.getAction()) || 
          Intent.ACTION_LOCKED_BOOT_COMPLETED.equals(intent.getAction())) {
        }
    }
}
Ashraf Alshahawy
  • 1,139
  • 1
  • 14
  • 38