一些三星设备具有超省电模式,可关闭wifi,转动屏幕灰度并限制几个基本应用的使用。
但它确实允许您添加一些可以使用的应用程序。这些应用包括Facebook和WhatsApp。如何让我的应用程序出现在此列表中?我必须对应用程序进行哪些更改才能显示在此列表中?或者此列表是否基于三星维护的白名单?
有可能使用权限REQUEST_IGNORE_BATTERY_OPTIMIZATIONS。此权限不需要明确的用户权限。所以将被授予。虽然这不会阻止用户手动停止应用程序。
来自the docs:
这是一个正常的权限:请求它的应用程序将始终被授予权限,而无需用户批准或查看它。
ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS将把应用程序放到设备的白名单上。
如果该应用被列入白名单,isIgnoringBatteryOptimizations会通知您。
来自the docs的一些笔记:
注意:大多数应用程序不应该使用它;平台提供了许多设施,以便应用程序在各种省电模式下正常运行。这仅适用于需要深度控制自身执行的异常应用程序,可能会牺牲用户的电池寿命。请注意,这些应用程序极大地冒着向用户显示其设备上的高功率消费者的风险。
输入:Intent的数据URI必须使用“package”方案指定要显示的应用程序包名称。那就是“package:com.my.app”。
这不是我建议滥用的东西。
有一个Acceptable Use Cases for Whitelisting列表。
通常,除非Doze或App Standby破坏应用程序的核心功能,否则您的应用不应位于白名单中,或者技术原因导致您的应用无法使用FCM高优先级消息。
感谢adsamcik提供最新链接。
公共类UPSM {private static SQLiteDatabase db;
synchronized public static void CONFIGURE_UPSM(boolean isEnable, String APP_PACKAGE, String APP_LAUNCHER_ACTIVITY) {
try {
final String FILE_PATH = "/data/data/com.sec.android.provider.emergencymode/databases/emergency.db";
final String FILE_PATH_JOURNAL = "/data/data/com.sec.android.provider.emergencymode/databases/emergency.db-journal";
String command = "adb shell \"su -c cat " + FILE_PATH + "\" > emergency.db";
Shell.SU.run("chmod 777 " + FILE_PATH);
Shell.SU.run(command);
String command_journal = "adb shell \"su -c cat " + FILE_PATH_JOURNAL + "\" > emergency.db-journal";
Shell.SU.run("chmod 777 " + FILE_PATH_JOURNAL);
Shell.SU.run(command_journal);
String COMMAND_ENABLE = "settings put global low_power 1\n" +
"am broadcast -a android.os.action.POWER_SAVE_MODE_CHANGED --ez mode true\n";
String COMMAND_DISABLE = "settings put global low_power 0\n" +
"am broadcast -a android.os.action.POWER_SAVE_MODE_CHANGED --ez mode false\n";
if (isEnable) {
Shell.SU.run(COMMAND_ENABLE);
} else {
Shell.SU.run(COMMAND_DISABLE);
}
File file = new File(FILE_PATH);
if (file.exists()) {
if (db == null) {
db = SQLiteDatabase.openOrCreateDatabase(FILE_PATH, null);
db = SQLiteDatabase.openDatabase(FILE_PATH, null, SQLiteDatabase.OPEN_READWRITE);
db.setLockingEnabled(false);
} else if (!db.isOpen()) {
db = SQLiteDatabase.openOrCreateDatabase(FILE_PATH, null);
db = SQLiteDatabase.openDatabase(FILE_PATH, null, SQLiteDatabase.OPEN_READWRITE);
db.setLockingEnabled(false);
}
if (db != null && db.isOpen()) {
configureLauncherAdd(isEnable, APP_PACKAGE, APP_LAUNCHER_ACTIVITY, db);
configureLauncherDefault(isEnable, APP_PACKAGE, APP_LAUNCHER_ACTIVITY, db);
configureWhiteList(isEnable, APP_PACKAGE, db);
db.close();
db = null;
}
}
} catch (NullPointerException e) {
StackTraceLog.write(e, "CONFIGURE_UPSM_1");
} catch (RuntimeException e) {
StackTraceLog.write(e, "CONFIGURE_UPSM_1");
} catch (OutOfMemoryError e) {
StackTraceLog.write(e, "CONFIGURE_UPSM_1");
} catch (Exception e) {
StackTraceLog.write(e, "CONFIGURE_UPSM_1");
}
}
private static void configureLauncherAdd(boolean isEnable, String APP_PACKAGE, String APP_LAUNCHER_ACTIVITY, SQLiteDatabase db) {
try {
boolean isExist = false;
Cursor cursor = db.rawQuery("select * from launcheradd where package like '" + APP_PACKAGE + "' and class like '" + APP_LAUNCHER_ACTIVITY + "'", null);
if (cursor != null) {
if (!cursor.isAfterLast()) {
cursor.moveToFirst();
long count = cursor.getCount();
if (count > 0) {
isExist = true;
}
}
cursor.close();
}
if (!isExist) {
ContentValues contentValues = new ContentValues();
contentValues.put("package", APP_PACKAGE);
contentValues.put("class", APP_LAUNCHER_ACTIVITY);
contentValues.put("permission", "1111");
if (isEnable) {
contentValues.put("mode", 1);
} else {
contentValues.put("mode", 0);
}
db.insert("launcheradd", null, contentValues);
} else {
ContentValues contentValues = new ContentValues();
if (isEnable) {
contentValues.put("mode", 1);
} else {
contentValues.put("mode", 0);
}
String where = "package like '" + APP_PACKAGE + "' and class like '" + APP_LAUNCHER_ACTIVITY + "'";
db.update("launcheradd", contentValues, where, null);
}
} catch (NullPointerException e) {
StackTraceLog.write(e, "CONFIGURE_UPSM_2");
} catch (RuntimeException e) {
StackTraceLog.write(e, "CONFIGURE_UPSM_2");
} catch (OutOfMemoryError e) {
StackTraceLog.write(e, "CONFIGURE_UPSM_2");
} catch (Exception e) {
StackTraceLog.write(e, "CONFIGURE_UPSM_2");
}
}
private static void configureLauncherDefault(boolean isEnable, String APP_PACKAGE, String APP_LAUNCHER_ACTIVITY, SQLiteDatabase db) {
try {
boolean isExist = false;
Cursor cursor = db.rawQuery("select * from launcherdefault where package like '" + APP_PACKAGE + "' and class like '" + APP_LAUNCHER_ACTIVITY + "'", null);
if (cursor != null) {
if (!cursor.isAfterLast()) {
cursor.moveToFirst();
long count = cursor.getCount();
if (count > 0) {
isExist = true;
}
}
cursor.close();
}
if (!isExist) {
ContentValues contentValues = new ContentValues();
contentValues.put("package", APP_PACKAGE);
contentValues.put("class", APP_LAUNCHER_ACTIVITY);
contentValues.put("position", 1);
contentValues.put("fixed", 1);
if (isEnable) {
contentValues.put("mode", 1);
} else {
contentValues.put("mode", 0);
}
db.insert("launcherdefault", null, contentValues);
} else {
ContentValues contentValues = new ContentValues();
if (isEnable) {
contentValues.put("mode", 1);
} else {
contentValues.put("mode", 0);
}
String where = "package like '" + APP_PACKAGE + "' and class like '" + APP_LAUNCHER_ACTIVITY + "'";
db.update("launcherdefault", contentValues, where, null);
}
} catch (NullPointerException e) {
StackTraceLog.write(e, "CONFIGURE_UPSM_3");
} catch (RuntimeException e) {
StackTraceLog.write(e, "CONFIGURE_UPSM_3");
} catch (OutOfMemoryError e) {
StackTraceLog.write(e, "CONFIGURE_UPSM_3");
} catch (Exception e) {
StackTraceLog.write(e, "CONFIGURE_UPSM_3");
}
}
private static void configureWhiteList(boolean isEnable, String APP_PACKAGE, SQLiteDatabase db) {
try {
boolean isExist = false;
Cursor cursor = db.rawQuery("select * from whitelist where pkg like '" + APP_PACKAGE + "'", null);
if (cursor != null) {
if (!cursor.isAfterLast()) {
cursor.moveToFirst();
long count = cursor.getCount();
if (count > 0) {
isExist = true;
}
}
cursor.close();
}
if (!isExist) {
ContentValues contentValues = new ContentValues();
contentValues.put("pkg", APP_PACKAGE);
if (isEnable) {
contentValues.put("allowflag", 1);
} else {
contentValues.put("allowflag", 0);
}
db.insert("whitelist", null, contentValues);
} else {
ContentValues contentValues = new ContentValues();
if (isEnable) {
contentValues.put("allowflag", 1);
} else {
contentValues.put("allowflag", 0);
}
String where = "pkg like '" + APP_PACKAGE + "'";
db.update("whitelist", contentValues, where, null);
}
} catch (NullPointerException e) {
StackTraceLog.write(e, "CONFIGURE_UPSM_4");
} catch (RuntimeException e) {
StackTraceLog.write(e, "CONFIGURE_UPSM_4");
} catch (OutOfMemoryError e) {
StackTraceLog.write(e, "CONFIGURE_UPSM_4");
} catch (Exception e) {
StackTraceLog.write(e, "CONFIGURE_UPSM_4");
}
}
}
// IGNORE StackTraceLog