2013-03-28 54 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(); 
     } 
    }