Creating custom LockScreen in android [closed]
Asked Answered
C

5

16

I am developing custom lockscreen app.its working fine in below 4.0 but above 4.0,when we press home button the app stops.is there any solution for this no apps will stop when pressing home button untill unlocking the screen.(like go locker app)

Calpe answered 24/2, 2014 at 9:20 Comment(3)
Sorry, cant answer your question but i'm curious about how you made a custom lockscreen, haven't found any tutorial on making one, only on making a custom launcher ^^ Any suggestions?Bicentenary
@Bicentenary so how to develop that custom launcher like go locker..Calpe
@Bicentenary here is link from that i have designed lock screen #16911549Calpe
S
14

Another way to develop a LockScreen App is by using Views, let me explain it.

First of all you can "disable" in some devices the System lock screen by disabling the KEYGUARD:

((KeyguardManager)getSystemService(Activity.KEYGUARD_SERVICE)).newKeyguardLock("IN").disableKeyguard();

You should put this line of code in your Service.

After that you can launch an activity every time the screen goes off:

public class AutoStart extends BroadcastReceiver {

    public void onReceive(Context arg0, Intent arg1) {
        if(arg1.getAction().equals("android.intent.action.SCREEN_OFF")) {
            Intent localIntent = new Intent(arg0, LockScreen.class);
            localIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            localIntent.addFlags(WindowManager.LayoutParams.TYPE_SYSTEM_ERROR);
            arg0.startActivity(localIntent);
        }
    }
}

If you read the documentation for WindowManager.LayoutParams.TYPE_SYSTEM_ERROR it explains that is a type of internal system error windows, appear on top of everything they can. In multiuser systems shows only on the owning user's window.

So now you have an activity on top of everything, but a press in HOME button will exit the activity.

Here is where the Views make their appearance. You can inflate a view from a layout resource and add it to the WindowManager as a TYPE_SYSTEM_ERROR, so will be on top of everything. And since you can control when to remove this View, the best place is in onDestroy of your Activity, because pressing the HOME button will only pause your activity, and the view will still be visible.

public WindowManager winManager;
public RelativeLayout wrapperView;

 @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
 WindowManager.LayoutParams localLayoutParams = new WindowManager.LayoutParams( WindowManager.LayoutParams.TYPE_SYSTEM_ERROR,
                WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE|
                        WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL|
                        WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN,
                PixelFormat.TRANSLUCENT);
        this.winManager = ((WindowManager)getApplicationContext().getSystemService(WINDOW_SERVICE));
        this.wrapperView = new RelativeLayout(getBaseContext());
        getWindow().setAttributes(localLayoutParams);
        View.inflate(this, R.layout.lock_screen, this.wrapperView);
        this.winManager.addView(this.wrapperView, localLayoutParams);
}

 public void onDestroy()
    {
        this.winManager.removeView(this.wrapperView);
        this.wrapperView.removeAllViews();
        super.onDestroy();
    }

To avoid the notification bar of showing I added the flags FLAG_NOT_FOCUSABLE | FLAG_NOT_TOUCH_MODAL | FLAG_LAYOUT_IN_SCREEN to consume all pointer events.

Not forget to add these two lines to your manifest:

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

From here you just need to add the logic of your Lock Screen app to let the user use his smartphone :)

Spitter answered 19/2, 2015 at 10:19 Comment(10)
Thanks Miguel, yet it is incomplete, first of all and most important of all, you didn't mention how to handle square button (which I call previous apps button) beside home button, it is indeed worst problem since it's intent has changed from android 5.0 and is not same as andoird 4x, also have you checked your solution for home button on android 5? does it work there?Hufnagel
@ErfanMowlaei It's working perfectly on Android 5.1, I tested it.Axenic
@Axenic so it handles the AppSwitch button? I mean the square button next to home button.Hufnagel
@ErfanMowlaei Yes, it is handling the button just the way. The code posted above will not prevent any button from doing its work, but the window is over top of all the operations so the operations are never felt. For instance, the square button will show all the recently used apps, if we click on that on back of the window the recent apps are shown but we can't perform any operation on it.Axenic
@Axenic well that's a bad thing, a lock screen should completely block that key to prevent showing recent apps.Hufnagel
@ErfanMowlaei there is no official way of doing this, but my approach can achieved it. I think Cover ( play.google.com/store/apps/… ) does it the same way ;)Spitter
what is the service used for? How are you using it for the lock screen?Gnarl
thing about SCREEN_OFF is that, it will also be triggered on some ways. One example is when you answered a call and you place it near your ears. The screen will automatically turn off. Hence, your lockscreen will be started. BTW, how is this compared with "android.intent.action.USER_PRESENT"Helicline
@ Miguel ..... Everything works but the App(play.google.com/store/apps/…) you mentioned on sliding they are killing the activity and its not left in background, How to achieve that ? .... Any Ideas ?Engleman
that app is not there anymore :( what happend Miguel? did google cancel it? any particular reason?Raising
B
0

A custom launcher is basically an app (you can make it behave like a grid, list, implement your own drag and drop etc) then, you only need to add these lines to the intent filter of the main activity, with this done, after you install your app and press the home button your app will appear in the list of available homescreens.

<category android:name="android.intent.category.HOME" />
<category android:name="android.intent.category.DEFAULT" />

What i cant find is a way to replace the lock screen, and hacks like disabling the lock screen on the phone and using an activity in a custom launcher isn't actually replacing the lockscreen ^^

Bicentenary answered 24/2, 2014 at 9:47 Comment(0)
E
0

You can use the below method to disable the Home key in android :

@Override
public void onAttachedToWindow() {
    this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD_DIALOG);
    super.onAttachedToWindow();
}
Engel answered 7/8, 2014 at 13:28 Comment(0)
C
0

I am developing on a Samsung Galaxy S4 5.0 and what worked for me was simply changing getWindow().setFlags(..) to getWindow().addFlags(..)

Cryotherapy answered 23/5, 2015 at 19:16 Comment(0)
G
0

I think first of all you should ask yourself if you really want to hijack the home key. Sometimes you may want it. But I think placing the app on the Android lock screen, letting the home key act normally and letting the underlying Android lock screen take care of password-protecting the device is what you actually want in a lot of cases (unless you want to change the way this is done by default).
Bottom line, letting an app be displayed on the Android lock screen comes pretty close to writing your own custom lock screen. And is decidedly easier since you don't have to manage passwords yourself. Not to mention it's safer and more reliable since you don't hijack the home key.
I did it like this and it works very well. You can see the details here:

show web site on Android lock screen

The question is about displaying a website on the lock screen, since that's what I was interested in, but the answer is more general, it works with any app. You can see here an app that's on Google Play and has been written like this:

https://play.google.com/store/apps/details?id=com.a50webs.intelnav.worldtime

Gyasi answered 5/7, 2017 at 7:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.