Android 如何以编程方式在小米中为我的应用程序启用自动启动
我的应用程序中有一项服务需要一直在后台运行。在所有的设备上,除了小米,它工作正常。我在某个地方读到,我们需要在应用程序的设置中启用自动启动,以保持服务运行Android 如何以编程方式在小米中为我的应用程序启用自动启动,android,service,Android,Service,我的应用程序中有一项服务需要一直在后台运行。在所有的设备上,除了小米,它工作正常。我在某个地方读到,我们需要在应用程序的设置中启用自动启动,以保持服务运行 因此,请告诉我如何以编程方式启用自动启动,因为用户永远不会这样做。试试这个……它对我有用。它将打开屏幕以启用自动启动。但是如果你尝试从那里禁用它,它将关闭应用程序。我正在想办法解决这个问题。在那之前,你可以用这个作为解决方案 String manufacturer = "xiaomi"; if(manufacturer.equ
因此,请告诉我如何以编程方式启用自动启动,因为用户永远不会这样做。试试这个……它对我有用。它将打开屏幕以启用自动启动。但是如果你尝试从那里禁用它,它将关闭应用程序。我正在想办法解决这个问题。在那之前,你可以用这个作为解决方案
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);
}
您可以通过以下方式执行此操作:
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);}}
如果要在后台继续运行服务,则需要更改设备的某些设置
上述代码可能适用于您您无法直接启用自动启动,但您可以将用户重定向到自动启动设置屏幕,并要求用户为您的应用程序打开它。小米、oppo、维梧、乐视、荣光、华硕、诺基亚、华为手机使用以下解决方案。自动启动屏幕(如果存在)将启动
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回答您可以尝试以下方法:
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."));
}
我偶然发现了这个图书馆。 . 它是针对不同设备制造商的AutoStart库
上次我用它时,它支持小米和乐视设备。我真的无法给出代码示例,但我希望它能帮助偶然发现这一点的人@rajkumar是对的,但你必须注意小米运行纯Android,MI A1、MI A2和MI A3即将出现,因此你应该添加如下内容:
if (Build.MANUFACTURER.toLowerCase().contains("xiaomi")
&&
!Build.MODEL.toLowerCase().contains("mi a")){
//intent
}
导入android.app.AlertDialog;
导入android.content.Context;
导入android.content.DialogInterface;
导入android.content.Intent;
导入android.content.SharedReferences;
导入android.content.pm.PackageManager;
导入android.content.pm.ResolveInfo;
导入android.os.Build;
导入android.support.v7.widget.AppCompatCheckBox;
导入android.widget.CompoundButton;
导入java.util.List;
公共类UTIL{
公共静态void startpowersavirecontent(上下文){
SharedReferences设置=context.getSharedReferences(“ProtectedApps”,context.MODE\u PRIVATE);
boolean skipMessage=settings.getBoolean(“skipProtectedAppCheck”,false);
如果(!skipMessage){
final sharedReferences.Editor=settings.edit();
布尔foundCorrectIntent=false;
for(意图:常量。POWERMANAGER\u意图){
if(可调用(上下文、意图)){
foundCorrectIntent=true;
final-AppCompatCheckBox dontshowleach=新的AppCompatCheckBox(上下文);
setText(“不再显示”);
setOnCheckedChangeListener(新建CompoundButton.OnCheckedChangeListener()){
@凌驾
检查更改后的公共无效(复合按钮视图,布尔值已检查){
编辑器.putBoolean(“skipProtectedAppCheck”,已选中);
editor.apply();
}
});
新建AlertDialog.Builder(上下文)
.setTitle(Build.MANUFACTURER+“受保护的应用程序”)
.setMessage(String.format(“%s需要在“受保护的应用程序”中启用才能正常工作。%n”,context.getString(R.String.app_name)))
.setView(不再显示)
.setPositiveButton(“转到设置”,新建DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog,int which){
背景。开始触觉(意图);
}
})
.setNegativeButton(android.R.string.cancel,null)
.show();
打破
}
}
如果(!foundCorrectIntent){
编辑器.putBoolean(“skipProtectedAppCheck”,true);
editor.apply();
}
}
}
私有静态布尔值可调用(上下文、意图){
List List=context.getPackageManager().QueryInputActivities(intent,
PackageManager.MATCH_(仅限默认值);
返回列表.size()>0;
}
}
}
导入android.content.ComponentName;
导入android.content.Intent;
导入java.util.array;
导入java.util.List;
公共类常量{
公共静态列表POWERMANAGER\u INTENTS=Arrays.asList(
new Intent().setComponent(新组件名称(“com.miui.securitycenter”、“com.miui.permcenter.autostart.AutoStartManagementActivity”),
new Intent().setComponent(新组件名称(“com.letv.android.letvsafe”,“com.letv.android.letvsafe.AutobootManageActivity”),
new Intent().setComponent(新组件名称(“com.huawei.systemmanager”、“com.huawei.systemmanager.optimize.process.ProtectActivity”),
new Intent().setComponent(新组件名称(“com.coloros.safecenter”、“com.coloros.safecenter.permission.startup.StartupAppListActivity”),
new Intent().setComponent(新组件名称(“com.coloros.safecenter”、“com.coloros.safecenter.startupapp.StartupAppListActivity”),
new Intent().setComponent(新组件名称(“com.oppo.safe”、“com.oppo.safe.permission.startup.StartupAppListActivity”),
new Intent().setComponent(新组件名称(“com.iqoo.secure”、“com.iqoo.secure.ui.phoneoptimize.AddWhiteListActivity”),
new Intent().setComponent(新组件名称(“com.iqoo.secure”、“com.iqoo.secure.ui.phoneoptimize.bgstartmanager”),
new Intent().setComponent(新组件名称(“com.vivo.permissionmanager”、“com.vivo.permissionmanager.activity.BgStartUpManagerActivity”),
new Intent().setComponent(新组件名(“com.asus.mobilemanager”、“com.asus.mobilemanager.entry.FunctionActivity”)).setData(安卓.net.Uri.parse(“mobilemanager://function/entry/AutoStart"))
);
}
除非小米的api允许您访问,否则您无法从代码中执行此操作
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"))
);
}