华为手机上的“保护应用”设置,以及如何处理

我有用于testing应用程序的Android 5.0版本的华为P8。 该应用程序需要在后台运行,因为它跟踪BLE区域。

我发现华为内置了一个名为“保护应用”的function,可以通过手机设置(电池pipe理器>保护应用)访问。 这允许当选应用程序在屏幕closures后继续运行。

明智的华为,但不幸的是,对我来说,它看起来像它的select,即应用程序默认情况下,你必须手动把它们。这不是一个showstopper,因为我可以build议用户在常见问题或打印关于修复的文档,但是我最近安装了Tinder(用于研究目的!),并且注意到它被自动放入了保护列表。

有谁知道我可以为我的应用程序做到这一点? 这是清单中的一个设置吗? 华为已经启用了Tinder,因为它是一个stream行的应用程序?

if("huawei".equalsIgnoreCase(android.os.Build.MANUFACTURER) && !sp.getBoolean("protected",false)) { AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setTitle(R.string.huawei_headline).setMessage(R.string.huawei_text) .setPositiveButton(R.string.go_to_protected, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialogInterface, int i) { Intent intent = new Intent(); intent.setComponent(new ComponentName("com.huawei.systemmanager", "com.huawei.systemmanager.optimize.process.ProtectActivity")); startActivity(intent); sp.edit().putBoolean("protected",true).commit(); } }).create().show(); } 

清单中没有设置,华为启用了Tinder,因为它是一个stream行的应用程序。 没有办法知道应用程序是否受到保护。

无论如何,我在“onCreate”中使用“ifWuaweiAlert()”来显示AlertDialog:

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

我正在使用@Aiuspaktyn解决scheme,它缺less了在用户将应用程序设置为受保护之后停止显示对话框时如何检测的部分。 我使用服务来检查应用程序是否被终止,检查是否存在。