我有2个类,分别是GcmMessageHandler和Control(它是一个活动类,显示一些图形)。当我处理gcm消息时,我想刷新控件类(但如果它的前面)
public class GcmMessageHandler extends IntentService {
String mes;
private Handler handler;
public GcmMessageHandler() {
super("GcmMessageHandler");
}
@Override
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();
handler = new Handler();
}
@Override
protected void onHandleIntent(Intent intent) {
Bundle extras = intent.getExtras();
GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(this);
// The getMessageType() intent parameter must be the intent you received
// in your BroadcastReceiver.
String messageType = gcm.getMessageType(intent);
mes = extras.getString("title");
showToast();
Log.i("GCM", "Received : (" +messageType+") "+extras.getString("title"));
GcmBroadcastReceiver.completeWakefulIntent(intent);
}
public void showToast(){
handler.post(new Runnable() {
public void run() {
if(mes.equals("Control")){
}else{
Toast.makeText(getApplicationContext(),mes , Toast.LENGTH_LONG).show();
}
}
});
}
}
在这部分:
if(mes.equals("Control")){ }
如果控制活动类是恢复的,我想刷新它。我怎么能这样做?
答案 0 :(得分:1)
您可以使用BroadcastReceiver
通知您的活动有关任何更改。因此,请先在您的活动中注册BroadcastReceiver
:
public class MainActivity extends Activity {
public static String REFRESH_ACTIVITY = "com.domain.action.REFRESH_UI"
private BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
// do UI updates
}
};
@Override
public void onResume() {
super.onResume();
// do UI updates
IntentFilter filter = new IntentFilter();
filter.addAction(REFRESH_ACTIVITY);
this.registerReceiver(broadcastReceiver, filter);
}
@Override
public void onPause() {
super.onPause();
this.unregisterReceiver(broadcastReceiver);
}
...
}
然后发送广播以从任何位置执行UI更新:
if (mes.equals("Control")) {
Intent intent = new Intent();
intent.setAction(MainActivity.REFRESH_ACTIVITY);
sendBroadcast(intent);
}
答案 1 :(得分:0)
也许你可以使用观察者设计模式。 让GcmMessageHandler将Control活动作为观察者保存,然后在需要时通知它。
public class GcmMessageHandler extends IntentService {
String mes;
private Handler handler;
private Control mObserver
public GcmMessageHandler() {
super("GcmMessageHandler");
}
public void attachObserver(Control ctrl) {
mObserver = ctrl;
}
然后,您只需向Control类中添加一个可以从GcmMessageHandler类调用的方法。
if(mes.equals("Control")){
mObserver.update(); // example
}else ...
如果你第一次定义一个观察者界面,它会更光滑:
public interface IObserver {
public abstract void update();
}
让你的Control类实现它。这样,您的GcmMessageHandler类可以有一个观察者列表:
public class GcmMessageHandler extends IntentService {
String mes;
private Handler handler;
private List<IObserver> mObservers;
public GcmMessageHandler() {
super("GcmMessageHandler");
}
@Override
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();
handler = new Handler();
mObservers = new ArrayList<IObserver>();
}
public void attachObserver(Control ctrl) {
mObservers.add(ctrl);
}
private void notify() {
for(IObserver observer : mObservers)
observer.update();
}
当然,如果Control类是持有GcmMessageHandler对象的那个,你只需从Control调用attach方法,就像这样:
myGcmMessageHandler.attachObserver(this);