开发者

How to write message to client from server

for example,in a webs开发者_如何学编程ite or software on the pc,you could write some news or other things,and then it will update in your android

is there any example of this,or tutorial related about it


Well, as Falmarri already said...its a very vague question. But still, I can give you some thought-provoking impulse:

You could use a service, which queries a server on specific time periodes, to check, if there are new messages. This would produce alot overhead data, because even if there aren't any messages, the service would still asking the server. A timer would do that:

public Timer timerT = new Timer();
Handler updateNow;

TimerTask task = new TimerTask() {
  public void run() {
    updateNow.sendEmptyMessage(TICK);
  }
};  

updateNow = new Handler() {
  @Override
  public void handleMessage(Message msg) {
    switch (msg.what) {
    case TICK:                  
        // Query wird gestartet
        new updateMsg().execute();
        break;
    }
  }
};      

// Fragt alle 60 Sekunden
timerT.scheduleAtFixedRate(task, 0, (60 * 1000));

Und die Abfrage an den Server:

public class updateMsg extends AsyncTask<String, String, Void> {            
    String message = "";

    protected void onPreExecute() { }

    protected Void doInBackground(String... data) {
       HttpReq request = Http.getInstance().createRequest();
       request.setUrl("http://www.deinserver.de/msg.php");
       request.setMethod("POST");

       request.addParam("fuer_name", "keenora");

       request.execute();

       message = request.getResult();           
       return null;         
    }

    protected void onPostExecute(Void unused) {
       Log.e("DEBUG", "Nachricht vom Server erhalten: " + message);
    }

    protected void onCancelled() { } 
}

Und nicht zu vergessen die Server-Seite:

<?php

  // Ganz dummes Script
  switch($_POST['fuer_name']) {
    case 'keenora': {
      echo 'Hallo!';
      break;
    }
    default: {
      echo 'Es gibt keine neuen Nachrichten!';
      break;
    }
  }

?>
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜