android: which wake_lock should we be using - android

The android documentation says that I should be using
WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
instead of
PowerManager.SCREEN_DIM_WAKE_LOCK. The link is here
But when I try to use it I get an exception saying it is an invalid lock level. I'm using API level 18, here is the code that fails.
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
PowerManager.WakeLock wl = pm.newWakeLock(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON, "My Tag");
Has anybody tried this? Does it work?

As per the API documentation one should be using the Window.addFlags() for keeping the screen on.
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

The code below is working fine, check this
import android.os.PowerManager;
public class MyActivity extends Activity {
protected PowerManager.WakeLock mWakeLock;
#Override
public void onCreate(final Bundle icicle) {
setContentView(R.layout.main);
/* This code together with the one in onDestroy()
* will make the screen be always on until this Activity gets destroyed. */
final PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
this.mWakeLock = pm.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK, "My Tag");
this.mWakeLock.acquire();
}
#Override
public void onDestroy() {
this.mWakeLock.release();
super.onDestroy();
}
}
Don't forget to give permission in manifest file
<uses-permission android:name="android.permission.WAKE_LOCK" />

Related

What permission is SHOW_ON_LOCK_SCREEN in MIUI? And how to programatically enable it?

I simply want to show an Activity in Lock Screen but because of this unknown or special permission called SHOW_ON_LOCK_SCREEN in MIUI, I can't make it work.
I've tried the following code but no luck.
<uses-permission android:name="android.permission.WAKE_LOCK" />
public class FloatingWindow extends AppCompatActivity {
PowerManager powerManager;
PowerManager.WakeLock wakeLock;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
//First Attempt
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON|
WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD|
WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED|
WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
setContentView(R.layout.floating_window);
//Second Attempt
powerManager = (PowerManager) getSystemService(POWER_SERVICE);
wakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, String.valueOf(getApplicationContext()));
wakeLock.acquire();
}
#Override
public void onDestroy(){
super.onDestroy();
wakeLock.release();
}
}
Here's a thing, It works on other android phone except MIUI. I've tried manually enabling SHOW_ON_LOCK_SCREEN and works.
What exactly is SHOW_ON_LOCK_SCREEN? I can't find it in Android documentation, How are we supposed to programatically enable it?
I noticed that some of my app successfully enable it without me having manually enable it.

how to keep app running when screen off or open another app?

How to keep app running when screen off or open another app,just like some running track app . when user running they can also track user's pace.
I don't know how to start with.
You can use android.permission.WAKE_LOCK permission.
First add this permission in manifest.
<uses-permission android:name="android.permission.WAKE_LOCK" />
In your activity :
import android.os.PowerManager;
public class MyActivity extends Activity {
protected PowerManager.WakeLock mWakeLock;
#Override
public void onCreate(final Bundle icicle) {
setContentView(R.layout.main);
/* This code together with the one in onDestroy()
* will make the screen be always on until this Activity gets destroyed. */
final PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
this.mWakeLock = pm.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK, "My Tag");
this.mWakeLock.acquire();
}
#Override
public void onDestroy() {
this.mWakeLock.release();
super.onDestroy();
}
}
Or you can add this code before setContentView activity. like this:
#Override
public void onCreate(final Bundle icicle) {
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
setContentView(R.layout.main);
}

How to keep CPU running while closing screen?

There is a time-consuming task (a Thread) in Fragment. It works fine. But, when I close the screen, I see the CPU not work so that the task cannot work fine.
I have use PowerManager in Activity, but not work Fragment too.
Also add <uses-permission android:name="android.permission.WAKE_LOCK" />
#Override
protected void onResume() {
super.onResume();
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
PowerManager.WakeLock wakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "CPUKeepRunning");
wakeLock.acquire();
}
#Override
protected void onDestroy() {
super.onDestroy();
if (wakeLock != null && wakeLock.isHeld()) {
wakeLock.release();
wakeLock = null;
}
}
I have saw the CPU
Run your code through a Service, if you're running it in an Activity it will stop once the activity goes in the background. Use Services instead.
http://developer.android.com/training/run-background-service/create-service.html
http://developer.android.com/guide/components/services.html

How to keep screen always on android [duplicate]

This question already has answers here:
How do I keep the screen on in my App? [duplicate]
(11 answers)
Closed 8 years ago.
I am Building a android application in which i want screen(activity) not locked after some time means application screen always on.How can i do this in my application for all screen keep always on.means no screen ssaver no lock screen overcome in my application.
try below code:-
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
or
import android.os.PowerManager;
public class MyActivity extends Activity {
protected PowerManager.WakeLock mWakeLock;
/** Called when the activity is first created. */
#Override
public void onCreate(final Bundle icicle) {
setContentView(R.layout.main);
/* This code together with the one in onDestroy()
* will make the screen be always on until this Activity gets destroyed. */
final PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
this.mWakeLock = pm.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK, "My Tag");
this.mWakeLock.acquire();
}
#Override
public void onDestroy() {
this.mWakeLock.release();
super.onDestroy();
}
}
for more info see below links:-
How do I keep the screen on in my App?
Android disable screen timeout while app is running
simply add this line in onCreate method
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
It will keeps screen in active state.
Do something like below,
protected PowerManager.WakeLock wakelock;
/** Called when the activity is first created. */
#Override
public void onCreate(final Bundle savedInstances) {
setContentView(R.layout.main);
final PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
this.wakelock= pm.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK, "My Tag");
this.wakelock.acquire();
}
#Override
public void onDestroy() {
this.wakelock.release();
super.onDestroy();
}
don't forget to add the following permission in manifest file :
<uses-permission android:name="android.permission.WAKE_LOCK" />
These are the ways to keep the screen on:
http://developer.android.com/training/scheduling/wakelock.html

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();
}
}

Categories

Resources