[英]How to enable auto start for my app in Xiaomi programmatically
我的應用程序中有一項服務需要一直在后台運行。 在所有設備上,除小米外,它都運行良好。 我在某處讀到我們需要在應用程序的設置中啟用自動啟動以保持服務運行。
所以請告訴我如何以編程方式啟用自動啟動,因為用戶永遠不會這樣做。
您不能直接啟用自動啟動,但您可以將用戶重定向到自動啟動設置屏幕並要求用戶為您的應用打開它。 小米、OPPO、vivo、樂視、榮耀、華碩、諾基亞、華為手機使用以下解決方案。 如果存在,將啟動自動啟動屏幕。
try {
val intent = Intent()
val manufacturer = Build.MANUFACTURER
when {
"xiaomi".equals(manufacturer, ignoreCase = true) -> {
intent.component = ComponentName(
"com.miui.securitycenter",
"com.miui.permcenter.autostart.AutoStartManagementActivity"
)
}
"oppo".equals(manufacturer, ignoreCase = true) -> {
intent.component = ComponentName(
"com.coloros.safecenter",
"com.coloros.safecenter.permission.startup.StartupAppListActivity"
)
}
"vivo".equals(manufacturer, ignoreCase = true) -> {
intent.component = ComponentName(
"com.vivo.permissionmanager",
"com.vivo.permissionmanager.activity.BgStartUpManagerActivity"
)
}
"letv".equals(manufacturer, ignoreCase = true) -> {
intent.component = ComponentName(
"com.letv.android.letvsafe",
"com.letv.android.letvsafe.AutobootManageActivity"
)
}
"honor".equals(manufacturer, ignoreCase = true) -> {
intent.component = ComponentName(
"com.huawei.systemmanager",
"com.huawei.systemmanager.optimize.process.ProtectActivity"
)
}
"asus".equals(manufacturer, ignoreCase = true) -> {
intent.component = ComponentName(
"com.asus.mobilemanager",
"com.asus.mobilemanager.powersaver.PowerSaverSettings"
)
}
"nokia".equals(manufacturer, ignoreCase = true) -> {
intent.component = ComponentName(
"com.evenwell.powersaving.g3",
"com.evenwell.powersaving.g3.exception.PowerSaverExceptionActivity"
)
}
"huawei".equals(manufacturer, ignoreCase = true) -> {
intent.component = ComponentName(
"com.huawei.systemmanager",
"com.huawei.systemmanager.startupmgr.ui.StartupNormalAppListActivity"
)
}
}
startActivity(intent)
} catch (e: Exception) {
/*Timber.e(e)*/
}
Quvonchbek Y回答了
試試這個……它對我有用。 它將打開屏幕以啟用自動啟動。 但是,如果您嘗試從那里禁用,它將關閉該應用程序。 我正在想辦法解決這個問題。 在此之前,您可以將其用作解決方案。
String manufacturer = "xiaomi";
if(manufacturer.equalsIgnoreCase(android.os.Build.MANUFACTURER)) {
//this will open auto start screen where user can enable permission for your app
Intent intent = new Intent();
intent.setComponent(new ComponentName("com.miui.securitycenter", "com.miui.permcenter.autostart.AutoStartManagementActivity"));
startActivity(intent);
}
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.os.Build;
import android.support.v7.widget.AppCompatCheckBox;
import android.widget.CompoundButton;
import java.util.List;
public class Utils {
public static void startPowerSaverIntent(Context context) {
SharedPreferences settings = context.getSharedPreferences("ProtectedApps", Context.MODE_PRIVATE);
boolean skipMessage = settings.getBoolean("skipProtectedAppCheck", false);
if (!skipMessage) {
final SharedPreferences.Editor editor = settings.edit();
boolean foundCorrectIntent = false;
for (Intent intent : Constants.POWERMANAGER_INTENTS) {
if (isCallable(context, intent)) {
foundCorrectIntent = true;
final AppCompatCheckBox dontShowAgain = new AppCompatCheckBox(context);
dontShowAgain.setText("Do not show again");
dontShowAgain.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
editor.putBoolean("skipProtectedAppCheck", isChecked);
editor.apply();
}
});
new AlertDialog.Builder(context)
.setTitle(Build.MANUFACTURER + " Protected Apps")
.setMessage(String.format("%s requires to be enabled in 'Protected Apps' to function properly.%n", context.getString(R.string.app_name)))
.setView(dontShowAgain)
.setPositiveButton("Go to settings", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
context.startActivity(intent);
}
})
.setNegativeButton(android.R.string.cancel, null)
.show();
break;
}
}
if (!foundCorrectIntent) {
editor.putBoolean("skipProtectedAppCheck", true);
editor.apply();
}
}
}
private static boolean isCallable(Context context, Intent intent) {
List<ResolveInfo> list = context.getPackageManager().queryIntentActivities(intent,
PackageManager.MATCH_DEFAULT_ONLY);
return list.size() > 0;
}
}
}
import android.content.ComponentName;
import android.content.Intent;
import java.util.Arrays;
import java.util.List;
public class Constants {
public static List<Intent> POWERMANAGER_INTENTS = Arrays.asList(
new Intent().setComponent(new ComponentName("com.miui.securitycenter", "com.miui.permcenter.autostart.AutoStartManagementActivity")),
new Intent().setComponent(new ComponentName("com.letv.android.letvsafe", "com.letv.android.letvsafe.AutobootManageActivity")),
new Intent().setComponent(new ComponentName("com.huawei.systemmanager", "com.huawei.systemmanager.optimize.process.ProtectActivity")),
new Intent().setComponent(new ComponentName("com.coloros.safecenter", "com.coloros.safecenter.permission.startup.StartupAppListActivity")),
new Intent().setComponent(new ComponentName("com.coloros.safecenter", "com.coloros.safecenter.startupapp.StartupAppListActivity")),
new Intent().setComponent(new ComponentName("com.oppo.safe", "com.oppo.safe.permission.startup.StartupAppListActivity")),
new Intent().setComponent(new ComponentName("com.iqoo.secure", "com.iqoo.secure.ui.phoneoptimize.AddWhiteListActivity")),
new Intent().setComponent(new ComponentName("com.iqoo.secure", "com.iqoo.secure.ui.phoneoptimize.BgStartUpManager")),
new Intent().setComponent(new ComponentName("com.vivo.permissionmanager", "com.vivo.permissionmanager.activity.BgStartUpManagerActivity")),
new Intent().setComponent(new ComponentName("com.asus.mobilemanager", "com.asus.mobilemanager.entry.FunctionActivity")).setData(android.net.Uri.parse("mobilemanager://function/entry/AutoStart"))
);
}
你可以試試這個:
if ("xiaomi".equalsIgnoreCase(str))
{
intent.setComponent(new ComponentName("com.miui.securitycenter", "com.miui.permcenter.autostart.AutoStartManagementActivity"));
}
else if ("oppo".equalsIgnoreCase(str))
{
intent.setComponent(new ComponentName("com.coloros.safecenter", "com.coloros.safecenter.permission.startup.StartupAppListActivity"));
}
else if ("vivo".equalsIgnoreCase(str))
{
intent.setComponent(new ComponentName("com.iqoo.secure", "com.iqoo.secure.MainGuideActivity."));
}
我偶然發現了這個圖書館。 自動啟動器。 它是一個適用於不同設備制造商的自動啟動庫
上次用的時候,它支持小米和樂視設備。 我真的不能給你代碼示例,但我希望它可以幫助那些偶然發現這個的人
@rajkumar 是對的,但你必須小心小米運行純安卓系統,小米 A1、小米 A2 和小米 A3 即將到來,所以你應該添加如下內容:
if (Build.MANUFACTURER.toLowerCase().contains("xiaomi")
&&
!Build.MODEL.toLowerCase().contains("mi a")){
//intent
}
你可以通過以下方式做到這一點:
if (manufactXiaomi.equalsIgnoreCase(android.os.Build.MANUFACTURER)) {
if (!session.getVisibilityOfAutoStartDialog()) {Intent intent = new Intent();
intent.setComponent(new ComponentName("com.miui.securitycenter", "com.miui.permcenter.autostart.AutoStartManagementActivity"));
startActivity(intent);}}
如果您想繼續運行您的服務是后台,您需要更改設備的某些設置檢查此
上面的代碼可能適合你
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.