0

I want to update the icon status from red to green after successfull upload of the image in background service. I am using Otto event bus to transfer data between Service to Activity, I can able to subscribe and get the data from Service to Activity after successful uploading of image. Then I am trying to update the Grid view of my custom adapter from activity, it's working fine in debug mode but not at run time. No clue why it happening like so..

What I am doing wrong? Any suggestion/clue will be appreciated.

Here is my event publishing code in Service:

The TaskBus.java:

public class TaskBus {

    public static Bus bus = new Bus();
}

In onStartCommand called thread to upload image:

    // start thread to upload image
       new Thread(ImageUpload).start();

Here is the thread to upload image in background:

    public Runnable ImageUpload = new Runnable() {

            public void run() {
                try {
                    response = uploadImageToServer(server, imageData);


    if (!TextUtils.isEmpty(response)) {
                        showToastMessage("Image uploaded successfully.");
// update status icon in UI
                    updateStatus(imageData);
    // save/update in local DB
    // save/Update code goes here to save status in local DB

                } catch (Exception ex) {
                    ex.printStackTrace();
                    showToastMessage("Problem occurred while uploading image.");

                }

            }
        };

    private void showToastMessage(final String message) {
            Handler handler = new Handler(Looper.getMainLooper());
            handler.post(new Runnable() {
                @Override
                public void run() {
                    Toast.makeText(getApplicationContext(), message, Toast.LENGTH_SHORT).show();
                }
            });
        }

Here is the code to send data to activity from Service after successful upload of image:

private void updateStatus(ImageData data) {
        final UploadStatusEvent uploadStatusEvent = new UploadStatusEvent();
        uploadStatusEvent.data = data;
        Handler handler = new Handler(Looper.getMainLooper());
        handler.post(new Runnable() {
            @Override
            public void run() {
                bus.post(uploadStatusEvent);
            }

        });
    }

public class UploadStatusEvent {
        public ImageData data;
    }

Here is the subscribe code to get data from Service in Activity:

// update status icons , it called when image uploaded successfully in Service in background

        @Subscribe
        public void receiveTakingPhotoServiceUploadImageStatus(TakingPhotoService.UploadStatusEvent uploadStatusEvent) {
            ImageData data = uploadStatusEvent.data;

            if (data != null) {
                imageAdapter.setImageInGridItem(data);
                gridView.invalidateViews();
                gridView.setAdapter(imageAdapter);
            }
        }

Here is my Adapter code to update grid view:

 public void setImageInGridItem(ImageData data) {
        try {
            GridItem gridItem = getItem(data.position);

            gridItem.setStatus(data.status);

            gridItems.remove(vehicleImage.position);
            gridItems.add(ImageData.position, gridItem);
            setNotifyOnChange(true);
            this.notifyDataSetChanged();
        } catch (Exception e) {
            e.printStackTrace();

        }
    }
Shailendra Madda
  • 20,649
  • 15
  • 100
  • 138

2 Answers2

0

I would try to change two things.

  1. Make Bus a singleton and use the same instance in the whole app.
  2. Check the proguard settings and exclude TakingPhotoService.UploadStatusEvent from it and observe the results.

You haven't shown registration process in the activity, but since you have said it is working, I guess you did it. However, if you are not listening for events in the Service, there is no need to register bus in service. P.S. Don't forget to unregister it as well when activity is closed.

bajicdusko
  • 1,630
  • 1
  • 17
  • 32
  • Thanks for the reply 1. Am already using it as singleton yes am registered it in activity as well, but where to change proguard settings? – Shailendra Madda Feb 27 '17 at 07:31
  • I think you're not using it as a singleton, because on each Task.bus call you are creating new instance of bus. Create a static method in Task class getBusInstance(){ return bus != null ? bus : bus = new Bus(); } and use it instead. public static Bus bus = new Bus() change to private static Bus = null; As for proguard settings, there are multiple things to check, it might not be applied at all. In build.gradle, check for minifyEnable value. Your proguard settings can be found in proguard-rules.pro file – bajicdusko Feb 27 '17 at 07:54
  • I changed it to singleton but still same issue.. Here the thing is it's working fine in debugging but not at run time. Even without singleton also happening like this. – Shailendra Madda Feb 27 '17 at 08:46
0

Before that I was called updateStatus(imageData); this before updating status in SQLite data base. Now I fixed this issue by just called updateStatus(imageData); after saving/updating the image data in local DB. Then problem solved.

Moved updateStatus(imageData); to end of the thread i.e., after saving in local DB fixed issue.

May it use for some one.

Shailendra Madda
  • 20,649
  • 15
  • 100
  • 138