我正在开发一个应用程序,如果用户靠近某个地方,它应该提醒用户。 当然,如果手机处于空闲状态,也必须这样做。 现在有了 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);
嗯,这应该太容易了...因为谷歌不喜欢这种方法,如果你这样做,你的应用程序应该被禁止从游戏商店...没有评论... 好的,所以方法应该是让用户进入电池设置并手动将您的应用程序添加到 DOZE 的白名单中...是的,这应该是一堵需要攀爬的大墙...无论如何似乎是唯一的方法...现在答案是: 我可以使用意图来查看电量使用摘要,这样(谢谢 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);
}
但是如何直接进入应用程序列表选择电池优化呢?
感谢您的回答。
要打开应用程序列表以选择电池优化,您可以使用以下代码示例:
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帖子和这个问题的评论)。
注意
添加此行
intent.setData(Uri.parse("package:" + mContext.getPackageName()));
将导致崩溃“致命异常:android.content.ActivityNotFoundException 找不到处理 Intent { act=android.settings.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS dat=package:io.demo.example } 的活动”。用户必须在列表中找到该应用程序,似乎无法直接跳转到我们的应用程序。
这就是我用的:
清单:
<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.core.content.ContextCompat
object PowerSaverHelper {
enum class WhiteListedInBatteryOptimizationsState {
WHITE_LISTED, NOT_WHITE_LISTED, ERROR_GETTING_STATE, IRRELEVANT_OLD_ANDROID_API
}
fun getWhiteListedInBatteryOptimizationsState(
context: Context,
packageName: String = context.packageName
): WhiteListedInBatteryOptimizationsState {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M)
return WhiteListedInBatteryOptimizationsState.IRRELEVANT_OLD_ANDROID_API
val pm = context.getSystemService(Context.POWER_SERVICE) as PowerManager?
?: return WhiteListedInBatteryOptimizationsState.ERROR_GETTING_STATE
return if (pm.isIgnoringBatteryOptimizations(packageName))
WhiteListedInBatteryOptimizationsState.WHITE_LISTED else WhiteListedInBatteryOptimizationsState.NOT_WHITE_LISTED
}
@SuppressLint("BatteryLife", "InlinedApi")
fun prepareIntentForWhiteListingOfBatteryOptimization(
context: Context,
packageName: String = context.packageName,
alsoWhenWhiteListed: Boolean = false
): Intent? {
when (getWhiteListedInBatteryOptimizationsState(context, packageName)) {
WhiteListedInBatteryOptimizationsState.WHITE_LISTED -> {
if (alsoWhenWhiteListed)
return Intent(Settings.ACTION_IGNORE_BATTERY_OPTIMIZATION_SETTINGS)
}
WhiteListedInBatteryOptimizationsState.NOT_WHITE_LISTED -> {
if (ContextCompat.checkSelfPermission(context, Manifest.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS) == PackageManager.PERMISSION_DENIED)
return Intent(Settings.ACTION_IGNORE_BATTERY_OPTIMIZATION_SETTINGS)
return Intent(Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS)
.setData(Uri.parse("package:$packageName"))
}
WhiteListedInBatteryOptimizationsState.ERROR_GETTING_STATE -> {
return Intent(Settings.ACTION_IGNORE_BATTERY_OPTIMIZATION_SETTINGS)
}
WhiteListedInBatteryOptimizationsState.IRRELEVANT_OLD_ANDROID_API -> {
}
}
return null
}
}
示例:
PowerSaverHelper.prepareIntentForWhiteListingOfBatteryOptimization(this)?.let { startActivity(it) }
尝试以下代码打开忽略电池优化设置页面。
private void openPowerSettings() {
startActivityForResult(new Intent(android.provider.Settings.ACTION_IGNORE_BATTERY_OPTIMIZATION_SETTINGS), 0);
}
无需向清单文件添加额外权限。