How can I start android application on device boot?
After I added the boot permissions in manifest file (see code below), BroadcastReceiver started to get boot completed event and the service is starting successfully. For the solution to work (as suggested by @Eren Tüfekçi) I had to enable auto start permission in phone settings for my application. If anyone has a solution, how to enable it programmatically, please let us know. Thank you.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="kor.location.tracker">
<uses-permission android:name="android.permission.INTERNET" ></uses-permission>
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<receiver android:name="kor.location.tracker.AutoStart"
android:enabled="true"
android:exported="true">
<intent-filter android:directBootAware="true">
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.LOCKED_BOOT_COMPLETED" />
<action android:name="android.intent.action.QUICKBOOT_POWERON" />
<action android:name="android.intent.action.REBOOT"/>
</intent-filter>
</receiver>
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:theme="@style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:enabled="true"
android:name="kor.location.tracker.WorkerService"
android:exported="true"
android:permission="android.permission.BIND_JOB_SERVICE"
/>
</application>
</manifest>
How to start/ launch application at boot time Android
These lines of code may be helpful for you...
Step 1: Set the permission in AndroidManifest.xml
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
Step 2: Add this intent filter in receiver
<receiver android:name=".BootReceiver">
<intent-filter >
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver>
Step 3: Now you can start your application's first activity from onReceive
method of Receiver class
public class BootReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Intent myIntent = new Intent(context, MainActivity.class);
myIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(myIntent);
}
}
launch android app when android device is turn on or restart
Per the following post:
How to start an Application on startup?
You might need a Receive_boot_completed permission. There are two answers in the post which show how to start either an activity or a service on app start.
Is it possible to launch an Activity on startup in Android 11?
The larger goal is to tinker with a used Pixel 3a and turn it into a home utility device
Write a launcher app (i.e., have your activity respond to ACTION_VIEW
/CATEGORY_HOME
) and set it as your default launcher. It will automatically start when the phone starts, just as your current default launcher does. And, you can put a button or something in your own launcher that launches the real launcher via an explicit Intent
.
This would allow you to skip the on-boot receiver and the service.
Why doesn't this start?
You do not qualify for a background activity start.
Google Maps most certainly has a visible window, so I am expecting to be able to start it.
First, presumably it does not have a visible window right after boot.
Second, you are not writing the Google Maps app, and Google Maps is not trying to start an activity from the background. You are writing your own app, and your own app is trying to start an activity from the background. And, since your own app does not have a visible window right after boot, it does not qualify for a background activity start under the "app has a visible window" rule.
How to launch the application when the device booted up
You should register a BroadcastReceiver
and then listen for ACTION_BOOT_COMPLETED
event in that receiver.
public class yourBroadcastReceiver extends BroadcastReceiver{
@Override
public void onReceive(Context ctxt, Intent intent) {
// TODO Auto-generated method stub
if(Intent.ACTION_BOOT_COMPLETED.equals(intent.getAction())) {
Intent selfIntent = new Intent(Intent.ACTION_MAIN);
selfIntent.setClass(ctxt, DestActivty.class);
selfIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
ctxt.startActivity(selfIntent);
}
}
}
You will need to modify your AndroidManifest
file too and register this receiver in the Application
node
<receiver android:name="yourBroadcastReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver>
And this too
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
How to enable auto start automatically just after installing app from play store
From my understanding, this can't be achieved by every app. The apps you see, which are automatically auto-launching after installation are white-listed by manufacturers or Google themselves. All you can do is send the user to the specific settings to enable the auto-launch after installing the app. This is also described here:
Add my app to AutoStart apps list in android programmatically
What is the best approach for autostarting an Android app on a dedicated device?
I'm currently engaged on a dedicated-device application and I can provide you a few details that might be useful.
Listening to ACTION_BOOT_COMPLETED
is the same as waiting for the device to finish it's boot, start it's Home application and then launch your app.
I believe you don't want your final client to see a home screen and neither do I.
You can, however, define your application as the HOME
application of the Android device. As soon as it finishes it's boot, it will be the first visible application on the run.
<activity....>
<!-- Put this filter inside the activity you want to make the Home screen -->
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.HOME" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
Note: if you need your device to boot automatically when your dedicated-device turns on, try removing the battery from Android and connect it straight. This is not related to your question indeed but the way I see, might be helpful on your project.
Related Topics
Http Failure Response for (Unknown Url): 0 Unknown Error on Android
Method to Refresh Fragment Content When Data Changed ( Like Recall Oncreateview)
How to Measure Distance to Object With Camera
How to Validate Password Field in Android
Remove Apk from Library in Google Play Developer Console
How to Control Navigationview Margin Between Menu Items
Open Play Store App from Browser Link
Android How to Get Current Positon on Recyclerview That User Scrolled to Item
How to Format Credit Card Expiry Date in Mm/Yy When Entered by User
How to Display an Image in Full Screen on Click in the Imageview
Get City Name and Postal Code from Google Place API on Android
Scrollview Not Scrolling in Android Bottomsheet
Remove Green Border Around Android Application
How to Fix Mainactivity Does Not Have a Navcontroller
How to Set Android_Ndk_Home So That Android Studio Does Not Ask for Ndk Location