How to Configure an Android App to Run Automatically After Terminal Boot

From wizarPOS
Revision as of 07:42, 21 June 2023 by Mahong (talk | contribs)

Description

  • Android apps can listen to system or custom broadcast messages by registering receivers1.
  • Android system sends a broadcast message when the phone boots up, called android.intent.action.BOOT_COMPLETED23.
  • Apps can declare a receiver in the manifest file to receive the boot broadcast and start a service or activity in it23.

Code example:

// Declare receiver in manifest file
<receiver android:name=".BootReceiver">
    <intent-filter>
        <action android:name="android.intent.action.BOOT_COMPLETED" />
    </intent-filter>
</receiver>

// Receiver class
public class BootReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        // Check if it is boot broadcast
        if (intent.getAction().equals("android.intent.action.BOOT_COMPLETED")) {
            // Start service or activity
            Intent newIntent = new Intent(context, MyService.class);
            context.startService(newIntent);
        }
    }
}

Demo APK

Demo APK