gpt4 book ai didi

android - vivo、oppo等新版本Activity几分钟后自动终止的问题如何解决

转载 作者:行者123 更新时间:2023-11-29 01:05:40 29 4
gpt4 key购买 nike

我做了一个app,这个是按时间间隔存储位置信息并发送到服务器的app服务在后台或前台模式下工作。它在普通的 android 手机上运行良好。顺便说一句,应用程序在 oppo 和 vivo 中运行不佳启动第一个 Activity 后,应用程序(此 Activity )将在 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);

这是我的来源。你能帮我吗?我没有找到正确的解决方案。如果有人有解决方案吗?你能告诉我吗?

最佳答案

是的,这是因为这些设备默认只允许某些白名单应用程序的后台服务。如果您的应用也必须像那样工作意味着您必须从设置中启用autoStart,下面的代码将帮助您让用户为您的应用启用自动启动。如果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;
}

关于android - vivo、oppo等新版本Activity几分钟后自动终止的问题如何解决,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47426313/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com