12

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 ?

Khushbu Shah
  • 1,603
  • 3
  • 27
  • 45
  • possible duplicate of [Trying to start a service on boot on Android](http://stackoverflow.com/questions/2784441/trying-to-start-a-service-on-boot-on-android) – Christina Apr 27 '15 at 16:55

5 Answers5

10

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..

R.daneel.olivaw
  • 2,681
  • 21
  • 31
  • but after booting completed, first my onCreate of my application derived class is called before call onReceive of broadcastreceiveer. It means my application is created or started. But I don't want to start application if auto start is disabled. – Khushbu Shah Jan 21 '12 at 05:25
  • Can you post the contents of your manifest file ?, I think the problem may lie there... – R.daneel.olivaw Jan 21 '12 at 05:31
  • Please make sure that in you manifest file the intent filter for BOOT_COMPLETE is not with your Activity tag but with your Receiver tag. – R.daneel.olivaw Jan 21 '12 at 05:35
  • Ya. it is in receiver tag. Application receives Boot completed intent every time perfectly, so there is no problem in manifest file. I don't want to receive it every time. – Khushbu Shah Jan 21 '12 at 05:36
  • Did you check that is the activity that is started on BOOT_COMPLETE is registered to receive the BOOT_COMPLETE broadcast ? – R.daneel.olivaw Jan 21 '12 at 05:39
  • Yes. But it is not the problem. please read the question again. – Khushbu Shah Jan 21 '12 at 05:41
  • In your case, the problem is not whether you receive the broadcast but who receives the broadcast. Best of luck.. – R.daneel.olivaw Jan 21 '12 at 05:52
  • I have exited app if auto start is disabled. But to check flag and receive Boot_completed intent, it is necessary to start application (App automatically started to receive BOOT_COMPLETED intent). – Khushbu Shah Feb 12 '13 at 09:11
6

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

AskNilesh
  • 67,701
  • 16
  • 123
  • 163
Kammaar
  • 1,611
  • 1
  • 14
  • 12
6

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 
    }
}
user370305
  • 108,599
  • 23
  • 164
  • 151
  • Currently I already read flag of auto start using SharedPreference. But how to possible to not start application when auto start is disabled ? Can you give answer in detail ? – Khushbu Shah Jan 21 '12 at 05:29
  • I have written that it is already implemented in application. And it works perfectly, but i don't want to receive intent every time because it starts application every time. Is it possible to not receive boot completed intent at every time ? – Khushbu Shah Jan 21 '12 at 05:40
  • 1
    No, If you register any broadcast receiver then when any action make on device for that broadcast then you have it, You can't control after register it.. Just use on your condition.. – user370305 Jan 21 '12 at 05:42
  • Use files for save settings. And check them in your Auto Start Recever. – XXX Jan 21 '12 at 09:26
  • @user370305 The "update" is a bit misleading as it doesn't mentioned whether it also makes it work on Android 3.0+ or only Android 2.x. I suggest you clarify it in the answer, otherwise users might think the update solves what you mentioned in your first sentence of your answer. Cheers. – Mathias Conradt Jun 21 '12 at 02:27
1

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);

            }
        }
    }

}
Edric
  • 24,639
  • 13
  • 81
  • 91
ksu
  • 882
  • 1
  • 11
  • 17
1
final SharedPreferences sharedPreferences = getSharedPreferences("Application", MODE_PRIVATE);
        boolean isAutoStartEnabled = sharedPreferences.getBoolean("isAutoStartEnabled", false);

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

I hope this helps you

Nishant Shah
  • 3,442
  • 5
  • 25
  • 34
  • In application there is service also which starts in background when application is created, there is not only activity in application. – Khushbu Shah Jan 21 '12 at 05:34
  • From Settings when you deselect the checkbox, In SharedPreference set "isAutoStartEnabled" value to false otherwise make it true. And In Boot Complete check the "isAutoStartEnabled" value from SharedPreference. – Nishant Shah Jan 21 '12 at 05:45