i have tried to change the status of the power saver mode from my app i have figured out how to read the status (Through the Power Manager) but how can i change it
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK, "My Tag");
I Guess There is no API in android SDK to support this, Because power saver might be a third party function given by the device manufactures company and they have there own version of "Power Savers" . You can just use the PowerManager to acquire and release wakelocks.
Use wl.acquire(); and make sure you have this permission added to your manifest,
<uses-permission android:name="android.permission.WAKE_LOCK" />
Related
My app takes up too much RAM. I found that placing the accessibility service in a different process will prevent it from being shut down alongside the app. It also apparently has the added benefit that if the app crashes then the accessibility service won't crash along with it.
I tried just adding android:process=":externalProcess" under the accessibility service in the manifest file but that doesn't work. How do I do it? Also one more question, how do I inform the OS that no matter how much RAM the accessibility service is taking it should never be shut down, i.e. highest priority.
<uses-permission android:name="android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS"/>
Request White-list / optimization enabled your app
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
Intent intent = new Intent();
String packageName = getPackageName();
PowerManager pm = (PowerManager) getSystemService(POWER_SERVICE);
if (!pm.isIgnoringBatteryOptimizations(packageName)) {
intent.setAction(Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS);
intent.setData(Uri.parse("package:" + packageName));
startActivity(intent);
}
}
If this won't help in your case, try to use this library: https://github.com/iardelian/Revive-S
I am creating an android app which has functionality to sleep the device based on certain conditions and wake up based on some other conditions. Before API 21, there was a method powerManager.goToSleep() in PowerManager which used to do the trick. But, now the same method is not accessible anymore. Is there any other way to do that?
int defaultTurnOffTime = Settings.System.getInt(getContentResolver(), Settings.System.SCREEN_OFF_TIMEOUT, 60000);
Settings.System.putInt(getContentResolver(), Settings.System.SCREEN_OFF_TIMEOUT, 1000);
Settings.System.putInt(getContentResolver(), Settings.System.SCREEN_OFF_TIMEOUT, defaultTurnOffTime);
Above mentioned code is something I have got so far. But, it doesn't seem to work for me. Please suggest if there is any way to achieve the functionality.
Have you tried using PowerManager.Wakelock ?
Acquires the wake lock with a timeout.
Ensures that the device is on at the level requested when the wake lock was created. The lock will be released after the given timeout expires.
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK, "My Tag");
wl.acquire();
..screen will stay on during this section..
wl.release();
there are 4 types of Flags
Flag Value CPU Screen Keyboard
PARTIAL_WAKE_LOCK On* Off Off
SCREEN_DIM_WAKE_LOCK On Dim Off
SCREEN_BRIGHT_WAKE_LOCK On Bright Off
FULL_WAKE_LOCK On Bright Bright
I am attempting to put a device to sleep and I have found references all over about using the PowerManager class's goToSleep(long) method but I do not see it in the documentation and it does not work when I attempt to using it in my code.
Android's documentation does not contain a goToSleep method that I could see.
My Code:
private void sleepDevice() {
try {
PowerManager powerMgr = (PowerManager) getSystemService(Context.POWER_SERVICE);
long time = 1000;
powerMgr.goToSleep(time);
} catch (Exception ex) {
updateStatus("Error attempting to reboot device.");
updateStatus(ex.getLocalizedMessage());
}
}
Android Studio does not let the code compile with the message, "Cannot resolve method 'goToSleep(long).
I don't even see this method as deprecated. Also, I don't need to worry about security permissions, the call is intended to run on rooted devices or fail elegantly on non-rooted devices.
You can use the DeviceAdministrator , but you need the user to grant you those rights.
PowerManager manager = (PowerManager) getSystemService(Context.POWER_SERVICE);
PowerManager.WakeLock wl = manager
.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "YOUR_OWN_TAG");
wl.acquire();
wl.release();
Try this way and give some feedback.
I have a broadcastreceiver class that starts a service. What I want to happen is when it starts the service it should also turn the device screen on. I haven't found a solution to implement. I tried extending the WakefulBroadcastReceiver but it won't open the screen, rather it keeps the device on if it was already on. Does anyone know of a solution?
try this method
public void turnScreenOn(Context context) {
PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
#SuppressWarnings("deprecation")
WakeLock wakeLock = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP | PowerManager.ON_AFTER_RELEASE, "MyWakeLock");
wakeLock.acquire();
wakeLock.release();
}
requires the permission
<uses-permission android:name="android.permission.WAKE_LOCK"/>
My app sets an alarm. While waiting for the alarm my phone closes the screen. When the alarm sounds the screen is dark so I press the power key and the screen lights up but I now have to touch the Lock button to access the app interface. I would prefer that the Lock has been disposed off when the user responds to the alarm so I tried to use the power manager. The alarm receiver starts a new activity so I initialised the power manager in the onCreate for this activity. However this causes a force close error. I call the power manager as follows
PowerManager pm = (PowerManager)cText.getSystemService(Context.POWER_SERVICE);
PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK , TAG);
wl.acquire();
Any suggestions please.
Did add the uses permisson for power mananger in your manifest?
Since the person asking the question states that he "has to touch the lock" button I assume that he is speaking about the Key Guard. (Pattern, Pin etc). You cannot use the PowerManager API to disable this. Instead you should diable the KeyGuard
KeyguardManager keyguardManager =
(KeyguardManager) getSystemService(Activity.KEYGUARD_SERVICE);
KeyguardManager.KeyguardLock lock =
keyguardManager.newKeyguardLock(KEYGUARD_SERVICE);
lock.disableKeyguard();
To My knowledge, this is the correct way to disable the Screen Lock.
You also need this Permission
<uses-permission android:name="android.permission.DISABLE_KEYGUARD"/>
You can take wl in Application context so once its initlize and set back to that and after that when you release that you will set null and check that it again if its null than init again other wise take it as it is.