Example usage for android.provider Settings ACTION_IGNORE_BATTERY_OPTIMIZATION_SETTINGS

List of usage examples for android.provider Settings ACTION_IGNORE_BATTERY_OPTIMIZATION_SETTINGS

Introduction

In this page you can find the example usage for android.provider Settings ACTION_IGNORE_BATTERY_OPTIMIZATION_SETTINGS.

Prototype

String ACTION_IGNORE_BATTERY_OPTIMIZATION_SETTINGS

To view the source code for android.provider Settings ACTION_IGNORE_BATTERY_OPTIMIZATION_SETTINGS.

Click Source Link

Document

Activity Action: Show screen for controlling which apps can ignore battery optimizations.

Usage

From source file:aenadon.wienerlinienalarm.activities.MainActivity.java

@TargetApi(Build.VERSION_CODES.M)
private Dialog batteryWarningWithDoze() {
    View batteryReminderDialog = View.inflate(MainActivity.this, R.layout.checkbox, null);
    final CheckBox batteryReminderCheckbox = batteryReminderDialog.findViewById(R.id.battery_reminder_checkbox);

    String messageBody = getString(R.string.battery_optimization_warning_dialog_text) + "\n\n"
            + getString(R.string.battery_optimization_warning_doze);

    return new AlertDialog.Builder(MainActivity.this)
            .setTitle(getString(R.string.battery_optimization_warning_dialog_title)).setMessage(messageBody)
            .setView(batteryReminderDialog).setPositiveButton(R.string.allow, (dialog, which) -> {
                Intent batterySettings = new Intent(Settings.ACTION_IGNORE_BATTERY_OPTIMIZATION_SETTINGS);
                startActivity(batterySettings);
            }).setNegativeButton(R.string.cancel, (dialog, which) -> {
                if (batteryReminderCheckbox.isChecked()) {
                    dismissBatteryReminder();
                }//from ww w . java2 s .c  o m
            }).create();
}

From source file:org.deviceconnect.android.manager.core.util.DConnectUtil.java

/**
 * Doze??????.//from w  w  w . ja v  a  2  s.  c  o m
 * <p>
 * Doze???????????<br>
 * Android M ?? OS ????????????
 * </p>
 *
 * @param context 
 */
public static void startDozeModeSettingActivity(final Context context) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        Intent intent = new Intent(Settings.ACTION_IGNORE_BATTERY_OPTIMIZATION_SETTINGS);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(intent);
    }
}

From source file:com.sentaroh.android.SMBSync2.ActivityMain.java

@SuppressLint("NewApi")
private void checkBatteryOptimization() {
    if (Build.VERSION.SDK_INT >= 23) {
        Intent intent = new Intent();
        String packageName = mContext.getPackageName();
        PowerManager pm = (PowerManager) mContext.getSystemService(Context.POWER_SERVICE);
        if (pm.isIgnoringBatteryOptimizations(packageName))
            intent.setAction(Settings.ACTION_IGNORE_BATTERY_OPTIMIZATION_SETTINGS);
        else {// w  ww.j av  a2 s . co m
            intent.setAction(Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS);
            intent.setData(Uri.parse("package:" + packageName));
        }
        startActivity(intent);
    }
}

From source file:be.blinkt.openvpn.activities.MainActivity.java

@TargetApi(Build.VERSION_CODES.M)
private void requestDozeDisable() {
    Intent intent = new Intent();
    String packageName = getPackageName();
    PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
    if (pm.isIgnoringBatteryOptimizations(packageName))
        intent.setAction(Settings.ACTION_IGNORE_BATTERY_OPTIMIZATION_SETTINGS);
    else {/*from   w w  w. ja  v a 2 s . com*/
        intent.setAction(Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS);
        intent.setData(Uri.parse("package:" + packageName));
    }
    startActivity(intent);
}

From source file:com.master.metehan.filtereagle.ActivityMain.java

private void checkDoze() {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        final Intent doze = new Intent(Settings.ACTION_IGNORE_BATTERY_OPTIMIZATION_SETTINGS);
        PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
        if (!pm.isIgnoringBatteryOptimizations(getPackageName())
                && getPackageManager().resolveActivity(doze, 0) != null) {
            final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
            if (!prefs.getBoolean("nodoze", false)) {
                LayoutInflater inflater = LayoutInflater.from(this);
                View view = inflater.inflate(R.layout.doze, null, false);
                final CheckBox cbDontAsk = (CheckBox) view.findViewById(R.id.cbDontAsk);
                dialogDoze = new AlertDialog.Builder(this).setView(view).setCancelable(true)
                        .setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                prefs.edit().putBoolean("nodoze", cbDontAsk.isChecked()).apply();
                                startActivity(doze);
                            }/*from www . ja  va  2 s  .c  o m*/
                        }).setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                prefs.edit().putBoolean("nodoze", cbDontAsk.isChecked()).apply();
                            }
                        }).setOnDismissListener(new DialogInterface.OnDismissListener() {
                            @Override
                            public void onDismiss(DialogInterface dialogInterface) {
                                dialogDoze = null;
                            }
                        }).create();
                dialogDoze.show();
            }
        }
    }
}

From source file:com.googlecode.mindbell.MindBellPreferences.java

private void onPreferenceClickBatterySettings() {
    if (Build.VERSION.SDK_INT >= 23) {
        if (Utils.isAppWhitelisted(this)) {
            new AlertDialog.Builder(this) //
                    .setTitle(R.string.prefsBatterySettings) //
                    .setMessage(R.string.summaryBatterySettingsWhitelisted) //
                    .setNegativeButton(android.R.string.cancel, null) //
                    .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            // Take user to the battery settings so he can check the settings
                            startActivity(new Intent(Settings.ACTION_IGNORE_BATTERY_OPTIMIZATION_SETTINGS));
                        }/*w  ww .  ja  v  a  2  s  .  c om*/
                    }) //
                    .show();
        } else {
            new AlertDialog.Builder(this) //
                    .setTitle(R.string.prefsBatterySettings) //
                    .setMessage(R.string.summaryBatterySettingsNotWhitelisted) //
                    .setNegativeButton(android.R.string.cancel, null) //
                    .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            // Take user to the battery settings instead of adding a new permission that might result in
                            // suspending MindBell from Google Play Store. See the comments for this answer:
                            // https://stackoverflow.com/a/33114136/2532583
                            startActivity(new Intent(Settings.ACTION_IGNORE_BATTERY_OPTIMIZATION_SETTINGS));
                            Context context = MindBellPreferences.this;
                            Toast.makeText(context, context.getText(R.string.battery_settings_guidance1),
                                    Toast.LENGTH_LONG).show();
                            Toast.makeText(context, context.getText(R.string.battery_settings_guidance2),
                                    Toast.LENGTH_LONG).show();
                            Toast.makeText(context, context.getText(R.string.battery_settings_guidance3),
                                    Toast.LENGTH_LONG).show();
                        }
                    }) //
                    .show();
        }
    } else {
        new AlertDialog.Builder(this) //
                .setTitle(R.string.prefsBatterySettings) //
                .setMessage(R.string.summaryBatterySettingsUnknown) //
                .setPositiveButton(android.R.string.ok, null) //
                .show();
    }
}