简体   繁体   中英

How to remove lock screen from Android?

I am creating a launcher app and I would like to replace the lock screen with a custom one. How can I remove the current lock screen or somehow make it so that when I press the power button ON/OFF it is my custom lock screen that appears?

This is what I have thought to do. I tried to create a broadcast receiver to determine if Screen is OFF/ON/Boot Completed.

public class LockScreenReceiver extends BroadcastReceiver {

    private Intent mIntent;
    public static boolean isScreenOn = true;

    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
            isScreenOn = false;
            mIntent = new Intent(context, LockScreenAppActivity.class);
            mIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(mIntent);
        } else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
            isScreenOn = true;
            mIntent = new Intent(context, LockScreenAppActivity.class);
            mIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        } else if (intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)) {
            mIntent = new Intent(context, LockScreenAppActivity.class);
            mIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(mIntent);
        }
    }
}

I also created a service

public class MyService extends Service {
    BroadcastReceiver mReceiver;

    @Override
    public IBinder onBind(Intent intent) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public void onCreate() {
        KeyguardManager.KeyguardLock k1;
        KeyguardManager km = (KeyguardManager) getSystemService(KEYGUARD_SERVICE);
        k1 = km.newKeyguardLock("IN");
        k1.disableKeyguard();
        IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);
        filter.addAction(Intent.ACTION_SCREEN_OFF);
        mReceiver = new LockScreenReceiver();
        registerReceiver(mReceiver, filter);
        super.onCreate();
    }

    @Override
    public void onStart(Intent intent, int startId) {
        // TODO Auto-generated method stub
        super.onStart(intent, startId);
    }

    @Override
    public void onDestroy() {
        unregisterReceiver(mReceiver);
        super.onDestroy();
    }
}

The issue I am running in to is that this is not working properly. When I press the power button, the Android unlock screen is present. Then it goes to mine. Is this one of those situations where I should disable lock screen through root, so that only mine will come up?

As it turns out I had all the correct components with exception to the permissions. Once I added the below permissions everything started working.

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

Thank you to Alka Jadav for his reference links. Cheers! Hopefully this will be helpful to someone else.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM