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
Related
I am trying to wake the screen when the screen is off (dark).
I create a class like the following code:
public class ScreenWakeLock {
private static PowerManager.WakeLock WakeLock;
#SuppressLint("Wakelock")
static void acquireCpuWakeLock(Context context) {
Log.i("ScreenWakeLock", "acquireCpuWakeLock");
PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
WakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP,"okTag");
WakeLock.acquire();
}
static void releaseCpuLock() {
if (WakeLock != null) {
WakeLock.release();
WakeLock = null;
}
}
}
When the Screen is off , the App call ScreenWakeLock.acquireCpuWakeLock(getActivity());.
But the Screen didn't wake up. I have seen the acquireCpuWakeLock in the log , I am sure the function acquireCpuWakeLock has been called. I also add the <uses-permission android:name="android.permission.WAKE_LOCK" /> in Manifest.xml.
Why the Screen didn't wake up when I call acquire in Android ?
Did I missing something?
Thanks in advance.
Straight from the PowerManager documentation:
In addition, you can add two more flags, which affect behavior of the
screen only. These flags have no effect when combined with a
PARTIAL_WAKE_LOCK.
Use something else instead of PowerManager.PARTIAL_WAKE_LOCK. Try SCREEN_DIM_WAKE_LOCK and see if that's good enough for your use case; if not, try SCREEN_BRIGHT_WAKE_LOCK.
If my app is running and I press lock screen button, it will put the app in background.What is the method to check whether onPause() is called by screen lock?.Thanks in advance.
All you have to do is check if the screen is on or not.
#Override
protected void onPause() {
super.onPause();
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
boolean screenOn;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT_WATCH) {
screenOn = pm.isInteractive();
} else {
screenOn = pm.isScreenOn();
}
if (screenOn) {
// Screen is still on, so do your thing here
}
}
You Can Simply Know It By Using This Method
#Override
public void onPause() {
super.onPause(); // Always call the superclass method first
System.out.println("On Pause called");
}
For Keeping The Device Awake while lock screen. Documentation.
Ok in your case you would need Wake_Lock
To use a wake lock, the first step is to add the WAKE_LOCK permission to your application's manifest file:
<uses-permission android:name="android.permission.WAKE_LOCK" />
If your app includes a broadcast receiver that uses a service to do some work, you can manage your wake lock through a WakefulBroadcastReceiver, as described in Using a WakefulBroadcastReceiver. This is the preferred approach. If your app doesn't follow that pattern, here is how you set a wake lock directly:
PowerManager powerManager = (PowerManager) getSystemService(POWER_SERVICE);
Wakelock wakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,
"MyWakelockTag");
wakeLock.acquire();
To release the wake lock, call wakelock.release(). This releases your claim to the CPU. It's important to release a wake lock as soon as your app is finished using it to avoid draining the battery.
DO this after setting powermanager.
boolean screenOn;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT_WATCH) {
screenOn = powerManager.isInteractive();
} else {
screenOn = powerManager.isScreenOn();
}
if (screenOn) {
// Screen is still on, so do your thing here
}
You just want to know when onPause is called? You could override the super function and add logging to the function:
#Override
public void onPause() {
super.onPause();
System.out.println("On Pause called");
}
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" />
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));
}
}
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();
}
}