开发者

Call a method from my "MainActivity" in Android

I'm currently messing up with the Google's C2DM notification service. Following the steps in this tutorial: http://www.vogella.de/articles/AndroidCloudToDeviceMessaging/article.html, I succesfully r开发者_开发知识库ecieved a "push message" from the server. However, in the "protected void onMessage" I need to send the message to the "MainClass" to print it in a toast. Since I'm not deeply familiarized with the Android developing, I will appreciate any help on this. Thank you


Use a broadcast to communicate with the activity.

  1. In onMessage send a broadcast.
  2. In your activity onResume register a broadcast receiver and make it display a toast (remember to unregister it in the onPause)

You would need also to handle the case when the activity is not running (maybe display a notification). In this case, make the broadcast an ordered broadcast. The broadcast receiver in the activity should be set with a high prio, then register a default broadcast receiver through your manifest (this one displays a notification, or opens the activity, or whatever you want).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜