Hello, dear friends, this post is very important to the Android developers to resolve to avoid battery doze mode so let's start...
MainActivity.java
DozeHelper.java
Enjoy with code
MainActivity.java
package com.samset.doze;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.AppCompatButton;
import androidx.appcompat.widget.AppCompatTextView;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import com.samset.doze.utils.DozeHelper;
public class MainActivity extends AppCompatActivity {
private AppCompatButton btncheck;
private AppCompatTextView tvmsg;
@Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btncheck=findViewById(R.id.btncheck);
tvmsg=findViewById(R.id.tvmsg);
tvmsg.setText("This app is " +DozeHelper.getisBatteryOptimizations(this,getPackageName().toString()));
btncheck.setOnClickListener(new View.OnClickListener() {
@Override public void onClick(View view) {
Intent intent= DozeHelper.prepareBatteryOptimization(MainActivity.this,getPackageName(),true);
startActivity(intent);
}
});
}
}
public class DozeHelper { public enum PowerSaveState { ON, OFF, ERROR_GETTING_STATE, IRRELEVANT_OLD_ANDROID_API } public enum WhiteListedInBatteryOptimizations { WHITE_LISTED, NOT_WHITE_LISTED, ERROR_GETTING_STATE, UNKNOWN_TOO_OLD_ANDROID_API_FOR_CHECKING, IRRELEVANT_OLD_ANDROID_API } public enum DozeState { NORMAL_INTERACTIVE, DOZE_TURNED_ON_IDLE, NORMAL_NON_INTERACTIVE, ERROR_GETTING_STATE, IRRELEVANT_OLD_ANDROID_API, UNKNOWN_TOO_OLD_ANDROID_API_FOR_CHECKING } @NonNull public static DozeState getDozeState(@NonNull Context context) { if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) return DozeState.IRRELEVANT_OLD_ANDROID_API; if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) { return DozeState.UNKNOWN_TOO_OLD_ANDROID_API_FOR_CHECKING; } final PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE); if (pm == null) return DozeState.ERROR_GETTING_STATE; return pm.isDeviceIdleMode() ? DozeState.DOZE_TURNED_ON_IDLE : pm.isInteractive() ? DozeState.NORMAL_INTERACTIVE : DozeState.NORMAL_NON_INTERACTIVE; } @NonNull public static PowerSaveState getPowerSaveState(@NonNull Context context) { if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) return PowerSaveState.IRRELEVANT_OLD_ANDROID_API; final PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE); if (pm == null) return PowerSaveState.ERROR_GETTING_STATE; return pm.isPowerSaveMode() ? PowerSaveState.ON : PowerSaveState.OFF; } @NonNull public static WhiteListedInBatteryOptimizations getisBatteryOptimizations(@NonNull Context context, @NonNull String packageName) { if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) return WhiteListedInBatteryOptimizations.IRRELEVANT_OLD_ANDROID_API; if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) return WhiteListedInBatteryOptimizations.UNKNOWN_TOO_OLD_ANDROID_API_FOR_CHECKING; final PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE); if (pm == null) return WhiteListedInBatteryOptimizations.ERROR_GETTING_STATE; return pm.isIgnoringBatteryOptimizations(packageName) ? WhiteListedInBatteryOptimizations.WHITE_LISTED : WhiteListedInBatteryOptimizations.NOT_WHITE_LISTED; } @TargetApi(Build.VERSION_CODES.M) @RequiresPermission(Manifest.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS) @Nullable public static Intent prepareBatteryOptimization(@NonNull Context context, @NonNull String packageName, boolean alsoWhenWhiteListed) { if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) return null; if (ContextCompat.checkSelfPermission(context, Manifest.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS) == PackageManager.PERMISSION_DENIED) return null; final WhiteListedInBatteryOptimizations ispowersave = getisBatteryOptimizations(context, packageName); Intent intent = null; switch (ispowersave) { case WHITE_LISTED: if (alsoWhenWhiteListed) intent = new Intent(Settings.ACTION_IGNORE_BATTERY_OPTIMIZATION_SETTINGS); break; case NOT_WHITE_LISTED: intent = new Intent(Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS).setData(Uri.parse("package:" + packageName)); break; case ERROR_GETTING_STATE: case UNKNOWN_TOO_OLD_ANDROID_API_FOR_CHECKING: case IRRELEVANT_OLD_ANDROID_API: default: break; } return intent; } /** * registers a receiver to listen to power-save events. returns true if succeeded to register the broadcastReceiver. */ @TargetApi(Build.VERSION_CODES.M) public static boolean registerPowerSaveReceiver(@NonNull Context context, @NonNull BroadcastReceiver receiver) { if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) return false; IntentFilter filter = new IntentFilter(); filter.addAction(PowerManager.ACTION_DEVICE_IDLE_MODE_CHANGED); context.registerReceiver(receiver, filter); return true; } }
Enjoy with code