0

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));}
Cœur
  • 37,241
  • 25
  • 195
  • 267
  • 1
    This isn't going to work globally, ever. YOu're in a low resource condition if its killing your service. If you get restarted, you'd just be killed again immediately. They're skipping the whole process. You're doing the right thing by returning START_STICKY, when they have enough resources to keep you running you'll be restarted. – Gabe Sechan Jan 29 '17 at 07:44
  • Why don't you register your receiver in activity ? – W4R10CK Jan 29 '17 at 08:03
  • I dont understand why do you need broadcast to restart the service. You are returning START_STICKY which will restart the service again. – Ajay Shrestha Jan 29 '17 at 08:05
  • @W4R10CK Because i registered it in manifest. Why should register receiver in activity? – Ali Kouroshfar Jan 29 '17 at 08:57
  • what is androidutilities ? – W4R10CK Jan 29 '17 at 08:58
  • @AjayShrestha Always not working START_STICKY. I need a service that always run. – Ali Kouroshfar Jan 29 '17 at 09:00
  • @W4R10CK This is a class that prepare some utilities for me like showToast, showKeyboard, compare and other. in runOnUIThread, i called another class that name is ApplicationLoader that have object of Handler and use it to "post" my runnable. – Ali Kouroshfar Jan 29 '17 at 09:05
  • @W4R10CK public static void runOnUIThread(Runnable runnable, long delay) { if (delay == 0) { ApplicationLoader.applicationHandler.post(runnable); } else { ApplicationLoader.applicationHandler.postDelayed(runnable, delay); } } – Ali Kouroshfar Jan 29 '17 at 09:06
  • use `ApplicationLoader.startPushService();` only don't use anything esle – W4R10CK Jan 29 '17 at 09:07
  • @AliKouroshfar If that doesnt work why dont you use Binder .Follow this link http://stackoverflow.com/questions/15758980/android-service-needs-to-run-always-never-pause-or-stop – Ajay Shrestha Jan 30 '17 at 20:15

0 Answers0