android - how to detect and notify user of message from server? -
app sent transaction server, user closes app, message needs sent phone server 10+ minutes later. phone may asleep, or user might checking email. question have is:
- how can phone notified message has been received server ?
- how display message ?
a possible solution google cloud messaging, still not able answer these 2 questions
1) have use service that.
2) , show message.
do this.
the variable , method members of service class:
public final static string action = "com.apps.example.mainactivity"; private void messagefromserver()//this method sends broadcast messages { intent intent = new intent(movement_update); string messagefromserver=servermessage //here put server message intent.putextra("messagefromserver", messagefromserver); sendbroadcast(intent); }
and methods main activity:
you have register receiver in onresume method:
@override public void onresume() { intentfilter intentfilter; intentfilter= new intentfilter(yourservice.action); messagefromserver= new messagefromserver(); registerreceiver(messagefromserver, intentfilter); startyourservice(); super.onresume(); } private void startyourservice() { startservice(new intent(this, yourservice.class)); } public class messagefromserver extends broadcastreceiver { @override public void onreceive(context context, intent intent) { // method receives broadcast messages. // sure modify androidmanifest.xml file in // order enable message receiving string messagefromserver = intent.getstringextra("messagefromserver"); updategui();// here can update ui } }
and put service in manifest file.
<service android:name="com.apps.service.yourservice" ></service>
Comments
Post a Comment