Scorpy86
Scorpy86

Reputation: 339

Android change ToggleButton status - TextView or other android.widget

I'm using GCM and in the onRegistered method after the call to my server i've to change the state of the toggleButton in the AppSettings Activity

//called when i click the toggleButton
    public void onPushStateButtonClicked(View view) {
                // controllo se il bottone è su on
                boolean on = ((ToggleButton) view).isChecked();
                PushClientService p = new PushClientService();
                if (on) {
                    savePushStateButton(true);
                    // se il bottone  in impostazioni è settato ad on registro il dispositivo
                    p.pushService(this);
                }else if(!on) {
                    savePushStateButton(false);
                    // se il bottone  in impostazioni è settato ad on cancello il dispositivo
                    //nel caso sia il primo accesso essendo il bottone a false di default preveniamo l'eccezione
                    try{    
                        GCMRegistrar.unregister(this);
                    }catch(IllegalArgumentException iAE){
                        Log.e("Errore:","stai cercando di cancellate un device non registrato");
                    }
                }
            }

in the other class GCMIntentService

protected void onRegistered(Context context, String registrationId) {
            Log.i(TAG, "Device registered: regId = " + registrationId);
            Log.d("onRegistered", getString(R.string.gcm_registered));
            boolean myServerRegistration=ServerUtilities.customRegistration(context, registrationId);
            if(!myServerRegistration){
                // Errore sulla registrazione sul server, deregistro il device
                GCMRegistrar.unregister(context);
                **//change the state of the ToggleButton**

            }
        }

I want to set its value to false by another simple class where i've the context, it's possible? alternatively can i refresh an activity?

tnks for the response!

Upvotes: 1

Views: 237

Answers (1)

Scorpy86
Scorpy86

Reputation: 339

i've resolved myself this is the code in the activity

// Set AppSettings object into GCMIntentService
        GCMIntentService.setActivityMain(AppSettings.this);

and this is in the GCMIntent class:

protected static AppSettings activityMain;

public static void setActivityMain(AppSettings a){
        activityMain = a;
    }
// run on UI thread
public void changePushStateButtonStatus(){

    activityMain.runOnUiThread(new Runnable() {
        @Override
        public void run() {
            activityMain.pushStateButton.setChecked(false);    
        }
    });
}


protected void onRegistered(Context context, String registrationId) {
        Log.i(TAG, "Device registered: regId = " + registrationId);
        Log.d("onRegistered", getString(R.string.gcm_registered));
        boolean myServerRegistration=ServerUtilities.customRegistration(context, registrationId);
        if(!myServerRegistration){
            // Error on our server registration, unregister the device
            GCMRegistrar.unregister(context);
            // Save on sharedPreference the button status
            savePushStateButton(false);
            // Start a thread on UI to change the button status
            changePushStateButtonStatus();
        }
    }

Upvotes: 1

Related Questions