Reputation: 411
The Service class will load a library and it takes about 4-5 seconds for the library to become ready. What is the best way to make MainActivity to keep checking on the status of a static boolean in Service class and do something when it's ready? I looked around and knew that using busy wait loop is bad.
The outline of my planned MainActivity is
onCreate - start the service
onResume - show the splash screen until a specific boolean in Service become true then switch to another fragment
Upvotes: 2
Views: 491
Reputation: 11467
Simple solution is Broadcast Reciever
Try this
BroadcastReceiver broadCastNewMessage = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
// do your stuff here
}
};
Now in onCreate()
register this
registerReceiver(this.broadCastNewMessage, new IntentFilter("bcNewMessage"));
And in onDestroy()
unregisterReceiver(broadCastNewMessage);
Now Call this method from the service class
where u want to update the activity
sendBroadcast(new Intent().setAction("bcNewMessage"));
Upvotes: 1
Reputation: 7437
You could use a broadcast receiver from the Service to your MainActivity which triggers a method inside the MainActivity... instead of constantly checking a static bool in the service.
But you want to be very sure to handle cases where it never loads for whatever reason, otherwise users will be staring at a splash screen forever.
Upvotes: 3