N Sharma
N Sharma

Reputation: 34497

How to update the activity from Service in Android

I am learning Android. I have made a simple Activity then in the Activity start the Service that do some high network operation now I want when high network load call complete then I want to update to my Activity.

Is it possible to update the Activity from the Service ?

Thanks in advance.

Upvotes: 0

Views: 180

Answers (1)

Larry McKenzie
Larry McKenzie

Reputation: 3283

This is code taken and modified from a now depreciated library called DataDroid but it is relevant to what you are attempting to do.

private final class RequestReceiver extends ResultReceiver {

    RequestReceiver() {
        super(new Handler(Looper.getMainLooper()));

    }

    @Override
    public void onReceiveResult(int resultCode, Bundle resultData) {
        /*
         * Depending on how you implement this either update activity from here
         * or instantiate it with an interface that your activity implements and 
         * call that here. 
         */
    }
}

Create service with somethinglike this:

RequestReceiver requestReceiver = new RequestReceiver();

Intent i = new Intent(mContext, Service);
i.putExtra(RequestService.INTENT_EXTRA_RECEIVER, requestReceiver);
mContext.startService(i);

You could also just use the previously mentioned library and modify it to fit your current use case or use one of the many other similar libraries available.

Upvotes: 1

Related Questions