miui rom上的后台服务已停止 [英] background service has stopped on miui rom

查看:139
本文介绍了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上的后台服务已停止的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆