如何修复几分钟后在新版本(如vivo和oppo)中活动被自动杀死的问题

编程入门 行业动态 更新时间:2024-10-28 16:25:41
本文介绍了如何修复几分钟后在新版本(如vivo和oppo)中活动被自动杀死的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我制作了一个应用程序,这是一个按时间间隔存储位置信息并将其发送到服务器的应用程序 服务在后台或Forground模式下运行.在常见的android手机中运行良好. Btw应用程序在oppo和vivo中无法正常运行 启动第一个活动后,app(此活动)将在3到5分钟后被杀死.

I made a app,This is the app that store the location information by interval and send it to the server Services are working in background or forground mode.It is working well in common android phones. Btw app is not working well in oppo and vivo After launching the first activity,app(this activity) is killing after 3~5 min.

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);

这是我的消息来源. 您能帮我吗?我没有找到正确的解决方案.如果有人有解决方案,您可以让我知道吗?

This is piece of my source. Could you help me?I didn't find correct solution.If anyone have a solution?Could you let me know about it?

推荐答案

是的,这是因为默认情况下,这些设备仅允许某些白名单应用程序的后台服务.如果您的应用也必须这样工作,则意味着您必须从设置中启用 autoStart ,下面的代码将帮助您使用户启用应用的自动启动功能.如果 autoStart 启用后,您的服务将在后台正常运行.

Yes , this is because these devices only allow background services for some whitelist app by default. If your app also has to work like that means you have to enable autoStart from settings, below code will help you to make user to enable autostart for your app.If autoStart is enabled, your service will work well in background.

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

更多推荐

如何修复几分钟后在新版本(如vivo和oppo)中活动被自动杀死的问题

本文发布于:2023-11-27 05:52:45,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1636856.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:新版本   分钟后   oppo   vivo

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!