How to Configure an Android App to Run Automatically After Terminal Boot: Difference between revisions

From wizarPOS
No edit summary
Line 1: Line 1:
== Description ==
== Steps ==
* Android apps can listen to system or custom broadcast messages by registering receivers1.
* Android apps can listen to system or custom broadcast messages by registering receivers1.


Line 28: Line 28:
}
}
</syntaxhighlight >
</syntaxhighlight >
== Demo APK ==
== Demo APK ==
[http://ftp.wizarpos.com/advanceSDK/BootCompleteAutoStartDemo.zip Demo APK]
[http://ftp.wizarpos.com/advanceSDK/BootCompleteAutoStartDemo.zip Demo APK]

Revision as of 07:42, 21 June 2023

Steps

  • 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