简体   繁体   中英

How to start Service before BroadcastReceiver gets trigged in Android?

I have a BroadcastReceiver class which gets trigged sometimes And I also have a Service class. My Service class starts from my Application class named G.class . I want my Service class to start before BroadcastReceiver class. but as I see in LogCat , First G.class starts and it ends then BroadcastReceiver class starts and it ends then Service class starts. What is the problem?

AlarmReceiver.class

import android.content.BroadcastReceiver;
import android.content.Context; import android.content.Intent;
import com.hadi.android.dm.app.Logger;

public class AlarmReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
    Logger.i("receiver started");
    //do something
}

}

G.class

import android.content.Intent;

public class G extends Application {

  @Override
  public void onCreate() {
    super.onCreate();

    Logger.i("G started");
    startService(new Intent(getApplicationContext(), ApplicationService.class));
    Logger.i("G ended");

}

ApplicationService.class

import android.app.Service;
import android.content.Context;
import android.content.Intent;

public class ApplicationService extends Service {

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Logger.i("service started");
        return START_NOT_STICKY;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Logger.i("service ended");
    }
}

How my BroadcastReciever gets trigged

public void schedule(long time) {
        PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 2, new Intent(context, AlarmReceiver.class), 0);
        android.app.AlarmManager alarmManager = (android.app.AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            alarmManager.setExact(android.app.AlarmManager.RTC_WAKEUP, time, pendingIntent);
        } else {
            alarmManager.set(android.app.AlarmManager.RTC_WAKEUP, time, pendingIntent);
        }
    }

My LogCat

07-09 00:44:00.797 18172-18172/com.hadi.android.dm I/MYAPP: G started
07-09 00:44:00.886 18172-18172/com.hadi.android.dm I/MYAPP: G ended
07-09 00:44:00.888 18172-18172/com.hadi.android.dm I/MYAPP: receiver started
07-09 00:44:00.890 18172-18172/com.hadi.android.dm I/MYAPP: service started

There's no way to do this. Here's what's happening:

  • The alarm goes off

  • The app, which is not running, is started. The received broadcast is noted and an actuon to call the receiver put in the Handler on the main thread.

  • The app creates the Application class. This calls startService, which adds an action to create the service to the Handler on the main thread.

  • The main thread message loop is returned to. It takes the next message on the queue, the BR message, and runs it, starting the BroadcastReceiver.

  • The main thread message loop is returned to. It takes the next message on the queue, the SS message, and runs it, starting the Service.

There is no way to change the ordering in any of this.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM