2013-03-28 53 views
1

我使用GCM和調用我的服務器後onRegistered方法我已經改變在AppSettings的活動切換按鈕的狀態Android的變化切換按鈕狀態 - TextView的或其他android.widget

//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"); 
        } 
       } 
      } 

在其他類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** 

      } 
     } 

我想通過另一種簡單的類在那裏我已經背景下將其值設置爲false,這可能嗎?或者我可以刷新活動?

tnks for the response!

+0

是可能的,你將需要顯示一些相關的代碼。你可以通過發送切換實例到非Activity類或通過將Activity實例傳遞給非Activity類而不是Context來實現,並且還將ToggleButton實例聲明爲類字段而不是方法 – 2013-03-28 08:43:31

+0

我已編輯該問題 – Scorpy86 2013-03-28 08:52:30

回答

1

我已經解決了自己 這是在活動

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

的代碼,這是在GCMIntent類:

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(); 
     } 
    }