Android M startActivity电池优化
Android M startActivity battery optimization
我正在开发一个应用程序,如果用户靠近某个地方,它应该会提醒用户。
如果 phone 处于空闲状态,当然也必须这样做。
现在有了 DOZE,我明白我必须将我的应用程序列入白名单,为此我看到我可以通过操作请求启动一个意图,这要感谢 Buddy 在
中的回答
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 的白名单中……是的,这应该是一堵需要攀登的大墙……无论如何似乎是唯一的方法……现在答案是:
我可以使用 intent 进入电源使用摘要,这样 (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);
}
但是如何直接进入应用列表选择电池优化?
感谢您的回答。
要打开用于选择电池优化的应用程序列表,您可以使用此代码示例:
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播放应该就可以了(详情请查看讨论帖和本问题的评论).
注意
添加这一行
intent.setData(Uri.parse("package:" + mContext.getPackageName()));
会导致崩溃 "Fatal Exception: android.content.ActivityNotFoundException No Activity found to handle Intent { act=android.settings.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS dat=package:io.demo.example }"。用户必须在列表中找到应用程序,似乎没有办法直接跳转到我们的应用程序。
尝试使用以下代码打开“忽略电池优化设置”页面。
private void openPowerSettings() {
startActivityForResult(new Intent(android.provider.Settings.ACTION_IGNORE_BATTERY_OPTIMIZATION_SETTINGS), 0);
}
清单文件不需要额外的权限。
这是我用的:
清单:
<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) }
我正在开发一个应用程序,如果用户靠近某个地方,它应该会提醒用户。
如果 phone 处于空闲状态,当然也必须这样做。
现在有了 DOZE,我明白我必须将我的应用程序列入白名单,为此我看到我可以通过操作请求启动一个意图,这要感谢 Buddy 在
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 的白名单中……是的,这应该是一堵需要攀登的大墙……无论如何似乎是唯一的方法……现在答案是: 我可以使用 intent 进入电源使用摘要,这样 (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);
}
但是如何直接进入应用列表选择电池优化?
感谢您的回答。
要打开用于选择电池优化的应用程序列表,您可以使用此代码示例:
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播放应该就可以了(详情请查看
注意
添加这一行
intent.setData(Uri.parse("package:" + mContext.getPackageName()));
会导致崩溃 "Fatal Exception: android.content.ActivityNotFoundException No Activity found to handle Intent { act=android.settings.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS dat=package:io.demo.example }"。用户必须在列表中找到应用程序,似乎没有办法直接跳转到我们的应用程序。
尝试使用以下代码打开“忽略电池优化设置”页面。
private void openPowerSettings() {
startActivityForResult(new Intent(android.provider.Settings.ACTION_IGNORE_BATTERY_OPTIMIZATION_SETTINGS), 0);
}
清单文件不需要额外的权限。
这是我用的:
清单:
<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) }