gpt4 book ai didi

android - 小米和 Oppo 的后台服务受到限制

转载 作者:行者123 更新时间:2023-12-02 17:01:25 24 4
gpt4 key购买 nike

我们正在开发一个在前台服务中运行 MQTT 的应用程序。问题出在小米红米 Note 7 上,在我们终止应用程序后,该服务被终止,但在其他品牌上它工作正常。我没有在 Oppo 和 Vivo 上测试该应用程序,但当我搜索时它们也有问题。在服务的 onCreate 方法中,我调用了 startForeground(NOTIFICATION_ID, notification) ,我在 list 中的服务声明如下

<service
android:name=".service.mqtt.MqttService"
android:enabled="true"
android:exported="false"
android:foregroundServiceType="location" />

我还将 foregroundServiceType 更改为 connectedDevice|dataSync|mediaPlayback 并添加 android:stopWithTask="false" 并返回 START_STICKY 在服务的 onStartCommand 方法中,但仍然不起作用。

最佳答案

终于找到答案了here

private static final Intent[] POWERMANAGER_INTENTS = {
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.huawei.systemmanager", "com.huawei.systemmanager.startupmgr.ui.StartupNormalAppListActivity")),
new Intent().setComponent(new ComponentName("com.huawei.systemmanager", "com.huawei.systemmanager.optimize.process.ProtectActivity")),
new Intent().setComponent(new ComponentName("com.huawei.systemmanager", "com.huawei.systemmanager.appcontrol.activity.StartupAppControlActivity")),
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.samsung.android.lool", "com.samsung.android.sm.ui.battery.BatteryActivity")),
new Intent().setComponent(new ComponentName("com.htc.pitroad", "com.htc.pitroad.landingpage.activity.LandingPageActivity")),
new Intent().setComponent(new ComponentName("com.asus.mobilemanager", "com.asus.mobilemanager.MainActivity"))
};



for (Intent intent : POWERMANAGER_INTENTS)
if (getPackageManager().resolveActivity(intent, PackageManager.MATCH_DEFAULT_ONLY) != null) {
// show dialog to ask user action
break;
}

编辑:检查用户是否启用自动启动也存在问题。据我搜索,目前没有可用的解决方案。所以我自己设计了一个解决方案。我创建了一个工作程序,它将每 25 分钟在首选项中节省系统时间。每次打开应用程序时,我都会检查首选项,如果距保存时间超过 30 分钟,则意味着工作人员无法完成工作,因此可能用户上次没有启用自动启动,必须再次提示。

class BackgroundCheckWorker(val appContext: Context, val workerParams: WorkerParameters) :
Worker(appContext, workerParams), KoinComponent {


override fun doWork(): Result {
val pref = appContext.getSharedPreferences(PermissionHandler.AUTO_START_PREF, Context.MODE_PRIVATE)
val editor = pref.edit()
editor.putString(AUTO_START_PREF_KEY, Calendar.getInstance().time.toString())
editor.apply()
return Result.success()
}
}

在启动中我调用这个函数:

fun requestUnrestrictedBackgroundService(context: Activity): Boolean {
val pref = context.getSharedPreferences(AUTO_START_PREF, Context.MODE_PRIVATE)
var updated = false
val lastUpdate = pref.getString(AUTO_START_PREF_KEY, "")
updated = if (lastUpdate == null || lastUpdate == "") {
val editor = pref.edit()
editor.putString(AUTO_START_PREF_KEY, Calendar.getInstance().time.toString())
editor.apply()
false
} else lastUpdate != "" &&
DateUtil.minAgo(lastUpdate) <= 30
if (!updated) {
for (intent in POWERMANAGER_INTENTS)
if (context.packageManager.resolveActivity(
intent,
PackageManager.MATCH_DEFAULT_ONLY
) != null
) {
val dialog = AlertDialog.Builder(context)
dialog.setMessage("On this device you must allow us to run services in background")
.setPositiveButton("Yes") { paramDialogInterface, paramInt ->
val editor = pref.edit()
editor.putString(AUTO_START_PREF_KEY, Calendar.getInstance().time.toString())
editor.apply()
context.startActivityForResult(intent, 1234)
}
.setNegativeButton("Cancel") { paramDialogInterface, paramInt -> context.finish() }
dialog.show()
return false
}
}
return true
}

关于android - 小米和 Oppo 的后台服务受到限制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59442078/

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