如何以编程方式获取 MIUI Security 应用程序自动启动权限?
How to get MIUI Security app auto start permission programmatically?
我的小米红米 2 Prime 手机收不到 BOOT_COMPLETE
广播。
我的BroadcastReciever
是---
public class OnBootReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// Setting singleAlarm
SingleAlarmHandler.getInstance().setAlarm(context);
try {
// Sending System Setting broadcast
String offDate = SharedPrefrencesHandler.getInstance(context).readString(SharedPrefrencesConstants.SWITCH_OFF_DATE);
int type = SystemSettingsType.PHONE_SWITCH_ON_OFF.getNumericType();
if (offDate == null)
offDate = "";
SystemSettingsHandler.getSystemSettingsHandler().makeSystemSettingsCall(context, type, offDate);
SharedPrefrencesHandler.getInstance(context).removePrefrence(SharedPrefrencesConstants.SWITCH_OFF_DATE);
} catch (Exception e) {
Log.e(ChaseForceApplication.TAG, e.getMessage());
}
}
}
和清单:
<receiver
android:name=".broadcastlisteners.OnBootReceiver"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
经许可:
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
现在我的小米 Redmi 2 Prime 手机没有收到 BOOT COMPLETE 广播,因为没有设置闹钟。但在其他 android 手机中它工作正常。
我搜索了一下,发现是MIUI固件的问题。在此类移动设备中,他们提供了一个内置的安全应用程序,除非您在该安全应用程序中允许自动启动权限,否则您无法进行广播(任何通知)。
一旦您在该应用程序中检查了该权限,您就会开始接收广播。
现在我的问题是:
如何通过编程获取MIUI安全应用自启动权限(红米等手机)?
您需要在 小米的内置安全应用程序中授予权限。
1. open the security app
2. go to permissions
3. go to auto start
4. enable the applications that you want to keep running in the background!
这对我有用..!
这个问题已经在两个 Stack Overflow 线程中有了答案:
线程 #1
String xiaomi = "Xiaomi";
final String CALC_PACKAGE_NAME = "com.miui.securitycenter";
final String CALC_PACKAGE_ACITIVITY = "com.miui.permcenter.autostart.AutoStartManagementActivity";
if (deviceManufacturer.equalsIgnoreCase(xiaomi)) {
DisplayUtils.showDialog(activity, "Ask for permission", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
try {
Intent intent = new Intent();
intent.setComponent(new ComponentName(CALC_PACKAGE_NAME, CALC_PACKAGE_ACITIVITY));
activity.startActivity(intent);
} catch (ActivityNotFoundException e) {
Logger.e(TAG, "Failed to launch AutoStart Screen ", e);
} catch (Exception e) {
Logger.e(TAG, "Failed to launch AutoStart Screen ", e);
}
}
}, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
}
});
}
线程 #2
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);
}
对于华为设备上的类似问题:
private void ifHuaweiAlert() {
final SharedPreferences settings = getSharedPreferences("ProtectedApps", MODE_PRIVATE);
final String saveIfSkip = "skipProtectedAppsMessage";
boolean skipMessage = settings.getBoolean(saveIfSkip, false);
if (!skipMessage) {
final SharedPreferences.Editor editor = settings.edit();
Intent intent = new Intent();
intent.setClassName("com.huawei.systemmanager", "com.huawei.systemmanager.optimize.process.ProtectActivity");
if (isCallable(intent)) {
final AppCompatCheckBox dontShowAgain = new AppCompatCheckBox(this);
dontShowAgain.setText("Do not show again");
dontShowAgain.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
editor.putBoolean(saveIfSkip, isChecked);
editor.apply();
}
});
new AlertDialog.Builder(this)
.setIcon(android.R.drawable.ic_dialog_alert)
.setTitle("Huawei Protected Apps")
.setMessage(String.format("%s requires to be enabled in 'Protected Apps' to function properly.%n", getString(R.string.app_name)))
.setView(dontShowAgain)
.setPositiveButton("Protected Apps", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
huaweiProtectedApps();
}
})
.setNegativeButton(android.R.string.cancel, null)
.show();
} else {
editor.putBoolean(saveIfSkip, true);
editor.apply();
}
}
}
private boolean isCallable(Intent intent) {
List<ResolveInfo> list = getPackageManager().queryIntentActivities(intent,
PackageManager.MATCH_DEFAULT_ONLY);
return list.size() > 0;
}
private void huaweiProtectedApps() {
try {
String cmd = "am start -n com.huawei.systemmanager/.optimize.process.ProtectActivity";
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
cmd += " --user " + getUserSerial();
}
Runtime.getRuntime().exec(cmd);
} catch (IOException ignored) {
}
}
private String getUserSerial() {
//noinspection ResourceType
Object userManager = getSystemService("user");
if (null == userManager) return "";
try {
Method myUserHandleMethod = android.os.Process.class.getMethod("myUserHandle", (Class<?>[]) null);
Object myUserHandle = myUserHandleMethod.invoke(android.os.Process.class, (Object[]) null);
Method getSerialNumberForUser = userManager.getClass().getMethod("getSerialNumberForUser", myUserHandle.getClass());
Long userSerial = (Long) getSerialNumberForUser.invoke(userManager, myUserHandle);
if (userSerial != null) {
return String.valueOf(userSerial);
} else {
return "";
}
} catch (NoSuchMethodException | IllegalArgumentException | InvocationTargetException | IllegalAccessException ignored) {
}
return "";
}
我的小米红米 2 Prime 手机收不到 BOOT_COMPLETE
广播。
我的BroadcastReciever
是---
public class OnBootReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// Setting singleAlarm
SingleAlarmHandler.getInstance().setAlarm(context);
try {
// Sending System Setting broadcast
String offDate = SharedPrefrencesHandler.getInstance(context).readString(SharedPrefrencesConstants.SWITCH_OFF_DATE);
int type = SystemSettingsType.PHONE_SWITCH_ON_OFF.getNumericType();
if (offDate == null)
offDate = "";
SystemSettingsHandler.getSystemSettingsHandler().makeSystemSettingsCall(context, type, offDate);
SharedPrefrencesHandler.getInstance(context).removePrefrence(SharedPrefrencesConstants.SWITCH_OFF_DATE);
} catch (Exception e) {
Log.e(ChaseForceApplication.TAG, e.getMessage());
}
}
}
和清单:
<receiver
android:name=".broadcastlisteners.OnBootReceiver"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
经许可:
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
现在我的小米 Redmi 2 Prime 手机没有收到 BOOT COMPLETE 广播,因为没有设置闹钟。但在其他 android 手机中它工作正常。
我搜索了一下,发现是MIUI固件的问题。在此类移动设备中,他们提供了一个内置的安全应用程序,除非您在该安全应用程序中允许自动启动权限,否则您无法进行广播(任何通知)。
一旦您在该应用程序中检查了该权限,您就会开始接收广播。
现在我的问题是:
如何通过编程获取MIUI安全应用自启动权限(红米等手机)?
您需要在 小米的内置安全应用程序中授予权限。
1. open the security app
2. go to permissions
3. go to auto start
4. enable the applications that you want to keep running in the background!
这对我有用..!
这个问题已经在两个 Stack Overflow 线程中有了答案:
线程 #1
String xiaomi = "Xiaomi";
final String CALC_PACKAGE_NAME = "com.miui.securitycenter";
final String CALC_PACKAGE_ACITIVITY = "com.miui.permcenter.autostart.AutoStartManagementActivity";
if (deviceManufacturer.equalsIgnoreCase(xiaomi)) {
DisplayUtils.showDialog(activity, "Ask for permission", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
try {
Intent intent = new Intent();
intent.setComponent(new ComponentName(CALC_PACKAGE_NAME, CALC_PACKAGE_ACITIVITY));
activity.startActivity(intent);
} catch (ActivityNotFoundException e) {
Logger.e(TAG, "Failed to launch AutoStart Screen ", e);
} catch (Exception e) {
Logger.e(TAG, "Failed to launch AutoStart Screen ", e);
}
}
}, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
}
});
}
线程 #2
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);
}
对于华为设备上的类似问题:
private void ifHuaweiAlert() {
final SharedPreferences settings = getSharedPreferences("ProtectedApps", MODE_PRIVATE);
final String saveIfSkip = "skipProtectedAppsMessage";
boolean skipMessage = settings.getBoolean(saveIfSkip, false);
if (!skipMessage) {
final SharedPreferences.Editor editor = settings.edit();
Intent intent = new Intent();
intent.setClassName("com.huawei.systemmanager", "com.huawei.systemmanager.optimize.process.ProtectActivity");
if (isCallable(intent)) {
final AppCompatCheckBox dontShowAgain = new AppCompatCheckBox(this);
dontShowAgain.setText("Do not show again");
dontShowAgain.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
editor.putBoolean(saveIfSkip, isChecked);
editor.apply();
}
});
new AlertDialog.Builder(this)
.setIcon(android.R.drawable.ic_dialog_alert)
.setTitle("Huawei Protected Apps")
.setMessage(String.format("%s requires to be enabled in 'Protected Apps' to function properly.%n", getString(R.string.app_name)))
.setView(dontShowAgain)
.setPositiveButton("Protected Apps", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
huaweiProtectedApps();
}
})
.setNegativeButton(android.R.string.cancel, null)
.show();
} else {
editor.putBoolean(saveIfSkip, true);
editor.apply();
}
}
}
private boolean isCallable(Intent intent) {
List<ResolveInfo> list = getPackageManager().queryIntentActivities(intent,
PackageManager.MATCH_DEFAULT_ONLY);
return list.size() > 0;
}
private void huaweiProtectedApps() {
try {
String cmd = "am start -n com.huawei.systemmanager/.optimize.process.ProtectActivity";
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
cmd += " --user " + getUserSerial();
}
Runtime.getRuntime().exec(cmd);
} catch (IOException ignored) {
}
}
private String getUserSerial() {
//noinspection ResourceType
Object userManager = getSystemService("user");
if (null == userManager) return "";
try {
Method myUserHandleMethod = android.os.Process.class.getMethod("myUserHandle", (Class<?>[]) null);
Object myUserHandle = myUserHandleMethod.invoke(android.os.Process.class, (Object[]) null);
Method getSerialNumberForUser = userManager.getClass().getMethod("getSerialNumberForUser", myUserHandle.getClass());
Long userSerial = (Long) getSerialNumberForUser.invoke(userManager, myUserHandle);
if (userSerial != null) {
return String.valueOf(userSerial);
} else {
return "";
}
} catch (NoSuchMethodException | IllegalArgumentException | InvocationTargetException | IllegalAccessException ignored) {
}
return "";
}