1

我正在使用 GCM,并且在调用我的服务器后的 onRegistered 方法中,我必须在 AppSettings Activity 中更改 toggleButton 的状态

//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 的回应!

4

1 回答 1

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();
        }
    }
于 2013-03-29T16:35:49.950 回答