0

I am trying to work on a screen(say Activity A) where there is a progress bar displayed on the screen and I need to send a broadcast to a different activity(Activity B) broadcastReceiver while the progressbar is running. And if the function in the activity B is completed it will display that back to this activity A.

Now I am running progress bar in a worker thread like this and sending localbroadcast using handler(Looper.getMainLooper()):

final Context context = Activity.this.getApplicationContext();
new Thread(new Runnable() {
  @Override
  public void run() {
     while (progressBar.getProgress() != 100) {
        try {
            progressBar.incrementProgressBy(2);
            Thread.currentThread().sleep(100);
        } catch (InterruptedException ie) {
            Log.e(LOG_TAG, "Interrupted exception in progress bar: " + ie);
        }
        if (progressBar.getProgress() == 10) {
           Handler handler = new Handler(Looper.getMainLooper());
           handler.post(new Runnable() {
               @Override
               public void run() {
                   // model is a parameter which I want to send using intents
                   Intent intent = new Intent("new_device");
                   intent.putExtra("device", model);
                           LocalBroadcastManager.getInstance(context).sendBroadcast(intent);
               }
           });
        }
     }
  }
}).start(); 

But it's not working. Broadcast is not received by the other activity. I know that Broadcast should be done on UI thread and it works fine if I do it in onResume() or onCreate() method. But when I'm using it in a handler inside a thread (inside onCreate()) it is not working.

Am I doing something wrong or is there any other way of doing it?

EDIT

I am receiving my intent in Activity B as follows:

private BroadcastReceiver mMessageReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            Model model = (Model) intent.getSerializableExtra("device");
            onConnectDevice(model.getDevice());
        }
    };
Community
  • 1
  • 1
AND_SUN
  • 97
  • 2
  • 10
  • Are you registering your receiver? How? – natario Jan 26 '16 at 19:23
  • Yes I am registering my receiver in Activity B's onCreate() method: LocalBroadcastManager.getInstance(this).registerReceiver(mMessageReceiver, new IntentFilter("new_device")); – AND_SUN Jan 26 '16 at 19:32
  • You could start by adding a debug flag to your intent so that you can check whats crops up in the logs. `intent.addFlags(Intent.FLAG_DEBUG_LOG_RESOLUTION)` – VallaDanger Jan 20 '17 at 09:01

2 Answers2

0

Try using runonuithread method of the activity inside your thread

Suhaib Roomy
  • 2,501
  • 1
  • 16
  • 22
  • you mean the activity which contains the thread? Like this : ((Activity)context).runOnUiThread(new Runnable() { @Override public void run() { // model is a parameter which I want to send using intents Intent intent = new Intent("device"); intent.putExtra("device", model); LocalBroadcastManager.getInstance(context).sendBroadcast(intent); } }); – AND_SUN Jan 26 '16 at 19:29
  • You can juse use Activity.this.runOnUiThread. also register your reciever in the oncreate by passing broadcast receiver and intent filter to the registerreceiver method – Suhaib Roomy Jan 26 '16 at 19:35
  • Actually it doesn't work. runOnUiThread also doesn't work and I have no idea why! – AND_SUN Jan 26 '16 at 19:35
  • Yes I have registered my receiver in Activity B's onCreate() method. See my above comment. But still it doesn't work with runOnUiThread() – AND_SUN Jan 26 '16 at 19:37
0

I believe your issue is in progressBar.incrementProgress(), because you are trying to alter a UI element from outside the UI thread. Keeping your structure, you can try something like:

final Handler handler = new Handler(Looper.getMainLooper());
final Thread thread = new Thread(new Runnable() {
  @Override
  public void run() {
     while (progressBar.getProgress() != 100) {
        try {

            handler.post(new Runnable() {
                @Override
                public void run() {
                    progressBar.incrementProgressBy(2);
                }
            });
            Thread.sleep(100);

        } catch (InterruptedException ie) {

        }
        if (progressBar.getProgress() == 10) {
           handler.post(new Runnable() {
               @Override
               public void run() {
                   Intent intent = new Intent("new_device");
                   intent.putExtra("device", model);
                   Context context = progressBar.getContext();
                   LocalBroadcastManager.getInstance(context).sendBroadcast(intent);
               }
           });
        }
     }
  }
}); 

thread.start();

Also make sure that 10 is ever reached (for example, the sequence could be 1-3-5-7-9-11 and you won’t get anything.

I also took context from the progress bar, you don’t need to fetch it first.

There are better solutions than this but I just kept your structure.

natario
  • 24,954
  • 17
  • 88
  • 158
  • Thanks for the code! But this is also not working. BroadcastReceiver in other activity is not receiving anything. There are better solutions than this. Can you give me a an example or some hint as to how to proceed? – AND_SUN Jan 26 '16 at 19:53
  • Also you have written thread.sleep(100); This should be Thread.sleep(100); right? – AND_SUN Jan 26 '16 at 19:56
  • Yes. Progress bar editing is the only thing that comes to my mind. Are you getting any warning in your logcat? – natario Jan 26 '16 at 19:58
  • Also, registering your receiver onCreate() is bad if your are unregistering onPause() or onStop(). – natario Jan 26 '16 at 20:01
  • No I am not getting any warning in my logcat. And I am unregistering it in onDestroy() – AND_SUN Jan 26 '16 at 20:04