Search code examples
androidandroid-serviceandroid-service-binding

Sending data from bound Service's another thread to Activity


I have Activity and Service, which is called by bindService from Activity. In this service I perform some operations in another thread using Vk sdk. Here I parse json to my models and add them to list. My question is how can I send this list from another thread after parsing to my activity? Here is code of my service

public class RequestService extends Service {

private final IBinder mBinder = new RequestBinder();
private List<Item> mItems;
private String mStartFrom;
private VKRequest mVKRequest;

public class RequestBinder extends Binder {
    public RequestService getService() {
        return RequestService.this;
    }
}

@Override
public void onCreate() {
    super.onCreate();
    mItems = new ArrayList<>();
    loadNews("");
}

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

public void loadNews(String startFrom) {
    if (startFrom.equals("")) {
        mItems.clear();
    }
    mVKRequest = new VKRequest(Constants.METHOD_NAME, VKParameters.from(Constants.FILTERS,
            Constants.FILTER_NAMES, VKApiConst.COUNT, Constants.NEWS_COUNT, Constants.START_FROM,
            startFrom));

    // this performs in another thread
    mVKRequest.executeWithListener(new VKRequest.VKRequestListener() {

        @Override
        public void onComplete(VKResponse response) {
            super.onComplete(response);
            JSONObject result = response.json;
            try {
                mItems = ParseUtils.parse(result);
                mStartFrom = ParseUtils.getNextFrom();
            } catch (JSONException e) {
                e.printStackTrace();
            }

        }
    });

}

public List<Item> getItems() {
    return mItems;
}

public String getStartFrom() {
    return mStartFrom;
}

}


Solution

  • You can use context.sendBroadcast(intent). From any class in your project you can create an Intent and send this intent through broadcast.

    For example:

    // Somewhere in your code (your service class, for example)
    private void sendMessage(Context context) {
        Intent intent = new Intent("com.example.keys.MESSAGE");
        intent.putExtra("message", "Hello World!");
        // Send Broadcast to Broadcast receiver with message
        context.sendBroadcast(intent);
    }
    

    In your activity, you need to declare a broadcast receiver:

    private final BroadcastReceiver mHandleMessageReceiver = new BroadcastReceiver() {
             @Override
             public void onReceive(Context context, Intent intent) {
                      String message = intent.getExtras().getString("message"); // Contains "Hello World!"
                      // !!! Do something with your message !!!
             }
    }
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
            registerReceiver(mHandleMessageReceiver, new IntentFilter("com.example.keys.MESSAGE"));
    }
    
    @Override
    protected void onDestroy() {
        unregisterReceiver(mHandleMessageReceiver);
    }
    

    So, you can send strings, numbers and objects from anywhere in your code. And you can have more than one BroadcastReceiver.