title: launch activity after phone restart or manually turn power off or on
body: <p><strong>how to launch an launch activity after phone restart or manually turn power off or on</strong></p>
  
  <blockquote>
    <p>i tried below codes but its working for only for unlocking phone but i also want to make it done for boot up manually or restart phone.</p>
  </blockquote>
  
  <p><strong>below is code</strong></p>
  
  <p><strong>application.class</strong></p>
  
  <pre><code>public class MyApplication extends Application {
      public static SharedPreferences preferences;
      public static SharedPreferences.Editor editor;
      public static String PASSWORD = "password";
      public static String IS_SET = "nothing";
      public static String MYPREF = "mypref";
      public String TAG = getClass().getName();
  
      @Override
      protected void attachBaseContext(Context context) {
          super.attachBaseContext(context);
          MultiDex.install(this);
      }
  
      @Override
      public void onCreate() {
          super.onCreate();
          init();  
     }
  
      public void init() {
          preferences = getSharedPreferences(MYPREF, 0);
          editor = preferences.edit();
          startService(new Intent(getBaseContext(), ScreenReceiver.class));
  
      }
  }
  </code></pre>
  
  <p><strong>ScreenReceiver.class</strong></p>
  
  <pre><code>public class ScreenReceiver extends BroadcastReceiver {
       SharedPreferences preferences;
  
          @Override
          public void onReceive(Context context, Intent intent) {
              System.out.println(intent.getAction());
              preferences = context.getSharedPreferences(MYPREF, 0);
              if (preferences != null) {
                  String lock = preferences.getString(IS_SET, null);
                  if (lock != null) {
                      if (lock.equals("passcode")) {
                          gotopasslock(context, intent);
  
                      } else {
                          gotopatternlock(context, intent);
                      }
                  }
              }
          }
          public void gotopasslock(Context context, Intent intent) {
              if (intent.getAction().equals(Intent.ACTION_USER_PRESENT)) {
                  Intent intent1 = new Intent(context, Main.class);
                  intent1.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                  context.startActivity(intent1);
              } else if ("android.intent.action.BOOT_COMPLETED".equals(intent.getAction())) {
                  Intent myStarterIntent = new Intent(context, Main.class);
                  myStarterIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                  context.startActivity(myStarterIntent);
              }
          }
  
          public void gotopatternlock(Context context, Intent intent) {
  
              if (intent.getAction().equals(Intent.ACTION_USER_PRESENT)) {
                  Intent intent1 = new Intent(context, PatternLock.class);
                  intent1.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                  context.startActivity(intent1);
              } else if ("android.intent.action.BOOT_COMPLETED".equals(intent.getAction())) {
                  Intent myStarterIntent = new Intent(context, PatternLock.class);
                  myStarterIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                  context.startActivity(myStarterIntent);
              }
          }
      }
  </code></pre>
  
  <p><strong>Main.class</strong></p>
  
  <pre><code>public class Main extends AppCompatActivity {
      private PinLockView mPinLockView;
      private IndicatorDots mIndicatorDots;
      private String TAG = getClass().getName();
      String oldPassword = "";
      String newPassword = "";
      SharedPreferences preferences;
      SharedPreferences.Editor editor;
  
  
      @Override
      protected void onCreate(@Nullable Bundle savedInstanceState) {
          requestWindowFeature(Window.FEATURE_NO_TITLE);
          getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                  WindowManager.LayoutParams.FLAG_FULLSCREEN);
          // getSupportActionBar().hide();
          super.onCreate(savedInstanceState);
          setContentView(R.layout.main_xml);
  </code></pre>
  
  <p><strong>menifest</strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;manifest xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:tools="http://schemas.android.com/tools"
      package=""&gt;
  
      &lt;uses-permission android:name="android.permission.CAMERA" /&gt;
      &lt;uses-permission android:name="android.permission.DISABLE_KEYGUARD" /&gt;
      &lt;uses-permission android:name="android.permission.INTERNET" /&gt;
      &lt;uses-permission android:name="android.permission.WAKE_LOCK" /&gt;
      &lt;uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" /&gt;
      &lt;uses-permission android:name="android.permission.VIBRATE" /&gt;
      &lt;uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" /&gt;
  
  
      &lt;application
          android:name=".MyApplication"
          android:allowBackup="true"
          android:icon="@mipmap/ic_launcher"
          android:label="@string/app_name"
          android:supportsRtl="true"
          android:theme="@style/AppTheme"
          tools:replace="label"&gt;
  
          &lt;meta-data
              android:name="com.google.android.gms.vision.DEPENDENCIES"
              android:value="face" /&gt;
  
          &lt;activity android:name=".activities.ChooseActivity"&gt;
              &lt;intent-filter&gt;
                  &lt;action android:name="android.intent.action.MAIN" /&gt;
                  &lt;!--   &lt;category android:name="android.intent.category.HOME" /&gt;
                     &lt;category android:name="android.intent.category.DEFAULT" /&gt;--&gt;
                  &lt;category android:name="android.intent.category.LAUNCHER" /&gt;
              &lt;/intent-filter&gt;
          &lt;/activity&gt;
          &lt;activity android:name=".activities.Home" /&gt;
          &lt;activity android:name=".activities.MainActivity" /&gt;
          &lt;activity
              android:name=".activities.Main"
              android:showOnLockScreen="true" /&gt;
          &lt;activity
              android:name=".activities.PatternLock"
              android:showOnLockScreen="true" /&gt;
  
          &lt;!--&lt;receiver
              android:name=".receiver.ScreenReceiver"
              android:permission="android.permission.RECEIVE_BOOT_COMPLETED"&gt;
  
          &lt;/receiver&gt;--&gt;
          &lt;receiver
              android:name=".receiver.ScreenReceiver"
              android:label="ScreenReceiver"&gt;
              &lt;intent-filter&gt;
                  &lt;action android:name="android.intent.action.BOOT_COMPLETED" /&gt;
              &lt;/intent-filter&gt;
              &lt;intent-filter&gt;
                  &lt;action android:name="android.intent.action.USER_PRESENT" /&gt;
              &lt;/intent-filter&gt;
              &lt;intent-filter&gt;
                  &lt;category android:name="android.intent.category.HOME" /&gt;
  
                  &lt;action android:name="android.intent.action.SCREEN_ON" /&gt;
                  &lt;action android:name="android.intent.action.SCREEN_OFF" /&gt;
                  &lt;action android:name="android.intent.action.USER_PRESENT" /&gt;
                  &lt;action android:name="android.intent.action.BOOT_COMPLETED" /&gt;
                  &lt;action android:name="android.intent.action.QUICKBOOT_POWERON" /&gt;
  
                  &lt;category android:name="android.intent.category.DEFAULT" /&gt;
              &lt;/intent-filter&gt;
          &lt;/receiver&gt;
      &lt;/application&gt;
  
  &lt;/manifest&gt;
  </code></pre>