How to Configure an Android App to Run Automatically After Terminal Boot: Difference between revisions
(Created page with " [http://ftp.wizarpos.com/advanceSDK/BootCompleteAutoStartDemo.zip demo APK]") |
No edit summary |
||
Line 1: | Line 1: | ||
== Description == | |||
* Android apps can listen to system or custom broadcast messages by registering receivers1. | |||
[http://ftp.wizarpos.com/advanceSDK/BootCompleteAutoStartDemo.zip | * 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: | |||
<syntaxhighlight lang="java" line='line'> | |||
// 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); | |||
} | |||
} | |||
} | |||
</syntaxhighlight > | |||
== Demo APK == | |||
[http://ftp.wizarpos.com/advanceSDK/BootCompleteAutoStartDemo.zip Demo APK] |
Revision as of 07:42, 21 June 2023
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);
}
}
}