Reputation: 1209
I try to create simple timer. According to documentation I have permission <uses-permission android:name="android.permission.WAKE_LOCK" />
and use this code in the ForegroundService
:
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, getString( R.string.app_name ) );
wl.acquire();
It is working well for most devices when screen goes off, but some devices, for example Honor 10i (Android 10), after 10-15 seconds go to sleep mode and timer based on Handler
is sleeping.
I tried set off battery optimization etc., but this is not help. What is wrong?
Note. If function in the Handler
plays "tick" sound then it works fine.
Upvotes: 2
Views: 993
Reputation: 1209
Solution for Honor 10. Looks like Honor smartphones have non standard behavior. So,
After this very simple code start working.
Upvotes: 0
Reputation: 476
You should use REQUEST_IGNORE_BATTERY_OPTIMIZATIONS permission and ignore battery optimization for the app.
AndroidManifest.xml
<uses-permission android:name="android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS" />
then call the below method in your app
@RequiresApi(api = Build.VERSION_CODES.M)
private void ignoreBatteryOptimization() {
Intent intent = new Intent();
String packageName = getPackageName();
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
if (pm != null && !pm.isIgnoringBatteryOptimizations(packageName)) {
intent.setAction(Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS);
intent.setData(Uri.parse("package:" + packageName));
startActivity(intent);
}
}
You can also check this link
You can also check the below method for Honor 10i (Android 10). I've tested it on Xiaomi redmi phone. Hope it will also work on your listed of manufacturers.
public static List<Intent> POWER_MANAGER_INTENTS = Arrays.asList(
new Intent().setComponent(new ComponentName("com.miui.securitycenter", "com.miui.permcenter.autostart.AutoStartManagementActivity")),
new Intent().setComponent(new ComponentName("com.letv.android.letvsafe", "com.letv.android.letvsafe.AutobootManageActivity")),
new Intent().setComponent(new ComponentName("com.huawei.systemmanager", "com.huawei.systemmanager.optimize.process.ProtectActivity")),
new Intent().setComponent(new ComponentName("com.coloros.safecenter", "com.coloros.safecenter.permission.startup.StartupAppListActivity")),
new Intent().setComponent(new ComponentName("com.coloros.safecenter", "com.coloros.safecenter.startupapp.StartupAppListActivity")),
new Intent().setComponent(new ComponentName("com.oppo.safe", "com.oppo.safe.permission.startup.StartupAppListActivity")),
new Intent().setComponent(new ComponentName("com.iqoo.secure", "com.iqoo.secure.ui.phoneoptimize.AddWhiteListActivity")),
new Intent().setComponent(new ComponentName("com.iqoo.secure", "com.iqoo.secure.ui.phoneoptimize.BgStartUpManager")),
new Intent().setComponent(new ComponentName("com.vivo.permissionmanager", "com.vivo.permissionmanager.activity.BgStartUpManagerActivity")),
new Intent().setComponent(new ComponentName("com.asus.mobilemanager", "com.asus.mobilemanager.entry.FunctionActivity")).setData(android.net.Uri.parse("mobilemanager://function/entry/AutoStart")));
public static void startPowerSaverIntent(Context context) {
SharedPreferences settings = context.getSharedPreferences("ProtectedApps", Context.MODE_PRIVATE);
boolean skipMessage = settings.getBoolean("skipProtectedAppCheck", false);
if (!skipMessage) {
final SharedPreferences.Editor editor = settings.edit();
boolean foundCorrectIntent = false;
for (Intent intent : POWER_MANAGER_INTENTS) {
if (isCallable(context, intent)) {
foundCorrectIntent = true;
final AppCompatCheckBox dontShowAgain = new AppCompatCheckBox(context);
dontShowAgain.setText("Do not show again");
dontShowAgain.setOnCheckedChangeListener((buttonView, isChecked) -> {
editor.putBoolean("skipProtectedAppCheck", isChecked);
editor.apply();
});
new AlertDialog.Builder(context)
.setTitle(Build.MANUFACTURER + " Protected Apps")
.setMessage(String.format("%s requires to be enabled in 'Protected Apps' to function properly.%n", context.getString(R.string.app_name)))
.setView(dontShowAgain)
.setPositiveButton("Go to settings", (dialog, which) -> context.startActivity(intent))
.setNegativeButton(android.R.string.cancel, null)
.show();
break;
}
}
if (!foundCorrectIntent) {
editor.putBoolean("skipProtectedAppCheck", true);
editor.apply();
}
}
}
private static boolean isCallable(Context context, Intent intent) {
List<ResolveInfo> list = context.getPackageManager().queryIntentActivities(intent,
PackageManager.MATCH_DEFAULT_ONLY);
return list.size() > 0;
}
Read the full article, Click here.
or for related answers, Click here.
Upvotes: 2