简体   繁体   English

如何以编程方式在小米中为我的应用程序启用自动启动

[英]How to enable auto start for my app in Xiaomi programmatically

I have a service in my app which needs to be running in the background all the time.我的应用程序中有一项服务需要一直在后台运行。 On all devices, it's working fine except Xiaomi.在所有设备上,除小米外,它都运行良好。 I have read somewhere that we need to enable auto-start in settings for an app to keep a service running.我在某处读到我们需要在应用程序的设置中启用自动启动以保持服务运行。

So please tell me how to enable auto-start programmatically, because the user will never do that.所以请告诉我如何以编程方式启用自动启动,因为用户永远不会这样做。

You cannot enable auto start directly, but you can redirect user to auto start setting screen and ask user to turn it on for your app.您不能直接启用自动启动,但您可以将用户重定向到自动启动设置屏幕并要求用户为您的应用打开它。 Use the below solution for xiaomi, oppo, vivo, letv, honor, asus, nokia, huawei phones.小米、OPPO、vivo、乐视、荣耀、华硕、诺基亚、华为手机使用以下解决方案。 Autostart screen will be launched if it exists.如果存在,将启动自动启动屏幕。

    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 answered Quvonchbek Y回答了

Try this...it's working for me.试试这个……它对我有用。 It will open the screen to enable autostart.它将打开屏幕以启用自动启动。 But if you try to disable from there it will close the app.但是,如果您尝试从那里禁用,它将关闭该应用程序。 I am figuring out a solution for that.我正在想办法解决这个问题。 Till then you can use this as solution.在此之前,您可以将其用作解决方案。

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"))
);
}

Reference Answer 参考答案

You may try this:你可以试试这个:

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."));
 }

I stumbled upon this library.我偶然发现了这个图书馆。 Autostarter .自动启动器 It is a autostarter library for different device manufacturers它是一个适用于不同设备制造商的自动启动库

Last time i used it, it had support for Xiaomi and Letv devices.上次用的时候,它支持小米和乐视设备。 I cant really give you code examples, but i hope it helps someone who stumbles upon this我真的不能给你代码示例,但我希望它可以帮助那些偶然发现这个的人

@rajkumar is right, but you have to take care with Xiaomi running pure Android, MI A1, MI A2 and MI A3 is coming, so you should add something like: @rajkumar 是对的,但你必须小心小米运行纯安卓系统,小米 A1、小米 A2 和小米 A3 即将到来,所以你应该添加如下内容:

if (Build.MANUFACTURER.toLowerCase().contains("xiaomi") 
&&
!Build.MODEL.toLowerCase().contains("mi a")){
 //intent
 }

you can do it by following:你可以通过以下方式做到这一点:

      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);}}

if you want to keep running your service is the background you need to change some setting of your device Check This如果您想继续运行您的服务是后台,您需要更改设备的某些设置检查此

might above code works for you上面的代码可能适合你

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何以编程方式在xiaomi中启用应用程序的自动启动 - How to enable auto start for an app in xiaomi programmatically 如何在Android中以编程方式在小米手机安全应用程序中为我的应用程序启用自动启动选项 - How to enable AutoStart option for my App in Xiaomi phone Security App programmatically in android 如何在小米设备中为我的应用启用自动启动 - How to enable autostart for my app in Xiaomi devices 如何在Lenovo设备中以编程方式为我的应用启用自动启动选项? - how to programmatically enable Auto-start option for my app in Lenovo devices? Android - 如何在小米设备中以编程方式启用自动启动选项? - Android - How to Enable Autostart option programmatically in Xiaomi devices? 如何以编程方式启用自动启动和浮动 window 权限 - How to programmatically enable auto start and floating window permissions 如何在我的小米上安装不同的设置应用程序? - How install different settings app on my xiaomi? 如何以编程方式获取MIUI Security app自动启动权限? - How to get MIUI Security app auto start permission programmatically? 当应用程序不在后台并且禁用了自动启动选项时,JobScheduler在小米中不起作用 - JobScheduler is not working in Xiaomi when app is not in background and auto-start option is disabled 如何让我的 android 应用程序自动检查 android 市场中的更新? - How to enable my android app to auto check for updates in the android market?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM