today i create a app in map , but it crash when battery saver is on .
How to check event when battery saver in on, for every device, help me. Thanks
i try this, but not working in API <21:
PowerManager powerManager = (PowerManager)
this.getSystemService(Context.POWER_SERVICE);
if ( powerManager.isPowerSaveMode()) {
// Animations are disabled in power save mode, so just show a toast instead.
Toast.makeText(customer_textReport.this, "Vui lòng tắt chế độ tiết kiệm pin", Toast.LENGTH_SHORT).show();
}
else {
Intent intentvitri = new Intent(customer_textReport.this, CustomerGetLocation.class);
startActivityForResult(intentvitri, 111);
}
Check Google Developer: PowerManager
In the left top, you can change API level.
As you can see, isPowerSaveMode(), is added in API 21(Lollipop).
So it won't work on older devices.
On Mobiles with an Android version below 5.0 (Lollipop) the power_saving mode is different for each manufactor. However if your target version is 5.0 and higher you can use the PowerManager as descripted in Android Developer
Hmm.This is check high acurary mode in GPS. This is check:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
try {
if (getLocationMode(getApplicationContext()) != 3) {
tvmessage.setText("Please turn on GPS high Acurary");
btcancel_Dialog.setText("OK");
btcancel_Dialog.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS));
dlg.dismiss();
}
});
dlg.show();
} else {
Intent intentvitri = new Intent(customer_textReport.this, CustomerGetLocation.class);
startActivityForResult(intentvitri, 111);
}
} catch (Settings.SettingNotFoundException e) {
e.printStackTrace();
}
}
and method getLocationMode return mode of GPS:
private int getLocationMode(Context context) throws Settings.SettingNotFoundException {
return Settings.Secure.getInt(context.getContentResolver(), Settings.Secure.LOCATION_MODE);
}
I ended up with this function.
Note: It's not equal to isPowerSaveMode(). It's more like isRunningOutOfPower() or couldBePowerSaveMode()
It checks if the SDK >= 21 then isPowerSaveMode function is available. if not, then check if the GPS mode is not HIGH_ACCURACY and battery level is less than 15% then it "could be" powerSaveMode.
public static boolean couldBePowerSaveMode(Context context) {
PowerManager powerManager = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
if (powerManager != null) {
return powerManager.isPowerSaveMode();
}
}
if (getLocationMode(context) != Settings.Secure.LOCATION_MODE_HIGH_ACCURACY) {
return getBatteryPercentage(context) <= 15;
}
return getBatteryPercentage(context) <= 15;
}
Try this (ready for copy/paste):
/**
* Checks if device is in power save mode. For older versions that do not support this API, returns false.
* #return true if it is, false otherwise.
*/
#TargetApi(Build.VERSION_CODES.LOLLIPOP)
private static boolean isPowerSaveMode(Context context)
{
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
{
PowerManager powerManager = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
return powerManager.isPowerSaveMode();
}
// For older versions, we just say that device is not in power save mode
return false;
}
Related
I'm trying to figure out how to detect if the Display is Off on Android and I have a feeling that the Always-On Display on my S10 is affecting my results.
Currently, I'm using the logic found in this stackoverflow answer.
There is also a comment on that thread that says you should be able to use Display.FLAG_PRIVATE to check for Samsung's Always-On Display, but it doesn't explain how.
Using that information, I've put together this function. But it doesn't seem to work. The display seems to be counted as on almost always if i have the Always-On display enabled.
public boolean isScreenOn() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT_WATCH) {
DisplayManager dm = (DisplayManager) getSystemService(Context.DISPLAY_SERVICE);
for (Display display : dm.getDisplays()) {
if ((display.getFlags() & Display.FLAG_PRIVATE) != Display.FLAG_PRIVATE) {
if (display.getState() != Display.STATE_OFF) {
return true;
}
}
}
return false;
} else {
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
//noinspection deprecation
return pm.isScreenOn();
}
}
I'm not sure if the way i'm checking the Display.FLAG_PRIVATE flag is correct, or if that will even help with the potential of the Always-On display affecting the result.
I found that the flags don't actually have anything to do with it. It seems that the display state will be one of several states including Display.STATE_DOZE_SUSPENDED, Display.STATE_DOZE and several others when the Always-On Display is active. So instead, I'm just checking for Display.STATE_ON and treating all other states as the display being turned off.
public boolean isScreenOn() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT_WATCH) {
DisplayManager dm = (DisplayManager) getSystemService(Context.DISPLAY_SERVICE);
for (Display display : dm.getDisplays()) {
if (display.getState() == Display.STATE_ON) {
return true;
}
}
return false;
} else {
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
//noinspection deprecation
return pm.isScreenOn();
}
}
I am trying to turn on hardware acceleration for my application
but I never seem to get a 'true' result from this function.
I tried all the methods in the Android Developers blog post about the
the tag android:hardwareAccelerated=true to the application
and even called
getWindow().setFlags(WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED,
WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED);
You should always call isHardwareAccelearted() after the view is attached to the window. I am not sure about your case as I can't see where actually you are calling it.
Also, the support level of various operations across API levels are mentioned here. I hope this helps.
You can also try this method to verify hardwareAcceleration.
public static boolean hasHardwareAcceleration(Activity activity) {
// Has HW acceleration been enabled manually in the current window?
Window window = activity.getWindow();
if (window != null) {
if ((window.getAttributes().flags
& WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED) != 0) {
return true;
}
}
// Has HW acceleration been enabled in the manifest?
try {
ActivityInfo info = activity.getPackageManager().getActivityInfo(
activity.getComponentName(), 0);
if ((info.flags & ActivityInfo.FLAG_HARDWARE_ACCELERATED) != 0) {
return true;
}
} catch (PackageManager.NameNotFoundException e) {
Log.e("Chrome", "getActivityInfo(self) should not fail");
}
return false;
}
For checking for View try this.
chat_wv.post(new Runnable() {
#Override
public void run() {
Log.e("isHardwareAccelerated", ""+chat_wv.isHardwareAccelerated());
}
});
I'm wondering if there's any way in React Native that allows us to know if user has been selected "Battery Optimized" for our app?
in native android,You can use PowerManager.isIgnoringBatteryOptimizations() boolean to determine if an application is already ignoring optimizations ie whether the app is on the whitelist .
#ReactMethod public void isAppIgnoringBatteryOptimization(Callback callback){
String packageName = getReactApplicationContext().getPackageName();
PowerManager pm = (PowerManager) getReactApplicationContext().getSystemService(Context.POWER_SERVICE);
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
callback.invoke(pm.isIgnoringBatteryOptimizations(packageName));
}else{
callback.invoke(true);
}
and then in js
isAppIgnoringBatteryOptimization: function(callback) {
jobModule.isAppIgnoringBatteryOptimization(ignoringOptimization => {
if (ignoringOptimization != null) {
callback("", ignoringOptimization);
} else {
callback("error", null);
}
});
}
https://github.com/vikeri/react-native-background-job/ is a good library that sees all the edge cases of background execution
If you want to ask user to turn it off - ask for
Manifest.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS permission
from
https://developer.android.com/reference/android/provider/Settings#ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS
I'm trying to get the current RSSI of a wifi connection in a Service using an AsyncTask. When the screen is turned off the value will not be updated until I turn the display back on again. So the value stays the same as the last value that was obtained before the display was turned off.
I used an Nexus 6P with Android O preview and a LG 3 with Android 6.
I'm obtaining a WakeLock and a WifiLock before via:
PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "WifiMeasureTaskWakeLock");
mWakeLock.acquire();
mWifiLock = mWifiManager.createWifiLock(WifiManager.WIFI_MODE_FULL_HIGH_PERF, "WifiMeasureTaskWifiLock");
mWifiLock.acquire();
And in the doInBackground method I'm doing the following:
#Override
protected Void doInBackground(final Void... params) {
int dBm;
try {
//some other code here
while (true) {
if (isCancelled()) {
//some other code here
return null;
}
try {
//some other code here
//does not update when the screen if turned off
dBm = Math.max(mWifiManager.getConnectionInfo().getRssi(), -100);
//some other code here
}
} catch (IOException e) {
//some other code here
}
}
} catch (SocketException e) {
//some other code here
}
return null;
}
I already found an issue in the issue tracker but it's marked obsolete.
Does anybody know how to solve this problem?
I'm developing an Android application that might be used at night. Therefor, I need to turn off the buttons' backlight. How can I do this? On my own phone the backlight turns off after a while, but on the Motorola Droid I don't think this happens.
I'm using a wakelock to keep the screen on. Should I use another flag or how can I do this?
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
mWakeLock = pm.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK, WAKE_LOCK_TAG);
mWakeLock.acquire();
Thank you very much!
//Kaloer
There is a hack:
private void setDimButtons(boolean dimButtons) {
Window window = getWindow();
LayoutParams layoutParams = window.getAttributes();
float val = dimButtons ? 0 : -1;
try {
Field buttonBrightness = layoutParams.getClass().getField(
"buttonBrightness");
buttonBrightness.set(layoutParams, val);
} catch (Exception e) {
e.printStackTrace();
}
window.setAttributes(layoutParams);
}
I see that this is an old question that was mostly answered in a comment link, but to make it clear to anyone else who comes across this question, here's my own answer.
It's built-in since API 8. (doc)
float android.view.WindowManager.LayoutParams.buttonBrightness
This is a somewhat modified/simplified version of what I'm using in one of my apps (excluding irrelevant code). The inner class is required to prevent a crash at launch on older platforms that don't support it.
private void nightMode() {
Window win = getWindow();
LayoutParams lp = win.getAttributes();
if (prefs.getBoolean("Night", false))
changeBtnBacklight(lp, LayoutParams.BRIGHTNESS_OVERRIDE_OFF);
else changeBtnBacklight(lp, LayoutParams.BRIGHTNESS_OVERRIDE_NONE);
win.setAttributes(lp);
}
private void changeBtnBacklight(LayoutParams lp, float value) {
if (Integer.parseInt(Build.VERSION.SDK) >= 8) {
try {
new BtnBrightness(lp, value);
} catch (Exception e) {
Log.w(TAG, "Error changing button brightness");
e.printStackTrace();
}
}
}
private static class BtnBrightness {
BtnBrightness(LayoutParams lp, float v) {
lp.buttonBrightness = v;
}
}
AFAIK, there is no API to control the backlight of the buttons -- sorry!