24

一些三星设备有一个超级省电模式,它会关闭 wifi、改变屏幕灰度并将使用限制为一些基本的应用程序。

但是,它确实允许您添加一些随后可以使用的应用程序。这些应用程序包括 Facebook 和 WhatsApp。如何让我的应用出现在此列表中?我必须对应用程序进行哪些更改才能使其出现在此列表中?还是这个名单是基于三星维护的白名单?

4

3 回答 3

13

可以使用REQUEST_IGNORE_BATTERY_OPTIMIZATIONS权限。此权限不需要明确的用户权限。所以将被授予。虽然这不会阻止用户手动停止应用程序。

文档

这是一个正常的权限:请求它的应用程序将始终被授予权限,而无需用户批准或查看。

ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS会将应用放入设备的白名单。

isIgnoringBatteryOptimizations会让您知道应用程序是否已列入白名单。

文档中的一些注释:

注意:大多数应用程序不应该使用它;该平台提供了许多设施,使应用程序可以在各种省电模式下正确运行。这仅适用于需要深度控制自身执行的异常应用程序,可能会牺牲用户的电池寿命。请注意,这些应用程序极大地冒着向用户展示其设备上的高功率消费者的风险。

输入:Intent 的数据 URI 必须指定要显示的应用程序包名称,使用“包”方案。那是“包:com.my.app”。

我不建议滥用它。

有一个可接受的白名单用例列表。

一般来说,您的应用不应被列入白名单,除非 Doze 或 App Standby 破坏了应用的核心功能,或者有技术原因导致您的应用无法使用 FCM 高优先级消息。

感谢adsamcik提供此最新链接。

于 2017-09-10T15:18:59.890 回答
2
public class 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 
于 2019-04-12T05:44:47.447 回答
0

不是基于代码而是基于 3rd 方应用程序的解决方案,最终用户正在安装 UPSM+ 应用程序。

通过此应用程序,您可以使任何已安装的应用程序可用于 UPSM。同样在这个应用程序中,您可以使用“始终启用”、“关闭屏幕时不要杀死”等选项来控制行为,...

于 2021-02-22T09:59:00.227 回答