title: Android How to use checkbox for enable/disable start app on device boot?
body: <p>i want to add checkbox to start my app on device boot </p>
  
  <p>This is what i tried :</p>
  
  <p>Step 1: Set the permission in AndroidManifest.xml</p>
  
  <pre><code>&lt;uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" /&gt;
  </code></pre>
  
  <p>Step 2: Add this intent filter in receiver</p>
  
  <pre><code>&lt;receiver android:name=".BootReceiver"&gt;
      &lt;intent-filter &gt;
          &lt;action android:name="android.intent.action.BOOT_COMPLETED"/&gt;
      &lt;/intent-filter&gt;
  &lt;/receiver&gt;
  </code></pre>
  
  <p>Step 3: in BootReceiver.java</p>
  
  <pre><code>package com.example.myapp;
  
  import android.content.BroadcastReceiver;
  import android.content.Context;
  import android.content.Intent;
  import static com.example.myapp.MainActivity.Global.*;
  
  public class BootReceiver extends BroadcastReceiver {
  
      @Override
      public void onReceive(Context context, Intent intent) {
          if (preferences.getBoolean( "boot", false )) {
              Intent myIntent = new Intent( context, SplashScreen.class );
              myIntent.addFlags( Intent.FLAG_ACTIVITY_NEW_TASK );
              context.startActivity( myIntent );
          }
      }
  }
  </code></pre>
  
  <ol start="4">
  <li>In MainActivity.java Before onCreat:</li>
  </ol>
  
  <blockquote>
  <pre><code>public static class Global {
  public static SharedPreferences preferences;
  }
  CheckBox AutoBoot;
  </code></pre>
  </blockquote>
  
  <ol start="5">
  <li>In MainActivity.java in onCreat:</li>
  </ol>
  
  <blockquote>
  <pre><code>preferences = PreferenceManager.getDefaultSharedPreferences( this );
          final SharedPreferences.Editor editor = preferences.edit();
  
  if (preferences.getBoolean( "boot", false )) {
  AutoBoot.setChecked( true );
  } else {
  AutoBoot.setChecked( false );
  }
  
  AutoBoot.setOnCheckedChangeListener( new CompoundButton.OnCheckedChangeListener() {
              @Override
              public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                  if (isChecked) {
                      editor.putBoolean( "boot", true );
                      editor.apply();
                  } else {
                      editor.putBoolean( "boot", false );
                      editor.apply();
                  }
              }
          } );
  </code></pre>
  </blockquote>