java - Notify Activity Class -
i have 2 classes gcmmessagehandler , command (its activity class, shows graphics). when handle gcm message, want refresh command class (but if front)
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); // getmessagetype() intent parameter must intent received // in 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(); } } }); } }
in part:
if(mes.equals("control")){ }
if command activity class resume, want refresh it. how can this?
you can utilize broadcastreceiver
in order notify activity changes. register broadcastreceiver
in activity first:
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) { // ui updates } }; @override public void onresume() { super.onresume(); // ui updates intentfilter filter = new intentfilter(); filter.addaction(refresh_activity); this.registerreceiver(broadcastreceiver, filter); } @override public void onpause() { super.onpause(); this.unregisterreceiver(broadcastreceiver); } ... }
then send broadcast perform ui update location:
if (mes.equals("control")) { intent intent = new intent(); intent.setaction(mainactivity.refresh_activity); sendbroadcast(intent); }
java android google-cloud-messaging
No comments:
Post a Comment