Sending broadcast to restart service to keep-alive not working correctly

时光毁灭记忆、已成空白 提交于 2019-12-25 08:21:35

问题


I have an Android service that in onDestroy() method I'm sending a broadcast to restart my service. It's work on sony and samsung rom! but not work in Xiaomi or Huawei rom.

How do I handle this?

This is my code.

Manifest:

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

Service:

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:

public class AppStartReceiver extends BroadcastReceiver {
public void onReceive(Context context, Intent intent) {
    AndroidUtilities.runOnUIThread(new Runnable() {
        @Override
        public void run() {
            ApplicationLoader.startPushService();
        }
    });
}}

ApplicationLoader.startPushService():

    public static void startPushService() {

    applicationContext.startService(new Intent(applicationContext, NotificationsService.class));}

来源:https://stackoverflow.com/questions/41918691/sending-broadcast-to-restart-service-to-keep-alive-not-working-correctly

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!