我制作了一个应用程序,这是一个按间隔存储位置信息并将其发送到服务器的应用程序 服务正在后台或forground模式。它在常见的Android手机中运行良好。 顺便说一句应用程序在oppo和vivo中运行不佳 启动第一个活动后,app(此活动)在3~5分钟后被杀死。
try {
Intent intent = new Intent();
String manufacturer = android.os.Build.MANUFACTURER;
if ("xiaomi".equalsIgnoreCase(manufacturer)) {
intent.setComponent(new ComponentName("com.miui.securitycenter", "com.miui.permcenter.autostart.AutoStartManagementActivity"));
} else if ("oppo".equalsIgnoreCase(manufacturer)) {
intent.setComponent(new ComponentName("com.coloros.safecenter", "com.coloros.safecenter.permission.startup.StartupAppListActivity"));
} else if ("vivo".equalsIgnoreCase(manufacturer)) {
//intent.setComponent(new ComponentName("com.vivo.permissionmanager", "com.vivo.permissionmanager.activity.BgStartUpManagerActivity"));
intent.setComponent(new ComponentName("com.iqoo.secure", "com.iqoo.secure.ui.phoneoptimize.AddWhiteListActivity"));
}
List<ResolveInfo> list = getPackageManager().queryIntentActivities(intent,
PackageManager.MATCH_DEFAULT_ONLY);
if (list.size() > 0) {
startActivity(intent);
}
} catch (Exception e) {
Crashlytics.logException(e);
}
Account account = createSyncAccount(this);
Intent locationIntent = new Intent(this, UserLocationService.class);
locationIntent.putExtra("extra.account", account);
startService(locationIntent);
Intent i = new Intent(this, SynchronizeService.class);
SharedPreferences prefs =
PreferenceManager.getDefaultSharedPreferences(this);
long synchTime =
Long.parseLong(prefs.getString(Constants.SETTINGS_SYNCHRONIZE_INTERVAL, "210"));
i.setAction(SynchronizeService.ACTION_SET_ALARM);
if (!UserLocationService.isRepeated)
i.putExtra(SynchronizeService.EXTRA_TIME, Long.valueOf("30"));
else i.putExtra(SynchronizeService.EXTRA_TIME, synchTime);
startService(i);
这是我的来源。 你能帮助我吗?我没有找到正确的解决方案。如果有人有解决方案吗?你能让我知道吗?
答案 0 :(得分:0)
是的,这是因为这些设备默认只允许某些白名单应用的后台服务。如果您的应用也必须像这样工作,则意味着您必须从设置中启用自动启动,以下代码将帮助您让用户为您的应用启用自动启动。如果 autoStart 是启用后,您的服务将在后台运行良好。
private void enableAutoStart() {
if (Build.BRAND.equalsIgnoreCase("xiaomi")) {
new MaterialDialog.Builder(MainActivity.this).title("Enable AutoStart")
.content(
"Please allow QuickAlert to always run in the background,else our services can't be accessed when you are in distress")
.theme(Theme.LIGHT)
.positiveText("ALLOW")
.onPositive(new MaterialDialog.SingleButtonCallback() {
@Override
public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
Intent intent = new Intent();
intent.setComponent(new ComponentName("com.miui.securitycenter",
"com.miui.permcenter.autostart.AutoStartManagementActivity"));
startActivity(intent);
}
})
.show();
} else if (Build.BRAND.equalsIgnoreCase("Letv")) {
new MaterialDialog.Builder(MainActivity.this).title("Enable AutoStart")
.content(
"Please allow QuickAlert to always run in the background,else our services can't be accessed when you are in distress")
.theme(Theme.LIGHT)
.positiveText("ALLOW")
.onPositive(new MaterialDialog.SingleButtonCallback() {
@Override
public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
Intent intent = new Intent();
intent.setComponent(new ComponentName("com.letv.android.letvsafe",
"com.letv.android.letvsafe.AutobootManageActivity"));
startActivity(intent);
}
})
.show();
} else if (Build.BRAND.equalsIgnoreCase("Honor")) {
new MaterialDialog.Builder(MainActivity.this).title("Enable AutoStart")
.content(
"Please allow QuickAlert to always run in the background,else our services can't be accessed when you are in distress")
.theme(Theme.LIGHT)
.positiveText("ALLOW")
.onPositive(new MaterialDialog.SingleButtonCallback() {
@Override
public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
Intent intent = new Intent();
intent.setComponent(new ComponentName("com.huawei.systemmanager",
"com.huawei.systemmanager.optimize.process.ProtectActivity"));
startActivity(intent);
}
})
.show();
} else if (Build.MANUFACTURER.equalsIgnoreCase("oppo")) {
new MaterialDialog.Builder(MainActivity.this).title("Enable AutoStart")
.content(
"Please allow QuickAlert to always run in the background,else our services can't be accessed when you are in distress")
.theme(Theme.LIGHT)
.positiveText("ALLOW")
.onPositive(new MaterialDialog.SingleButtonCallback() {
@Override
public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
try {
Intent intent = new Intent();
intent.setClassName("com.coloros.safecenter",
"com.coloros.safecenter.permission.startup.StartupAppListActivity");
startActivity(intent);
} catch (Exception e) {
try {
Intent intent = new Intent();
intent.setClassName("com.oppo.safe",
"com.oppo.safe.permission.startup.StartupAppListActivity");
startActivity(intent);
} catch (Exception ex) {
try {
Intent intent = new Intent();
intent.setClassName("com.coloros.safecenter",
"com.coloros.safecenter.startupapp.StartupAppListActivity");
startActivity(intent);
} catch (Exception exx) {
}
}
}
}
})
.show();
} else if (Build.MANUFACTURER.contains("vivo")) {
new MaterialDialog.Builder(MainActivity.this).title("Enable AutoStart")
.content(
"Please allow QuickAlert to always run in the background.Our app runs in background to detect when you are in distress.")
.theme(Theme.LIGHT)
.positiveText("ALLOW")
.onPositive(new MaterialDialog.SingleButtonCallback() {
@Override
public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
try {
Intent intent = new Intent();
intent.setComponent(new ComponentName("com.iqoo.secure",
"com.iqoo.secure.ui.phoneoptimize.AddWhiteListActivity"));
startActivity(intent);
} catch (Exception e) {
try {
Intent intent = new Intent();
intent.setComponent(new ComponentName("com.vivo.permissionmanager",
"com.vivo.permissionmanager.activity.BgStartUpManagerActivity"));
startActivity(intent);
} catch (Exception ex) {
try {
Intent intent = new Intent();
intent.setClassName("com.iqoo.secure",
"com.iqoo.secure.ui.phoneoptimize.BgStartUpManager");
startActivity(intent);
} catch (Exception exx) {
ex.printStackTrace();
}
}
}
}
})
.show();
}
}
public boolean checkServiceRunning() {
ActivityManager manager = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
for (ActivityManager.RunningServiceInfo service : manager.getRunningServices(
Integer.MAX_VALUE)) {
if ("com.sac.speechdemo.MyService".equals(service.service.getClassName())) {
return true;
}
}
return false;
}
答案 1 :(得分:0)
您无法通过代码执行此操作。您可以按照以下说明为应用程序进行手动设置,然后您的应用程序将运行您的后台服务
应用信息>节电>允许后台运行
您已选择“允许后台运行”
不允许自动启动。一段时间后它将停止后台服务。
在Realme 1 CPH1859中进行了测试
所有电话都没有上述设置