Skip to content Skip to sidebar Skip to footer

Fetching Methods From Broadcastreceiver To Update Ui

I am trying to update the UI according to change of a variable in BroadcastReceiver. Thus, I need to call a method (to get the variable I mentioned) of a class which extends Broadc

Solution 1:

The reason that every time isProvidrActive changed the whole activity got regenerated is because I sent the intent using context.startActivity(i) but rather this time we send the intent using contect.sendBroadcast(i) to the ProviderChangeReceiver_updateUI inner class which update only desired part of the UI. So below is the new code.

privatestaticboolean isProviderActive;
@OverridepublicvoidonReceive(Context context, Intent intent) {
    LocationManagerlm= (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
    if (lm.isProviderEnabled(LocationManager.GPS_PROVIDER) && lm.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) {
        Log.v("-> ", "GPS + NETWORK");
        isProviderActive = true;
    }

    elseif (lm.isProviderEnabled(LocationManager.GPS_PROVIDER) && !lm.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) {
        Log.v("-> ", "GPS");
        isProviderActive = true;
    }

    elseif (lm.isProviderEnabled(LocationManager.NETWORK_PROVIDER) && !lm.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
        Log.v("-> ", "NETWORK");
        isProviderActive = true;
    }

    else
    {
        Log.v("-> ", "DISCONNECT");
        isProviderActive = false;
    }

    //Send value of isProviderActive to ProviderChangeReceiver_updateUIIntenti=newIntent(context, ProviderChangeReceiver_updateUI.class);
    i.putExtra("isProvidrActv", isProviderActive);
    i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    context.sendBroadcast(i);
}

Edit your manifest file to enable the inner class ProviderChangeReceiver_updateUI to listen for broadcast sent by our broadcastReciever, add the following entry to manifest

<receiverandroid:name="<package-name>.MainActivity$ProviderChangeReceiver_updateUI"android:enabled="true" ><intent-filter></intent-filter></receiver>

the $ sign indicates inner class. No need to add intent-filters unless required.

And in your ProviderChangeReceiver_updateUI class in the onReceive() method get the value of isProviderActive

...
//changed the identifiers from private to public static and the class extends //BroadcastReceiverpublicstaticclassProviderChangeReceiver_updateUIextendsBroadcastReceiver {

    //Empty constructor to prevent exception can't instantiate no empty constructorpublicProviderChangeReceiver_updateUI(){

    }

    //Removed the final keyword from handlerprivate Handler handler; // Handler used to execute code on the UI threadpublicProviderChangeReceiver_updateUI(Handler handler) {
        this.handler = handler;
    }

    @OverridepublicvoidonReceive(final Context context, final Intent intent) {
        booleanisProvidrActv= intent.getBooleanExtra("isProvidrActv", false);
        //mapsActivity is a global static object of the class MapsActivity<br/>//instantiate it the onCreate() method of mainactivity for ex:<br/>/*public class MapsActivity extends Activity{<br/>
         static MapsActivity mapsActivity;

         @Override
         protected void onCreate(Bundle savedInstancestate){
        ma  = new MapsActivity();
        }*/
        mapsActivity.runOnUiThread(newRunnable() {
            @Overridepublicvoidrun() {
                if (isProvidrActv)
                    originButton.setVisibility(View.VISIBLE);
                else
                    originButton.setVisibility(View.INVISIBLE);
            }
        });
    }
}

Solution 2:

I think a nice way to handle something like this is with an eventbus.

Take a look at Squares Otto library.

In your broadcast receiver you would publish the change event and then in your activity you would subscribe to the event and update the ui.

Based on the context of your question, I think you would be able to avoid the broadcast receiver entirely, since I suspect you have something watching your provider and then broadcasting the event.

Update: (based on comments)

I think the way this would work would be something like this

In the code that detects the change in location provider:

if (bothProvidersAreStopped()) {
   bus.publish(newBothProvidersStoppedEvent());
} elseif (onlyNetworkProviderIsStopped() {
   bus.publish(newNetworkProviderStoppedEvent());
} elseif (onlyGpsProviderIsStopped() {
   bus.publish(newGpsProviderStoppedEvent());
}

in your activity

@Subscribe public void onBothProvidersStopped(BothProvidersStopped event) {
    // handle case were both providers just stopped
}

@Subscribe public void onNetworkProvidersStopped(BothProvidersStopped event) {
    // handle case were network provider just stopped
}

@Subscribe public void onGpsProvidersStopped(BothProvidersStopped event) {
    // handle case were gps provider just stopped
}

Post a Comment for "Fetching Methods From Broadcastreceiver To Update Ui"