Search code examples
androidbroadcastreceiver

how to make my app open when the screen turns on in android


When I open my screnn I want to make my app open. It is password application and I want that EVERY time that the screnn open the app will ask the password.

I made all the code that i need for the app but it is not working and the app does not open. I used RECEIVE_BOOT_COMPLETED:

<user-permission android:name="android.intent.category.RECEIVE_BOOT_COMPLETED" />
<application
  ...
   <receiver
      android:name="com.test.receiver"
      android:enabled="true" >
      <intent-filter>
         <action android:name="android.intent.action.SCREEN_ON" />
         <action android:name="android.intent.action.BOOT_COMPLETED" />
      </intent-filter>
   </receiver>
</application>

and here is the java part:

public class receiver extends BroadcastReceiver
{
   @Override
   public void onReceive(Context context, Intent intent)
   {
      intent = new Intent(context, MainActivity.class);
      startActivity(intent); // context.startActivity(intent); does not work too
   }
}

i will be happy if someone will help me, and if someone can say too me how acn i cancel somehow the home button i will be thanksful a lot :)


Solution

  • Other SO posts suggest you will need to use this code for your app to appear above the lock screen;

    getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON|
        WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD|
        WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED|
        WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
    

    Android activity over default lock screen


    Further SO posts suggest you will need to do something like this so that you can unlock the phone with your application;

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

    mKeyGuardManager = (KeyguardManager) getSystemService(KEYGUARD_SERVICE);
        mLock = mKeyGuardManager.newKeyguardLock("activity_classname");
        mLock.disableKeyguard();
    

    Lock the android device programmatically (Someone says that method is obsolete, but the docs seem to disagree)