Currently using powerManager.isIgnoringBatteryOptimizations(getPackageName()) for devices running api >= 23.
And requesting user to allow my app to be not optimised by using
Intent intent = new Intent(Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS);
intent.setData(Uri.parse("package:" + getPackageName()));
This works on most of the devices, but on Oneplus devices even after user allowing it to be not optimised, I see no effect, my application is still optimised and I miss SMS_RECEIVED broadcast.
Oneplus device with Oxygen OS 5.0.1 and Android OS 8.0.0
Related
I have an app that uses BLE to exchange data with a BLE-capable device.
I used to develop and test it using Asus Zenfone Max 3 (Android 8.1) and I had no problems.
Then, I got an Asus Zenfone Max Pro M1 (Android 8.1). The app connects to the device but could not exchange any data.
After long investigation, it turns out that I had to go to Battery optimization in the smartphone's settings and change Bluetooth and Bluetooth MIDI Service to Not optimized and then my app worked fine.
I don't know whether this default setting is related to the OS (Android One) or to the phone model. But it is really a crappy thing because I am not supposed to tell every customer to do this (in his phone's settings) in order for the app to work !
So, my question is, is their a way to know, from the code, whether these services are optimized or not, and whether I can change these settings by code or any other ideas that could better fix this issue.
Add this permission to your manifest file:
<uses-permission android:name="android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS"/>
and test it like this :
Intent intent = new Intent();
String packageName = context.getPackageName();
PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
if (pm.isIgnoringBatteryOptimizations(packageName))
intent.setAction(Settings.ACTION_IGNORE_BATTERY_OPTIMIZATION_SETTINGS);
else {
intent.setAction(Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS);
intent.setData(Uri.parse("package:" + packageName));
}
context.startActivity(intent);
reference
I implemented ignore battery optimization functionality programmatically.
I found lots of questions and answers but i want to know that the IGNORE_BATTERY_OPTIMIZATIONS is works only for pure android or is there any other cases?
I have couple of devices as below listed :
Moto g4 (7.0)
Moto g play (6.0)
Samsung galaxy note 4(6.0)
Mi note 4 (6.0)
Lenovo Tab(6.0)
Honor (6.0)
It works proper in Moto device.
In Moto we able to get pop up and if we accept permission then it works well.
It work 50% correct in Lenovo tab & Honor devices.
In Lenovo tab when the Permission pop up appears and if we accept the permission then the application added but when we change settings manually it's not working.
Well while in Honor devices when we got permission popup and accept the permission after that we open setting and check the app status its listed in don't allow battery optimization app but the state of the application is "Allow battery optimization". After that when we allow it manually it will work.
In MI and Samsung devices it's not working anymore.
well The Mi Devices are exceptional cases as they have their customized architecture, so we are not concerning MI devices.
for Samsung note 4 there is a special case as below :
After installing app we got the permission pop up and accept the permission. After that we check it in setting, but the app is not listed in "don't allow battery optimization" app list. (Note: When device will reboot the app added in don't allow battery optimization)
Here is Code:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
msPackageName = getPackageName();
moPowerManager = (PowerManager) getSystemService(Context.POWER_SERVICE);
Log.i(TAG, "Permission " + moPowerManager.isIgnoringBatteryOptimizations(msPackageName));
if (!moPowerManager.isIgnoringBatteryOptimizations(msPackageName)) {
Intent loIntent = new Intent();
loIntent.setAction(android.provider.Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS);
loIntent.setData(Uri.parse("package:" + msPackageName));
startActivity(loIntent);
}
}
Please advice for this different behavior. and if there is a solution please let me know that. it's becoming frustrating.
I am trying to determine the storage encryption status of my Android device from within my application. Following the recommendations of the relevant Android Developer page, here is my code:
DevicePolicyManager mDPM = (DevicePolicyManager) getSystemService(Context.DEVICE_POLICY_SERVICE);
int encryptionStatus = mDPM.getStorageEncryptionStatus();
if (DEBUG) Log.v(TAG, "checkSecuritySettingsSufficient: encryptionStatus=" + encryptionStatus);
Here's the trouble: when I run this code on a device (I've tried it on a Motorola Droid Maxx running Android 4.4.4 and a Nexus 7 running Android 5.0.2) which I have previously encrypted, DevicePolicyManager.getStorageEncryptionStatus() will always return a value of 1, i.e. ENCRYPTION_STATUS_INACTIVE.
Android is therefore reporting that the device is not encrypted, despite the fact that the file system is definitely encrypted (I checked its status from the Settings > Security page).
Is this function broken? There doesn't seem to be any mention of that on SO or on other web sources. This leads me to believe that I am not doing something correctly with respect to DevicePolicyManager.
UPDATE After running through the encryption steps again with the Motorola device, DevicePolicyManager.getStorageEncryptionStatus() is returning the correct value, but it's still failing on the Nexus 7.
I just ran into this same issue and found out it was happening because the device had disk encryption enabled, but did not require the passcode be entered at startup. Changing the passcode, and forcing the require PIN at startup option to be true made DevicePolicyManager.getStorageEncryptionStatus() correctly return ENCRYPTION_STATUS_ACTIVE.
I am cutting my teeth on Android this week and I have a Beaglebone Black running an Android JB 4.2.2, linux kernel 3.2 image from TI with a Broadcomm BCM20702 usb bluetooth dongle attached.
The dongle works alright in Angstrom but I am sufficiently neophyte as to not recognise if the support is there in Jelly bean.
I begun testing with the following code:
private BluetoothAdapter BA;
BA = BluetoothAdapter.getDefaultAdapter();
Intent turnOn = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(turnOn, 0);
I get my BA, i get my turnOn intent but when I start the intent I get the allow permissions dialogs then the "Turning Bluetooth on..." modal indication which never clears.
The application threads are all running in the debugger but the UI looses focus and is unresponsive.
What can I do to debug the situation ?
How do I determine whether the dongle really is supported in this kernel or enable support if not ?
What provisions are in the Android UI for bluetooth connection/setup (I haven't seen any which may indicate a current lack of support)?
How can I debug the hung dialog "Turning Bluetooth on..." ?
Cheers much,
Chris
I have been working to turn off airplane mode(if airplane mode is in ON state) in rooted device version 4.3. The below following code doesn't work in android version 4.3. It's works fine up to the version 4.2.
Settings.Global.putInt(context.getContentResolver(),
"airplane_mode_on", 0);
isAirplaneModeOn = isAirplaneModeOn(context);
Intent localIntent2 = new Intent(
"android.intent.action.AIRPLANE_MODE");
localIntent2.putExtra("state", isAirplaneModeOn);
context.sendBroadcast(localIntent2);
Please let me know if anybody have any idea/code to turn off airplane mode in rooted device version 4.3?