gpt4 book ai didi

Android 更改 ToggleButton 状态 - TextView 或其他 android.widget

转载 作者:行者123 更新时间:2023-11-30 03:41:20 24 4
gpt4 key购买 nike

我正在使用 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,这可能吗?或者我可以刷新 Activity 吗?

感谢回复!

最佳答案

我已经解决了自己这是 Activity 中的代码

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

关于Android 更改 ToggleButton 状态 - TextView 或其他 android.widget,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15677318/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com