This is my Code:-
AndroidManifest.xml
<uses-permission-sdk-23 android:name="android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS"/>
Java side:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
getActivity().startActivity(new Intent(
Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS,
Uri.parse("package:" + getActivity().getPackageName())));
} else {
binding.txtBatteryOptimizationGrant.setText(Granted);
Toast.makeText(getContext(),
getContext().getString(R.string.battery_optimizations_not_required),
Toast.LENGTH_SHORT).show();
}
After Granting Permission from BATTERY_OPTIMIZATIONS Dialog it is not working because my AccessibilityService getting disable even after giving autostart permission.
I'm Granting BATTERY_OPTIMIZATIONS Permission and also Granting autostart permission but not working.
See My AccessibilityService Class from below Link:-
AccessibilityService getting disable even after giving autostart permission in vivo and oppo devices
To solved this have to do this below process manually:
The problem is that i can't open High background power consumption setting programmatically. So that user can grant. And by granting Permission from High background power consumption settings manually my AccessibilityService does not kill and stay alive in background.