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.
- In
onMessage
send a broadcast. - In your activity
onResume
register a broadcast receiver and make it display a toast (remember to unregister it in theonPause
)
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).
精彩评论