Android M startActivity battery optimization

前端 未结 3 821
北荒
北荒 2021-02-08 17:30

I\'m developing an app that should alert an user if is near a place. and of course have to do that also if the phone is in idle. With DOZE now I understood that I have to whitel

相关标签:
3条回答
  • 2021-02-08 17:32

    To open list of apps for choosing battery optimization you can use this code sample:

    private void openPowerSettings(Context context) {
        Intent intent = new Intent();
        intent.setAction(Settings.ACTION_IGNORE_BATTERY_OPTIMIZATION_SETTINGS);
        context.startActivity(intent);
    }
    

    It doesn't need to have <uses-permission android:name="android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS"/> permission, so it should be ok to publish it to Google Play (for details please check this thread and comments to this question).

    NOTE

    Adding this line

    intent.setData(Uri.parse("package:" + mContext.getPackageName()));
    

    will cause crash "Fatal Exception: android.content.ActivityNotFoundException No Activity found to handle Intent { act=android.settings.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS dat=package:io.demo.example }". User has to find the app in the list, it seems no way to jump directly to our app.

    0 讨论(0)
  • 2021-02-08 17:41

    Try the below code to open Ignore Battery Optimization Settings page.

    private void openPowerSettings() {
        startActivityForResult(new Intent(android.provider.Settings.ACTION_IGNORE_BATTERY_OPTIMIZATION_SETTINGS), 0);
    }
    

    No extra permissions are required to be added to the manifest file.

    0 讨论(0)
  • 2021-02-08 17:50

    This is what I use:

    manifest:

        <uses-permission android:name="android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS" />
    
    import android.Manifest
    import android.annotation.SuppressLint
    import android.content.Context
    import android.content.Intent
    import android.content.pm.PackageManager
    import android.net.Uri
    import android.os.Build
    import android.os.PowerManager
    import android.provider.Settings
    import androidx.annotation.RequiresPermission
    import androidx.core.content.ContextCompat
    
    object PowerSaverHelper {
        enum class WhiteListedInBatteryOptimizations {
            WHITE_LISTED, NOT_WHITE_LISTED, ERROR_GETTING_STATE, IRRELEVANT_OLD_ANDROID_API
        }
    
        fun getIfAppIsWhiteListedFromBatteryOptimizations(context: Context, packageName: String = context.packageName): WhiteListedInBatteryOptimizations {
            if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) return WhiteListedInBatteryOptimizations.IRRELEVANT_OLD_ANDROID_API
            val pm = context.getSystemService(Context.POWER_SERVICE) as PowerManager?
                    ?: return WhiteListedInBatteryOptimizations.ERROR_GETTING_STATE
            return if (pm.isIgnoringBatteryOptimizations(packageName)) WhiteListedInBatteryOptimizations.WHITE_LISTED else WhiteListedInBatteryOptimizations.NOT_WHITE_LISTED
        }
    
        //@TargetApi(VERSION_CODES.M)
        @SuppressLint("BatteryLife", "InlinedApi")
        @RequiresPermission(Manifest.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS)
        fun prepareIntentForWhiteListingOfBatteryOptimization(context: Context, packageName: String = context.packageName, alsoWhenWhiteListed: Boolean = false): Intent? {
            if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M)
                return null
            if (ContextCompat.checkSelfPermission(context, Manifest.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS) == PackageManager.PERMISSION_DENIED)
                return null
            val appIsWhiteListedFromPowerSave: WhiteListedInBatteryOptimizations = getIfAppIsWhiteListedFromBatteryOptimizations(context, packageName)
            var intent: Intent? = null
            when (appIsWhiteListedFromPowerSave) {
                WhiteListedInBatteryOptimizations.WHITE_LISTED -> if (alsoWhenWhiteListed) intent = Intent(Settings.ACTION_IGNORE_BATTERY_OPTIMIZATION_SETTINGS)
                WhiteListedInBatteryOptimizations.NOT_WHITE_LISTED -> intent = Intent(Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS).setData(Uri.parse("package:$packageName"))
                WhiteListedInBatteryOptimizations.ERROR_GETTING_STATE, WhiteListedInBatteryOptimizations.IRRELEVANT_OLD_ANDROID_API -> {
                }
            }
            return intent
        }
    }
    

    Example:

    PowerSaverHelper.prepareIntentForWhiteListingOfBatteryOptimization(this)?.let { startActivity(it) }
    
    
    0 讨论(0)
提交回复
热议问题