Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to start/ launch application at boot time Android

I would like to launch my app when my tablet starts, so that the main activity of my app is the first thing that the user see when they start the tablet.
I've read about LauncherActivity but I don't understand how to use it.
Can anyone help me with suggestions, links or tutorials for this?
Is LauncherActivity the best way or are there alternatives?

like image 528
Marco Gallella Avatar asked May 03 '12 09:05

Marco Gallella


People also ask

How do I make a program run at startup Android?

Androids can vary significantly based on the manufacturer and the model. To give this method a try, open Settings and go to the Application Manager. It should be in "Installed Apps" or "Applications," depending on your device. Select an app from the list of downloaded apps and turn the Autostart option on or off.

How do I start an application at startup?

This example demonstrates how do I start an android application at boot time. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml.

How do I reduce the startup time of an app?

Use lazy initialization: parts of your application don't need to be initialized during startup. Try to identify those parts and delay the initialization as much as possible — e.g., the user profile might only be necessary on the profile activity and not very useful during startup.

What is Android Auto Launch?

In other words, the auto-launching support allows Android Auto users to automatically run the app every time they connect their mobile devices to the head unit in a car.


2 Answers

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);    }  } 
like image 73
Vishesh Chandra Avatar answered Sep 29 '22 19:09

Vishesh Chandra


If you want to start the app when the tablets starts, you need to listen to the BOOT_COMPLETED action and react to it. BOOT_COMPLETED is a Broadcast Action that is broadcast once, after the system has finished booting. You can listen to this action by creating a BroadcastReceiver that then starts your launch Activity when it receives an intent with the BOOT_COMPLETED action.

Add this permission to your manifest:

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" /> 

Create a Custom BroadcastReceiver in your project:

public class MyBroadCastReceiver extends BroadcastReceiver {      @Override     public void onReceive(Context context, Intent intent) {          if(intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)){             Intent i = new Intent(context, MyActivity.class);             i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);             context.startActivity(i);         }     } }  

Then modify your manifest file by adding the BroadCastReceiver to the Manifest:

<receiver android:name=".MyBroadcastReceiver">     <intent-filter>        <action android:name="android.intent.action.BOOT_COMPLETED" />     </intent-filter> </receiver> 
like image 41
onosendai Avatar answered Sep 29 '22 18:09

onosendai