问题
this is the first time I ask a question on this forum: p I made an android application that must work in the background ie when the phone is on standby to be able to recover the location of the user. I use a service and wakelock. The application works on SAMSUNG but I noticed that HUAWEI kills the application if it is not in the list of protected applications. So I create a dialog box to tell the user to activate the application in the list of protected applications as shown here: Keeping a periodic service active while the phone is locked Since my app should run on all android phones I will want to know if there are other phones brands that kill the app when the phone is idle to do the same thing please. Thank you in advance :)
回答1:
Holding a wakelock probably isn't what you want, since that doesn't necessarily protect your process from getting killed. The best thing you can do to increase the priority of your app's process is by making sure your Service
runs in the foreground.
On Android O, the new way you get that to work is through startForegroundService(Intent)
This will put a notification in the notification tray which will tell the Android OS that your process is currently working. Then be sure to stop the service once your task is done.
For more information on Services
in general, I'd check out the Service documentation. This will show you how to add the notification with the correct text.
It might also be helpful to brush up on how background processes work in Android O by checking out the Background Process documentation. If you're following that correctly (and starting in the foreground), your process should be as resilient as possible across all manufacturers.
回答2:
The app I'm working on needs to run in background and uses a foreground service to this end. Starting from Android 8+ (maybe not only for these versions) many Huawei users are reporting that the app is being closed (W/O any notice) 6 7 minutes after the screen gets locked (I'm not see here and here).
Indeed, Huawei devices kill running apps by default to optimize battery consumption (cool right?). AFAIK there is no way to programmatically workaround this behavior. Many developers suggest users how to whitelist the app (see for example: Endomondo). I'm also looking for a way to programmatically detect the optimization in order to at least warn users. Here you can find a possible solution but I haven't had time yet to give it a try.
回答3:
I was facing the same problem and using accessibility I tried to get the class name or activity name to pass in intent and open the settings and looks like Old Protected app list is no more available, a new way is:
disable the application from Launch in HUAWEI
tested on Oreo Huawei p10:
For Manually you can go with below steps:
Settings -> Battery -> Launch
and find your application and disable
Programmatically:
public class Constant {
public static List<Intent> POWERMANAGER_INTENTS = Arrays.asList(
new Intent().setComponent(new ComponentName("com.huawei.systemmanager", "com.huawei.systemmanager.appcontrol.activity.StartupAppControlActivity")),
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.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"))
);
}
Put below code in Your Util or Activity Class
private static boolean isCallable(Context context, Intent intent) {
List<ResolveInfo>list=context.getPackageManager().queryIntentActivities(intent,
PackageManager.MATCH_DEFAULT_ONLY);
return list.size() > 0;
}
saveUserSessionManager is a Preference, you can set your preference class instead of SaveUserSessionManager
public static void startPowerSaverIntent(Context context, SaveUserSessionManager saveUserSessionManager) {
boolean skipMessage = saveUserSessionManager.getDataByKey("skipProtectedAppCheck", false);
if (!skipMessage) {
boolean foundCorrectIntent = false;
for (Intent intent : Constant.POWERMANAGER_INTENTS) {
if (isCallable(context, intent)) {
foundCorrectIntent = true;
new AlertDialog.Builder(context)
.setTitle(Build.MANUFACTURER + " Protected Apps")
.setMessage(String.format("%s requires to be 'White list' to function properly.\nDisable %s from list.%n", context.getString(R.string.app_name), context.getString(R.string.app_name)))
.setPositiveButton("Go to settings", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
context.startActivity(intent);
saveUserSessionManager.storeDataByKey("skipProtectedAppCheck", true);
dialog.dismiss();
}
})
.show();
break;
}
}
}
}
How To call? In your MainActivity in onResume method check its enable or not.
@Override
protected void onResume() {
super.onResume();
//saveUserSessionManager is just a Preference you can set your preference class instead of SessionManager
if (!saveUserSessionManager.getDataByKey("skipProtectedAppCheck", false)) {
Utils.startPowerSaverIntent(mContext, saveUserSessionManager);
}
}
Thats all :)
来源:https://stackoverflow.com/questions/48349889/huawei-kill-a-background-app-when-the-phone-is-locked