Android M startActivity battery optimization

╄→尐↘猪︶ㄣ 提交于 2020-02-20 01:33:08

问题


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 whitelist my app, and to do that I saw that I can start an intent with the action request thanks to Buddy's answer in this post

Intent intent = new Intent();
String packageName = context.getPackageName();
PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
if (pm.isIgnoringBatteryOptimizations(packageName))
    intent.setAction(Settings.ACTION_IGNORE_BATTERY_OPTIMIZATION_SETTINGS);
else {
    intent.setAction(Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS);
    intent.setData(Uri.parse("package:" + packageName));
}
context.startActivity(intent);

well this should be too easy...because google doesn't like this approach and if you do it, your app should be banned from the play store...no comment... Ok so the way should be to drive the user to the battery settings and manually add your app in the DOZE's white list...yes this should be a big wall to climb...anyway seems to be the only way...now the answer is: I Can use an intent to go to the power usage summary, in this way (thank you Chris):

Intent powerUsageIntent = new Intent(Intent.ACTION_POWER_USAGE_SUMMARY);
    ResolveInfo resolveInfo = getPackageManager().resolveActivity(powerUsageIntent, 0);
// check that the Battery app exists on this device
    if(resolveInfo != null){
        startActivity(powerUsageIntent);
    }  

But how to directly go at the list of app for choosing the battery optimization?

Thanks for any answer.


回答1:


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.




回答2:


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.




回答3:


This is what I use:

manifest:

    <uses-permission android:name="android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS" />
        enum class WhiteListedInBatteryOptimizations {
            WHITE_LISTED, NOT_WHITE_LISTED, ERROR_GETTING_STATE, IRRELEVANT_OLD_ANDROID_API
        }

        fun getIfAppIsWhiteListedFromBatteryOptimizations(context: Context, packageName: String): WhiteListedInBatteryOptimizations {
            if (VERSION.SDK_INT < 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")
        @RequiresPermission(permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS)
        fun prepareIntentForWhiteListingOfBatteryOptimization(context: Context, packageName: String, alsoWhenWhiteListed: Boolean): Intent? {
            if (VERSION.SDK_INT < VERSION_CODES.M)
                return null
            if (ContextCompat.checkSelfPermission(context, 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:

        prepareIntentForWhiteListingOfBatteryOptimization(this, packageName, false)?.let { startActivity(it) }



来源:https://stackoverflow.com/questions/41596509/android-m-startactivity-battery-optimization

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!