How to Configure an Android App to Run Automatically After Terminal Boot: Difference between revisions
Line 30: | Line 30: | ||
== Demo APK == | == Demo APK == | ||
[http://ftp.wizarpos.com/advanceSDK/ | [http://ftp.wizarpos.com/advanceSDK/BootCompleteSelf_131017.zip Demo APK] |
Revision as of 07:19, 7 October 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);
}
}
}