66

I'm not sure how to autostart an android application after the android emulator completes its booting. Does anyone have any code snippets that will help me?

3
  • @AdamC You are wrong - see Prashast's answer. Commented Jun 29, 2009 at 5:42
  • @Rajapandian Owner should accept an answer if it is a working solution, or mention your expectations in the comments. It would be helpful for others. Commented Jul 24, 2014 at 5:18
  • karanbalkar.com/2014/01/… Commented Jan 29, 2015 at 9:19

4 Answers 4

54

You have to add a manifest permission entry:

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

(of course you should list all other permissions that your app uses).

Then, implement BroadcastReceiver class, it should be simple and fast executable. The best approach is to set an alarm in this receiver to wake up your service (if it's not necessary to keep it running ale the time as Prahast wrote).

public class BootUpReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); PendingIntent pi = PendingIntent.getService(context, 0, new Intent(context, MyService.class), PendingIntent.FLAG_UPDATE_CURRENT); am.setInexactRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + interval, interval, pi); } } 

Then, add a Receiver class to your manifest file:

<receiver android:enabled="true" android:name=".receivers.BootUpReceiver" android:permission="android.permission.RECEIVE_BOOT_COMPLETED"> <intent-filter> <action android:name="android.intent.action.BOOT_COMPLETED" /> <category android:name="android.intent.category.DEFAULT" /> </intent-filter> </receiver> 
Sign up to request clarification or add additional context in comments.

3 Comments

What is the interval?
the ".receivers" in this line <receiver android:enabled="true" android:name=".receivers.BootUpReceiver" gives error "Unresolvable package receivers"
@asmgx - if you created class "BootUpReceiver" in your app's src root folder, then change that to ... android:name="BootUpReceiver". Or maybe need "." before the class name. I think Krzysztof has his in a subfolder named "receivers".
31

Edit your AndroidManifest.xml to add RECEIVE_BOOT_COMPLETED permission

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

Edit your AndroidManifest.xml application-part for below Permission

<receiver android:enabled="true" android:name=".BootUpReceiver" android:permission="android.permission.RECEIVE_BOOT_COMPLETED"> <intent-filter> <action android:name="android.intent.action.BOOT_COMPLETED" /> <category android:name="android.intent.category.DEFAULT" /> </intent-filter> </receiver> 

Now write below in Activity.

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

1 Comment

Work without android:permission="android.permission.RECEIVE_BOOT_COMPLETED" line only!!!
13

If by autostart you mean auto start on phone bootup then you should register a BroadcastReceiver for the BOOT_COMPLETED Intent. Android systems broadcasts that intent once boot is completed.

Once you receive that intent you can launch a Service that can do whatever you want to do.

Keep note though that having a Service running all the time on the phone is generally a bad idea as it eats up system resources even when it is idle. You should launch your Service / application only when needed and then stop it when not required.

Comments

2

I always get in here, for this topic. I'll put my code in here so i (or other) can use it next time. (Phew hate to search into my repository code).

Add the permission:

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

Add receiver and service:

<receiver android:enabled="true" android:name=".BootUpReceiver" android:permission="android.permission.RECEIVE_BOOT_COMPLETED"> <intent-filter> <action android:name="android.intent.action.BOOT_COMPLETED" /> <category android:name="android.intent.category.DEFAULT" /> </intent-filter> </receiver> <service android:name="Launcher" /> 

Create class Launcher:

public class Launcher extends Service { @Nullable @Override public IBinder onBind(Intent intent) { return null; } @Override public int onStartCommand(Intent intent, int flags, int startId) { new AsyncTask<Service, Void, Service>() { @Override protected Service doInBackground(Service... params) { Service service = params[0]; PackageManager pm = service.getPackageManager(); try { Intent target = pm.getLaunchIntentForPackage("your.package.id"); if (target != null) { service.startActivity(target); synchronized (this) { wait(3000); } } else { throw new ActivityNotFoundException(); } } catch (ActivityNotFoundException | InterruptedException ignored) { } return service; } @Override protected void onPostExecute(Service service) { service.stopSelf(); } }.execute(this); return START_STICKY; } } 

Create class BootUpReceiver to do action after android reboot.

For example launch MainActivity:

public class BootUpReceiver extends BroadcastReceiver{ @Override public void onReceive(Context context, Intent intent) { Intent target = new Intent(context, MainActivity.class); target.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); context.startActivity(target); } } 

4 Comments

error on <receiver android:enabled="true" android:name=".BootUpReceiver" Unresolvable class BootUpReceiver
@asmgx: I've update the answer. Forgot to put it in there.
class Launcher are not used
@letroll sorry for long respond. I am inactive for longtime. I believe Launcher class being used in the manifest as a service. CMIIW. I have not tried this anymore, i use flutter now.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.