Android change ToggleButton状态-TextView或其他Android.widget
我正在使用GCM,在调用服务器后的onRegistered方法中,我必须更改AppSettings活动中toggleButton的状态Android change ToggleButton状态-TextView或其他Android.widget,android,android-widget,Android,Android Widget,我正在使用GCM,在调用服务器后的onRegistered方法中,我必须更改AppSettings活动中toggleButton的状态 //called when i click the toggleButton public void onPushStateButtonClicked(View view) { // controllo se il bottone è su on boolean on = ((ToggleB
//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");
}
}
}
在另一个类gcminentservice中
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,在这里我有上下文,这是可能的吗?或者,我可以刷新活动吗
谢谢你的回复 我已经下定决心了
这是活动中的代码
// Set AppSettings object into GCMIntentService
GCMIntentService.setActivityMain(AppSettings.this);
这是在GCMINENT类中:
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();
}
}
我已经决定了
这是活动中的代码
// Set AppSettings object into GCMIntentService
GCMIntentService.setActivityMain(AppSettings.this);
这是在GCMINENT类中:
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();
}
}
是的,您可能需要显示一些相关代码。您可以通过将toggle实例发送到非活动类,或者通过将活动实例传递到非活动类而不是上下文,以及将ToggleButton实例声明为类字段而不是方法来实现。我已编辑了问题。是的,您可能需要显示一些相关代码。您可以通过将toggle实例发送到非活动类,或者通过将活动实例传递到非活动类而不是上下文,还可以将ToggleButton实例声明为类字段而不是方法来实现。我已经编辑了问题