问题
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