Khushbu Shah
Khushbu Shah

Reputation: 1683

Auto start application after boot completed in Android

I want to make an application which has auto start option in its settings. I have made Settings activity in my application which is derived from PreferenceActivity and give CheckBoxPreference for auto start option. If auto start option is enabled my application should start when booting of phone is completed. And if auto start option is disabled then it should not start on boot completed.

To achieve this I have implemented derived class of BroadcastReceiver which receives BOOT_COMPLETED intent, declare receiver in AndroidManifest.xml and also give permission in AndroidManifest.xml.

In application also there is a derived class of Application and start service also from the onCreate method of application derived class. If I declare receiver in AndroidManifest.xml then after booting completed onCreate of my application called and after that onReceive method of BroadcastReceiver called.

Now the problem is that my application starts on boot completed every time whether auto start is enabled or disabled. Is it possible to not start application when auto start is disabled ?

Upvotes: 12

Views: 29397

Answers (5)

Kammaar
Kammaar

Reputation: 1741

You have to add the uses-permission android.permission.RECEIVE_BOOT_COMPLETED in your Manifest.

Upvotes: 6

ksu
ksu

Reputation: 902

The following code works for me:

public class BootCompleteReceiver extends BroadcastReceiver {
    public static final String PREFS_NAME = "MyPrefsFile";  

    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent.getAction().equals("android.intent.action.BOOT_COMPLETED")) {
            Log.d("boot completed", "boot completed caught");
            Boolean autoRestart = false;
            SharedPreferences sp = context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
            autoRestart = sp.getBoolean("autoRestart", false);

            if (autoRestart){

                Log.d("boot completed", "auto restart true");

                Intent i = new Intent(context, WelcomeScreen.class);
                i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                context.startActivity(i);

            }
        }
    }

}

Upvotes: 1

user370305
user370305

Reputation: 109237

I think from Android 3.1 onwards your BroadcastReceiver which receives BOOT_COMPLETED intent its not going to work. User have to in-wake the application by interacted with it.

So, After booting the device all third party application are lying as a stop.

And for currently your application you can use SharedPreferences for Auto-Start your application..

UPDATE: (Only for Android version below 3.1 for higher version it works but you have to user interaction with your application after boot completed on device)

You need to use a BroadcastReceiver with android.intent.action.BOOT_COMPLETED intent.

Add following to your manifest file:

<receiver android:name="App_Receiver">
    <intent-filter>
        <action android:name="android.intent.action.BOOT_COMPLETED" />
        <category android:name="android.intent.category.DEFAULT" />
    </intent-filter>
</receiver>

App_Receiver class implementing BoradcastReciever. Implement the onReceive() method and start your favorite activity from your app.

public void onReceive(Context context, Intent intent) {
    // make sure you receive "BOOT_COMPLETED"
// Here isAutoStartEnabled check sharedPreferences for Auto Start flag
if ( isAutoStartEnabled ) {

    if ((intent.getAction() != null) && (intent.getAction().equals("android.intent.action.BOOT_COMPLETED")))
    {
        // Start the service or activity 
    }
}

Upvotes: 6

R.daneel.olivaw
R.daneel.olivaw

Reputation: 2679

You can use the shared preference to store a Boolean value for isAutoStartEnabled, and check this value in the BroadcastReciver, fire an intent only if it's true.

In your case, the problem is not whether you receive the broadcast but who receives the broadcast. Best of luck..

I hope it helps..

Upvotes: 10

Nishant Shah
Nishant Shah

Reputation: 3442

final SharedPreferences sharedPreferences = getSharedPreferences("Application", MODE_PRIVATE);
        boolean isAutoStartEnabled = sharedPreferences.getBoolean("isAutoStartEnabled", false);

        if ( isAutoStartEnabled ) {
            startActivity(new Intent());
        } 

I hope this helps you

Upvotes: 1

Related Questions