How to lock a device in android - android

How to lock a android device or screen??. Here is my code.
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
context = this;
Button b = new Button(context);
b = (Button)findViewById(R.id.button1);
b.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
System.out.println("inside button");
PowerManager pManager = (PowerManager)
getSystemService(Context.POWER_SERVICE);
PowerManager.WakeLock wl = pManager.newWakeLock(PowerManager.
PARTIAL_WAKE_LOCK, "lock screen");
wl.acquire();
wl.release();
}
});
}
Here I am using a button. when user clicks on then the screen should be lock. But this code is not working. Is have to use BraodCastRecievr??. I included this permmission in manifest
<uses-permission android:name="android.permission.WAKE_LOCK" />
But also not working

KeyguardManager keyguardManager = (KeyguardManager)getSystemService(Activity.KEYGUARD_SERVICE);
KeyguardLock lock = keyguardManager.newKeyguardLock(KEYGUARD_SERVICE);
For locking the screen use,
lock.reenableKeyguard();
and for disabling the lock use,
lock.disableKeyguard()
This will just hide the lock screen and will display if any incoming call or any such event is happening, it will display that screen. It wont display the lock screen.
When running the application, to disable from going to the lock state, just use
setKeepScreenOn() is set to true. or use the xml attribute android:keepScreenOn="true"
Another way to prevent the device to go to the sleep or lock mode when the application is running is set this api to true - setKeepScreenOn()
And of course we need to give permission android.permission.DISABLE_KEYGUARD

Try out the example shown here:
http://developer.android.com/reference/android/os/PowerManager.html
Lock the android device programmatically

Related

Activity does not start until the screen is turned on

My service starts an Activity like this:
Intent intent = new Intent(context, CallMonitor.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
this happens in CallMonitor.onCreate():
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_ALLOW_LOCK_WHILE_SCREEN_ON);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
On most devices, the activity (called up from service) wakes up the device, turns on the screen and is displayed.
But - for example - on Galaxy Tab 4, the activity is only called if the screen is already switched on.
If the screen is switched off and the service calls up the activity, it is displayed with a delay - It will be displayed immediatly after turning the screen on.
There is also a voice output in the activity. When the Galaxy S4 is switched off, it will not be played back - but immediatly after turning the screen on again.
Any suggestions?
I don't want to use WakeLock!
It looks like that's a device limitation and I don't know how you could work around that without using a WakeLock. We have a really old piece of code which you could modify to your needs which pretty much always worked for us:
public static void bringToFront() {
try {
PowerManager pm = (PowerManager) MainActivity.getAppContext().getSystemService(Context.POWER_SERVICE);
PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP | PowerManager.ON_AFTER_RELEASE, "ALARM");
wl.acquire();
KeyguardManager keyguardManager = (KeyguardManager) MainActivity.getAppContext().getSystemService(Activity.KEYGUARD_SERVICE);
KeyguardManager.KeyguardLock lock = keyguardManager.newKeyguardLock(KEYGUARD_SERVICE);
lock.disableKeyguard();
if (MainActivity.getAppActivity() != null) {
MainActivity.getAppActivity().getWindow().addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
}
} catch(Exception e){
e.printStackTrace();
Log.w(TAG, "bringToFront Err: "+e.toString());
}
}

Android app launches on opening lockscreen

I want my app or a part (any activity) of the app to be launched on opening the lockscreen, i.e, when we unlock the keypad of our phone, my activity should start running.. Thanks in advance. :D enter image description here
Try this one
A way to get unlock event in android?
It's using ACTION_USER_PRESENT receiver, but stil could help you.
To unlock the keyguard.
add this permission <uses-permission android:name="android.permission.DISABLE_KEYGUARD" />
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_layout);
// createWakeLocks();
KeyguardManager kgm = (KeyguardManager) getSystemService(Context.KEYGUARD_SERVICE);
boolean isKeyguardUp = kgm.inKeyguardRestrictedInputMode();
KeyguardLock kgl = kgm.newKeyguardLock("QuickPopup");
if (isKeyguardUp) {
kgl.disableKeyguard();
isKeyguardUp = false;
}
getWindow().addFlags(
WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
getWindow().addFlags(
WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
// getWindow()
// .addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
getWindow()
.addFlags(WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
//wakeDevice();
}

how to wake lock using powermanager class in android

hi i am trying to wake lock for a particular amount of time but i am not getting the result
i am using power manager class but i am not getting any result.
i am using this code to wake lock
PowerManager.WakeLock wl;
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "My Tag");
Button b=(Button)findViewById(R.id.button1);
b.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View arg0) {
wl.acquire();
wl.release();
}});
}
here i am trying to invoke lock when i click button but its not working.
i need to lock when the particular time out using broad cast receiver please suggest me
Thanks in advance
you should:
hold a wakelock at first;
then do something. At the end;
release the wakelock.
so you should test it by:
Button b1=(Button)findViewById(R.id.button1);
b1.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View arg0) {
wl.acquire();
}});
Button b2=(Button)findViewById(R.id.button2);
b2.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View arg0) {
wl.release();
}});
press button1
lock the screen for a test
press button2
what's more:
PARTIAL_WAKE_LOCK: Ensures that the CPU is running; the screen and keyboard
FULL_WAKE_LOCK(I think it's you want): Ensures that the screen and keyboard backlight are on at full brightness.
You acquire and them immediatly release the WL. You have only to acquire WL in OnClick event and to handle a task to release it.
I suggest you to use also a mWakeLockAcquired boolean variable to check the WL state.

What should I do as power button do (turn off screen, lock keyboard)?

My goal is make same thing as power button do.
I try PARTIAL_WAKE_LOCK and this is my code..
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, TAG);
wl.acquire();
after that I open WAKE_LOCK permission in AndroidManifest.xml
<uses-permission android:name="android.permission.WAKE_LOCK" />
But when I launch my application not thing happen.
Do I miss something ?
Thanks
From your question I'm not totally sure whether you:
Try to turn off the device by pressing a button
Want to make sure the device will not go to sleep (as this is what a WakeLock is supposed to help you with). It can't prevent user interaction though (just tested on HTC Desire).
For 1) You can't lock the device or turn it's power off without being signed as a system app, as written here: http://groups.google.com/group/android-developers/browse_thread/thread/36399f15724ac3ae/98d93e53616cf495?show_docid=98d93e53616cf495
For 2) You can prevent the device from sleeping using WakeLock, sample code can read like this:
/**
* Called when the activity is first created.
*/
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
wl = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK, TAG);
}
// Call me from a button
public void doLock(View view) {
Log.d(TAG, "Lock");
if (!wl.isHeld()) {
Log.d(TAG, "acquire");
wl.acquire();
} else {
Log.d(TAG, "release");
wl.release();
}
}

how to show popup Activity or Dialog when phone is locked?

I am trying to show a activity or a dialog when the phone is locked.
I have tried using a WakeLock but it did not work and I can only see the activity once my phone is unlocked?
What is the proper way to do this?
To show activity without dismissing the keyguard try this:
getWindow().addFlags(WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
setContentView();
You should use the KeyGuardManager to unlock the device automatically and then acquire your Wake Lock.
KeyguardManager kgm = (KeyguardManager)getSystemService(Context.KEYGUARD_SERVICE);
boolean isKeyguardUp = kgm.inKeyguardRestrictedInputMode();
KeyguardLock kgl = kgm.newKeyguardLock("Your Activity/Service name");
if(isKeyguardUp){
kgl.disableKeyguard();
isKeyguardUp = false;
}
wl.acquire(); //use your wake lock once keyguard is down.
To show a popup on top of a lock screen try this, from my other answer:
AlertDialog alertDialog = new AlertDialog.Builder(context).create();
alertDialog.getWindow().setType(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
alertDialog.show();
To show activity on top of a lock screen, or basically remove the lock screen when activity is starts, try this:
public void onCreate(Bundle savedInstanceState){
getWindow().addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
...
}
Both of those options require api 5+

Categories

Resources