【问题标题】:Android M startActivity battery optimizationAndroid M startActivity 电池优化
【发布时间】:2017-05-26 13:50:04
【问题描述】:

我正在开发一个应用程序,如果在某个地方附近,它会提醒用户。 当然,如果手机处于空闲状态,也必须这样做。 现在有了 DOZE,我明白我必须将我的应用程序列入白名单,为此我看到我可以通过操作请求开始一个意图,这要感谢 Buddy 在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);

嗯,这应该太容易了...因为谷歌不喜欢这种方法,如果你这样做,你的应用程序应该被禁止进入 Play 商店......没有评论...... 好的,所以方法应该是引导用户进入电池设置并手动将您的应用添加到 DOZE 的白名单中......是的,这应该是一堵要爬的大墙......无论如何似乎是唯一的方法......现在答案是: 我可以通过这种方式 (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);
    }  

但是如何直接进入应用列表选择电池优化呢?

感谢您的任何回答。

【问题讨论】:

  • 据我了解,操作 Settings.ACTION_IGNORE_BATTERY_OPTIMIZATION_SETTINGS 是您所需要的
  • 嗨 Grimmy,小心它,因为我写过 google 不喜欢这种方法,如果你这样做了,你的应用程序应该被禁止进入 Play 商店...阅读此消息:@987654323 @
  • 根据该线程,如果您的应用请求 android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS 权限,Google 可以禁止您的应用。但是 Settings.ACTION_IGNORE_BATTERY_OPTIMIZATION_SETTINGS 操作不需要它,它将显示用于选择电池优化的应用列表
  • 所以你说如果我不将请求放入清单文件中,谷歌就可以了吗?
  • 这是你发给我的帖子里提到的

标签: android android-intent optimization battery


【解决方案1】:

要打开选择电池优化的应用列表,您可以使用此代码示例:

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

不需要<uses-permission android:name="android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS"/>权限,发布到Google Play应该没问题(详情请查看this这个问题的线程和cmets)。

注意

添加这一行

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

会导致崩溃“致命异常:android.content.ActivityNotFoundException No Activity found to handle Intent { act=android.settings.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS dat=package:io.demo.example }”。用户必须在列表中找到该应用程序,似乎无法直接跳转到我们的应用程序。

【讨论】:

  • 我收到一个错误致命异常:android.content.ActivityNotFoundException No Activity found to handle Intent { act=android.settings.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS dat=package:io.demo.example }
  • 我也遇到了同样的异常。到目前为止有什么解决方案吗?
  • 我们可以直接打开安卓的原生弹窗询问是否禁用电池优化,查看以下链接。它对我有用。 stackoverflow.com/a/33114136/3497972
【解决方案2】:

尝试以下代码打开忽略电池优化设置页面。

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

不需要向清单文件添加额外的权限。

【讨论】:

    【解决方案3】:

    这是我用的:

    清单:

        <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
        }
    }
    

    例子:

    PowerSaverHelper.prepareIntentForWhiteListingOfBatteryOptimization(this)?.let { startActivity(it) }
    
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-05-04
    • 1970-01-01
    • 1970-01-01
    • 2019-06-02
    • 1970-01-01
    • 1970-01-01
    • 2022-01-10
    • 2021-05-13
    相关资源
    最近更新 更多