Android M startActivity电池优化 [英] Android M startActivity battery optimization

查看:269
本文介绍了Android M startActivity电池优化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个应在附近的地方提醒用户的应用程序. 如果手机处于闲置状态,当然也必须这样做. 有了DOZE,我了解到我必须将我的应用列入白名单,并且这样做,我看到由于

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);

这应该太容易了...因为Google不喜欢这种方法,如果您这样做,则应该禁止Play商店中的应用程序...无可奉告... 好的,所以方法应该是引导用户进行电池设置,然后将您的应用手动添加到DOZE的白名单中.是的,这应该是一堵攀登的大墙……无论如何,这似乎是唯一的方法……现在答案是: 我可以这样使用意图来查看电源使用情况摘要(谢谢克里斯):

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?

谢谢您的回答.

推荐答案

要打开用于选择电池优化的应用程序列表,可以使用以下代码示例:

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

它不需要具有<uses-permission android:name="android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS"/>权限,因此可以将其发布到Google Play(有关详细信息,请检查

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).

注意

添加此行

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

将导致崩溃致命异常:android.content.ActivityNotFoundException未找到任何处理意图的活动{act = android.settings.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS dat = package:io.demo.example}".用户必须在列表中找到该应用,似乎无法直接跳转到我们的应用.

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.

这篇关于Android M startActivity电池优化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆