Home > Software design >  Android App to start automatically on Boot Up
Android App to start automatically on Boot Up

Time:07-14

I am trying to get my Android app to start automatically on boot up. I have followed various examples and copied the code, but still, when I power up, the app does NOT start.

I am using a Galaxy Tab A7 Lite, running Android 11.

Any help gladly received.

Thank you.

Here is my code...

I have defined the receiver:

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

public class StartOnBootupReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        if (Intent.ACTION_BOOT_COMPLETED.equals(intent.getAction())) {
            Intent activityIntent = new Intent(context, MainActivity.class);
            context.startActivity(activityIntent);
        }
    }
}

And in the AndroidManifest.xml file:

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

and

<receiver android:name=".StartOnBootupReceiver"
    android:exported="false"
    >
    <intent-filter>
        <action android:name="android.intent.action.BOOT_COMPLETED" />
        <action android:name="android.intent.action..QUICKBOOT_POWERON" />
    </intent-filter>
</receiver>

CodePudding user response:

Your code looks good and your receiver is most likely executing. However, it is crashing because of the call to startActivity(). Since Android 10, startActivity() can only be called if the application is already in the Foreground or if it meets one of the listed exceptions listed in the Android guide Restrictions on starting activities from the background.

As an aside, the value of the android:exported has no effect. The OS can still call receivers who have android:exported="false" and, in the case of exported="true", other application cannot send a broadcast with BOOT_COMPLETED or QUICKBOOT_COMPLETED because they are protected intent actions.

CodePudding user response:

All your code is correct. However, if you look up android:exported="false" meaning you will find that:

The "exported" attribute describes whether or not someone else can be allowed to use it. So if you have "exported=false" on an Activity, no other app, or even the Android system itself, can launch it. Only you can do that, from inside your own application.

In your <receiver> statment, the android:exported="false" needs to be true.

That info borrowed from here and here.

  • Related