When my online radio application goes in doze mode there is no network access because of which the radio stops working.Even the wake locks don't work in doze mode.
I've added the intent as per qouted in documentation.
An app holding the REQUEST_IGNORE_BATTERY_OPTIMIZATIONS permission can
trigger a system dialog to let the user add the app to the whitelist
directly, without going to settings. The app fires a
ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS Intent to trigger the
dialog.
Here is the intent.
Intent myIntent = new Intent();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
myIntent.setAction(Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS);
myIntent.setData(Uri.parse("package:" + getPackageName()));
startActivity(myIntent);
}
also i have added permission in manifest because the intent is not working without it.
Is it safe to use this permission in a radio application?
Related
Hi Developers I hope All are You fine. I need help. I make a AppLocker app and I use service to check which app i currently running. i locked app in my database with package name. When I show my lock screen From Service my Intent hit but lockScreen not Showing.How i call Activity From Service below is my Codeto start Activty from Service
if (locker_list!=null) {
if (launchapp) {
Log.d("TAG", "run: lock Screen Show");
Intent intent = new Intent(mContext, Lock_app_screen.class);
intent.putExtra("package_name", current_app);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
mContext.startActivity(intent);
}
}
Android 10 (API level 29) and higher place restrictions on when apps can start activities when the app is running in the background. These restrictions help minimize interruptions for the user and keep the user more in control of what's shown on their screen.
for more...
see this
I am using the background services and it does not run on some devices as I have to enable the autostart permission for the app. I want to enable the autostart permission automatically where it needs. I am using this code but it redirects me on the window where the autostart permission is.
Note :-- What I want , Not to redirect the user on the settings screen.
Here is my code for xiaomi
if (manufacturer.equalsIgnoreCase(android.os.Build.MANUFACTURER)) {
//this will open auto start screen where user can enable permission for your app
Intent intent1 = new Intent();
intent1.setComponent(new ComponentName("com.miui.securitycenter", "com.miui.permcenter.autostart.AutoStartManagementActivity"));
startActivity(intent1);
}
Thanks !
I use the following code to request ignore battery optimzation for an app. I can successfully request the permission. I am able to detect if the app is on the system's whitelist using isIgnoringBatteryOptimizations().
<uses-permission android:name="android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS"/>
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);
I can see a dialog, somethings like "let the app always run in background? ", and I click yes. I assumed that it will add my app to a system battery optimization whitelist.
Here is my problem. The documentation mentioned that user can manually config the list in Settings > Battery > Battery Optimization. So I expected that once I request and granted the permission, I can see it on System Settings, and I should be able to remove it manally. But I can see nothing. I don't see there is any relationship between the request and the list from System Settings.
Do they share the same list? are they equivalent if they are not the same?
So I expected that once I request and granted the permission, I can see it on System Settings
That's not what happens, actually. Documentation (https://developer.android.com/training/monitoring-device-state/doze-standby#support_for_other_use_cases) says:
An app holding the REQUEST_IGNORE_BATTERY_OPTIMIZATIONS permission can trigger a system dialog to let the user add the app to the whitelist directly, without going to settings. The app fires a ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS Intent to trigger the dialog.
It's just a permission to launch a dialog. The app isn't automatically whitelisted. Any way the user has to explicitly add it to the list.
Do they share the same list? are they equivalent if they are not the same?
There is only one list.
I am building an android application where I am using some services. My services get close when application is close in some custom android OS like MI.
Then I figure out we have to push our application in white list of security autostart application.
I got some SOF link's where they are suggesting below answer.
String manufacturer = "xiaomi";
if(manufacturer.equalsIgnoreCase(android.os.Build.MANUFACTURER)) {
//this will open auto start screen where user can enable permission for your app
Intent intent = new Intent();
intent.setComponent(new ComponentName("com.miui.securitycenter", "com.miui.permcenter.autostart.AutoStartManagementActivity"));
startActivity(intent);
}
This above code used to send user to that particular page.
Now I have to check if my application is disable so I can send user to this page and if it is enable then move to other screen. Is there any way to check this?
There is no Android API to check if the the AutoStart is enabled or not. Though you can have your own logic in the App (may be you can use preference to set a Boolean for it) to check this. Also the above way to enabling the AutoStart might not work always. Please have a look here
On Xiaomi's MI devices, there is a feature of turning off/on "Autostart" in their security app. (In Security App->Permissions->AutoStart)
This means none of the broadcast receivers receive anything while the app is not running. So BOOT_COMPLETED, USER_PRESENT, CONNECTIVITY_CHANGE, etc... do not work. (They work for a while after the app is on foreground, but stop soon). They also stop working after the user swipes the app from Xiaomi's version of "recent apps"
Even GCM fails to wake it up
For messaging apps, this is a killer.
By default, apps like Whatsapp, Messenger, Flipkart etc... are enabled by default (Even if these apps are not pre-installed).
Most other apps have this disabled by default. eg. Slack is disabled by default.
Is there a way to get on this white-list by default?
You can actually disable the battery optiomisation for your app. It'll turn all off the optimisations for the app so it won't get killed.
boolean isIgnoringBatteryOptimizations = pm.isIgnoringBatteryOptimizations(getPackageName());
if(!isIgnoringBatteryOptimizations){
Intent intent = new Intent();
intent.setAction(Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS);
intent.setData(Uri.parse("package:" + getPackageName()));
startActivityForResult(intent, MY_IGNORE_OPTIMIZATION_REQUEST);
}
Now check if the optimisation has been disabled for your app.
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == MY_IGNORE_OPTIMIZATION_REQUEST) {
PowerManager pm = (PowerManager)getSystemService(Context.POWER_SERVICE);
boolean isIgnoringBatteryOptimizations = pm.isIgnoringBatteryOptimizations(getPackageName());
if(isIgnoringBatteryOptimizations){
// Ignoring battery optimization
}else{
// Not ignoring battery optimization
}
}
}