Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I start my app when the phone starts on Android?

I tried using the sample code in this tutorial but it seems outdated and it did not work. So what changes do I have to make and to what files to have my app start automatically when Android finishes booting up?

like image 754
Poojan Avatar asked Jun 17 '11 21:06

Poojan


People also ask

How do I make apps run on 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 make apps start automatically?

With the file location open, press the Windows logo key + R, type shell:startup, then select OK. This opens the Startup folder. Copy and paste the shortcut to the app from the file location to the Startup folder.

What is app auto-launch?

Use Auto-launch to help you identify and prohibit unwanted app activities. Open Optimiser, touch > App launch, and you can: Automatically manage apps: Enable Manage all automatically or toggle on the switches for individual apps.


2 Answers

First, you need the permission in your AndroidManifest.xml:

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

Also, in yourAndroidManifest.xml, define your service and listen for the BOOT_COMPLETED action:

<service android:name=".MyService" android:label="My Service">     <intent-filter>         <action android:name="com.myapp.MyService" />     </intent-filter> </service>  <receiver     android:name=".receiver.StartMyServiceAtBootReceiver"     android:label="StartMyServiceAtBootReceiver">     <intent-filter>         <action android:name="android.intent.action.BOOT_COMPLETED" />     </intent-filter> </receiver> 

Then you need to define the receiver that will get the BOOT_COMPLETED action and start your service.

public class StartMyServiceAtBootReceiver extends BroadcastReceiver {      @Override     public void onReceive(Context context, Intent intent) {         if (Intent.ACTION_BOOT_COMPLETED.equals(intent.getAction())) {             Intent serviceIntent = new Intent(context, MyService.class);             context.startService(serviceIntent);         }     } } 

And now your service should be running when the phone starts up.

like image 105
Sean Schulte Avatar answered Sep 20 '22 17:09

Sean Schulte


This is how to make an activity start running after android device reboot:

Insert this code in your AndroidManifest.xml file, within the <application> element (not within the <activity> element):

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />  <receiver     android:enabled="true"     android:exported="true"      android:name="yourpackage.yourActivityRunOnStartup"     android:permission="android.permission.RECEIVE_BOOT_COMPLETED">      <intent-filter>         <action android:name="android.intent.action.BOOT_COMPLETED" />         <action android:name="android.intent.action.QUICKBOOT_POWERON" />         <category android:name="android.intent.category.DEFAULT" />     </intent-filter>  </receiver> 

Then create a new class yourActivityRunOnStartup (matching the android:name specified for the <receiver> element in the manifest):

package yourpackage;  import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent;  public class yourActivityRunOnStartup extends BroadcastReceiver {      @Override     public void onReceive(Context context, Intent intent) {         if (intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)) {             Intent i = new Intent(context, MainActivity.class);             i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);             context.startActivity(i);         }     }  } 

Note: The call i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); is important because the activity is launched from a context outside the activity. Without this, the activity will not start.

Also, the values android:enabled, android:exported and android:permission in the <receiver> tag do not seem mandatory. The app receives the event without these values. See the example here.

like image 44
thrylos Avatar answered Sep 19 '22 17:09

thrylos