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.
Related
I have an android application that needs to be locked (redirect to the login page) whenever the user presses the lock button. The user can lock the phone while on my application or while on other applications/home screen. In both of these scenarios, I need my application to be locked.
This is working fine in case the phone is locked from an application :
#Override
protected void onStop() {
super.onStop();
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
boolean isScreenOn = pm.isInteractive();
if (!isScreenOn) {
LogUtils.logD(TAG, "Screen is off, Locking the application");
// Lock the application code
}
}
But I am not able to figure out the second scenario when the user has moved away from the application and then locks it. I do not want to start a service or any background thread for this purpose.
Try to add else and return at the end of the code so the activity will still awake in background process
#Override
protected void onStop() {
super.onStop();
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
boolean isScreenOn = pm.isInteractive();
if (!isScreenOn) {
LogUtils.logD(TAG, "Screen is off, Locking the application");
// Lock the application code
} else{
return true;
}
I want to override the lock screen button somehow. The idea is that I want to keep my screen awake for a while, and I did so
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
PowerManager powerManager = (PowerManager) getSystemService(POWER_SERVICE);
PowerManager.WakeLock wakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,
"MyWakelockTag");
wakeLock.acquire();
}
Anyway, I have a flip cover on my phone, and when I put the flip on, my phone is get locked. How could I stop the locking screen event when I put the flip cover on? That's why I need to know if it's possible to stop the lock screen functionality for a while.
For the Flipcover, you have to check the SensorManager and for the Power/Lock Button you can try this -
public class ScreenReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
switch (action) {
case Intent.ACTION_SCREEN_OFF:
BaseActivity.unlockScreen();
break;
case Intent.ACTION_SCREEN_ON:
// and do whatever you need to do here
BaseActivity.clearScreen();
}
}
}
But I'm not sure it'll work
My application sends gps updates to a API. I need my application to run in background all the time. But unfortunately, my application always end at some point while on background. I have read that when the cpu usage of the application is low, the application is will be killed automatically. I don't want this to happen. I already included a partial wake lock on my onCreate method in my application using this code:
powerManager = (PowerManager) getSystemService(Context.POWER_SERVICE);
wakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "MyWakeLock");
Then on pause:
protected void onPause()
{
super.onPause();
onBackground = true;
wakeLock.acquire();
Log.w("OnPause", "OnPause");
}
I really don't know how to prevent my application being killed. I also tried using full wake lock but it is deprecated. Any ideas on how will I keep my application alive on background? I never want my application to be killed while on background. Thanks!
You can not.
Just make sure, you persist anything that should survive after being killed.
For anything that should run in background, even if the app is not shown to the user:
Use a Service and set it into foreground mode.
public class BGService extends Service {
private PowerManager powerManager;
private WakeLock wakeLock;
#Override
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();
powerManager = (PowerManager) getSystemService(Context.POWER_SERVICE);
wakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,
"MyWakeLock");
wakeLock.acquire();
}
call and start service in your main activity
public class SendSMSActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.demo);
startService(new Intent(this, BGService.class));
}
}
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
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();
}
}