You can try overriding KeyguardManager
KeyguardManager.KeyguardLock key;
KeyguardManager km=(KeyguardManager)getSystemService(KEYGUARD_SERVICE);
//depreciated
key=km.newKeyguardLock("IN");
You must insert this in a service.Go as something like:
public class LockService extends Service{
BroadcastReceiver receiver;
@override
@SuppressWarnings("deprecation")
public void onCreate(){
KeyguardManager.KeyguardLock key;
KeyguardManager km=(KeyguardManager)getSystemService(KEYGUARD_SERVICE);
//depreciated
key=km.newKeyguardLock("IN");
IntentFilter filter=new IntentFilter(Intent.ACTION_SCREEN_ON);
filter.addAction(Intent.ACTION_SCREEN_OFF);
filter.addAction(Intent.ACTION_BOOT_COMPLETED);
receiver=new LockscreenReceiver();
registerReceiver(receiver,filter);
super.onCreate();
}
And then on LockscreenReceiver, you must implement this action:
public class LockscreenReceiver extends BroadcastReceiver{
@Override
public void onReceive(Context context,Intent intent){
String action=intent.getAction();
//if the screen was recently enabled, do this:
//If the screen was just turned on or it just booted up, start your Lock Activity
if(action.equals(Intent.ACTION_SCREEN_OFF) || action.equals(Intent.ACTION_BOOT_COMPLETED))
{
Intent i = new Intent(context, MainActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
}
}
}
afterwards, you must register or call service in MainActivity
startService(new Intent(this,LockscreenService.class));
To see this in action, go to https://github.com/thomasvidas/Simple-Lockscreen