miui rom上的后台服务已停止

编程入门 行业动态 更新时间:2024-10-28 10:29:55
本文介绍了miui rom上的后台服务已停止的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在开发一个后台服务,并且在OnDestroy方法中,我调用了一个意图以再次启动我的服务.我不会再开始使用Miui rom(小米手机和华为手机).

I am developing a background services and in OnDestroy Method, I've called an intent to start my services again. I'ts not started again on miui rom (Xiaomi mobile and huawei mobile).

我该如何处理?

public class NotificationsService extends Service { @Override public void onCreate() { ApplicationLoader.postInitApplication(); } @Override public int onStartCommand(Intent intent, int flags, int startId) { return START_STICKY; } @Override public IBinder onBind(Intent intent) { return null; } public void onDestroy() { Intent intent = new Intent("example.app.start"); sendBroadcast(intent); } }

在清单中

<receiver android:name=".AppStartReceiver" android:enabled="true"> <intent-filter> <action android:name="example.app.start" /> <action android:name="android.intent.action.BOOT_COMPLETED" /> </intent-filter> </receiver>

推荐答案

在小米上并不新鲜,因为小米具有一项称为应用程序许可的功能,用户必须允许该应用程序自动启动(服务).

It doesn't new on Xiaomi because Xiaomi has a feature called app permission, where user has to allow the app to start automatically (Service).

执行以下操作,并允许您的应用自动启动:

Go like this and allow your app to autostart:

Settings > permissions > Autostart

或者

不要尝试在onDestroy()内部重新启动相同的Service,而是在onStartCommand(Intent intent, int flags, int startId)方法内部使用START_STICKY.

Don't try to restart the same Service inside onDestroy() instead use START_STICKY inside onStartCommand(Intent intent, int flags, int startId) method.

同样,您正在发送广播而没有启动服务,请正确使用onDestroy:

Again you are sending broadcast not starting a service, Use onDestroy properly:

@Override public void onDestroy() { Intent intent = new Intent("example.app.start"); sendBroadcast(intent); super.onDestroy(); }

更多推荐

miui rom上的后台服务已停止

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

发布评论

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

>www.elefans.com

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