Josh Elias
Josh Elias

Reputation: 3290

Is there a reliable way to start an activity on bootup in Android?

I have tried using:

<action android:name="android.intent.action.BOOT_COMPLETED" />

But that is a woefully inconsistent way to start an activity on start up on the Android platform.

Is there anything GUARANTEED to start my app on start up?

Upvotes: 1

Views: 336

Answers (2)

John Byrne
John Byrne

Reputation: 41

I'm using Android Studio.

I am also creating a Service that is to run at Boot Complete and I also initially had a wholly bunch of inconsistent and unreliable results, where my Application (Service) seemed to not even try to start.

Sometimes it would work perfectly and then when I update the project with some other non-BroadcastReciever related updates, it appeared to break the Auto-Startup.

What I found was that when I went to run my app and monitored the logcat, I found that on some occasions, the Android OS Broadcast Services could take a whole 30 seconds to update with the updated Package details.

For example, from logcat...

03-03 17:08:26.757     681-1780/? D/PackageBroadcastService﹕ Received broadcast action=android.intent.action.PACKAGE_REMOVED and uri=com.test.jbyrne.anotherautostart
#### Note the time stamp between the PACKAGE_REMOVED (above) and PACKAGE_ADDED (below) ####
03-03 17:08:26.825      681-681/? I/ConfigFetchService﹕ PackageReceiver: Intent { act=android.intent.action.PACKAGE_ADDED dat=package:com.test.jbyrne.anotherautostart flg=0x8000010 cmp=com.google.android.gms/.config.ConfigFetchService$PackageReceiver (has extras) }
03-03 17:08:26.833      681-681/? I/ConfigFetchService﹕ onStartCommand Intent { act=android.intent.action.PACKAGE_ADDED dat=package:com.test.jbyrne.anotherautostart cmp=com.google.android.gms/.config.ConfigFetchService (has extras) }
03-03 17:08:26.841      707-707/? D/PackageAddedReceiver﹕ package added com.test.jbyrne.anotherautostart
03-03 17:08:26.873     681-1816/? D/PackageBroadcastService﹕ Received broadcast action=android.intent.action.PACKAGE_ADDED and uri=com.test.jbyrne.anotherautostart
03-03 17:08:26.877    1419-1818/? I/UpdateIcingCorporaServi﹕ Updating corpora: APPS=com.test.jbyrne.anotherautostart, CONTACTS=MAYBE
03-03 17:08:27.005     681-1816/? D/PackageBroadcastService﹕ Received broadcast action=android.intent.action.PACKAGE_REPLACED and uri=com.test.jbyrne.anotherautostart

And then I changed something unrelated and when I next updated the Package...

03-03 17:10:29.866    2454-3515/? D/PackageBroadcastService﹕ Received broadcast action=android.intent.action.PACKAGE_REMOVED and uri=com.test.jbyrne.anotherautostart
#### This time, it takes over 30 seconds to Update the Broadcast Services!!! ####
03-03 17:11:00.190    2454-2454/? I/ConfigFetchService﹕ PackageReceiver: Intent { act=android.intent.action.PACKAGE_ADDED dat=package:com.test.jbyrne.anotherautostart flg=0x8000010 cmp=com.google.android.gms/.config.ConfigFetchService$PackageReceiver (has extras) }
03-03 17:11:00.194    2454-2454/? I/ConfigFetchService﹕ onStartCommand Intent { act=android.intent.action.PACKAGE_ADDED dat=package:com.test.jbyrne.anotherautostart cmp=com.google.android.gms/.config.ConfigFetchService (has extras) }
03-03 17:11:00.206    2404-2404/? D/PackageAddedReceiver﹕ package added com.test.jbyrne.anotherautostart
03-03 17:11:00.222    2454-3587/? D/PackageBroadcastService﹕ Received broadcast action=android.intent.action.PACKAGE_ADDED and uri=com.test.jbyrne.anotherautostart
03-03 17:11:00.246    3168-3589/? I/UpdateIcingCorporaServi﹕ Updating corpora: APPS=com.test.jbyrne.anotherautostart, CONTACTS=MAYBE
03-03 17:11:00.326    2454-3601/? D/PackageBroadcastService﹕ Received broadcast action=android.intent.action.PACKAGE_REPLACED and uri=com.test.jbyrne.anotherautostart

I spent many hours trying to figure out why this was so unreliably and it ultimately came down to the fact that I was rebooting my device too early, before the Android OS Broadcast Services were updated.

Try waiting for a minute before you reboot your device to test your update, to insure the various Broadcast Services are updated. Or Monitor the logcat for the

android.intent.action.PACKAGE_ADDED

lines.

I hope this helps!!

Kind Regards,

John

Upvotes: 0

Goran Horia Mihail
Goran Horia Mihail

Reputation: 3645

You will have to declare the permission in manifest:

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

Then catch the intent with a broadcast receiver:

In the manifest:

<receiver
    android:name="com.mypackagename.MyBroadcastReceiver"
    android:permission="android.permission.RECEIVE_BOOT_COMPLETED"
    android:exported="false" >
    <intent-filter>
        <action android:name="android.intent.action.BOOT_COMPLETED" />
        <category android:name="android.intent.category.DEFAULT" />
    </intent-filter>
</receiver>

In MyBroadcastReceiver.java:

public class MyBroadcastReceiver extends BroadcastReceiver
{
    @Override
    public void onReceive(Context context, Intent intent)
    {
        Intent i = new Intent(context, YourClass.class);
        context.startActivity(i);
    }
}

Upvotes: 1

Related Questions