2015-08-14 44 views
0

我正在製作一個連接到我的電腦的Android應用程序,我正在嘗試使它能夠來回發送消息。截至目前,消息可以發送和接收服務器端,但對於我的Android應用程序,我不知道如何不斷檢查消息並更新UI。我正在使用BufferedReader進行輸入,並且想要在TextView上顯示消息。這是我到目前爲止有:構建一個聊天客戶端Android應用程序,我應該如何處理接收消息?

package com.example.david.chatclient; 

import android.os.AsyncTask; 
import android.os.Bundle; 
import android.support.v7.app.AppCompatActivity; 
import android.util.Log; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.view.View; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.TextView; 
import android.widget.Toast; 
import java.io.BufferedReader; 
import java.io.IOException; 
import java.io.InputStream; 
import java.io.InputStreamReader; 
import java.io.OutputStream; 
import java.io.PrintWriter; 
import java.net.Socket; 
import java.util.concurrent.ExecutionException; 

/** 
* After connecting to server, how can i recieive messages, append it, and then reloop through it 
*/ 


public class MainActivity extends AppCompatActivity { 

private String IP; 
private int portNumber; 
private Socket clientSocket; 
private PrintWriter writer; 
private BufferedReader reader; 
private Button connectButton; 
private Button sendButton; 
private EditText userMessage; 
private TextView chatHistory; 


@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    // 10.0.2.2 = Android Emulator IP Address (Reaches development machine, not emulator itself) 
    IP = "10.0.2.2"; 
    portNumber = 25565; 

    connectButton = (Button) findViewById(R.id.connect_button); 
    sendButton = (Button) findViewById(R.id.send_button); 
    userMessage = (EditText) findViewById(R.id.user_message); 
    chatHistory = (TextView) findViewById(R.id.chat_history); 

} 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.menu_main, menu); 
    return true; 
} 

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    // Handle action bar item clicks here. The action bar will 
    // automatically handle clicks on the Home/Up button, so long 
    // as you specify a parent activity in AndroidManifest.xml. 
    int id = item.getItemId(); 

    //noinspection SimplifiableIfStatement 
    if (id == R.id.action_settings) { 
     return true; 
    } 

    return super.onOptionsItemSelected(item); 
} 

public void connectClient(View view) { 
    ConnectToServer task = new ConnectToServer(); 
    task.execute(); 
} // end connectClient 


public void sendMessage(View view) { 
    String message = userMessage.getText().toString() + "\n"; 
    writer.write(message); 
    writer.flush(); 
    chatHistory.append("CLIENT: " + message); 
    userMessage.setText(""); 
} 

private class ConnectToServer extends AsyncTask<Void, Void, Void> 
{ 
    @Override 
    protected Void doInBackground(Void... params) { 
     try{ 
      clientSocket = new Socket(IP, portNumber); 
      reader = new BufferedReader(new InputStreamReader(clientSocket.getInputStream())); 
      writer = new PrintWriter(clientSocket.getOutputStream()); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
     return null; 
    } 

    @Override 
    protected void onPostExecute(Void aVoid) { 
     Toast.makeText(MainActivity.this, "Successfuly connected, streams are esablished", Toast.LENGTH_SHORT).show(); 
    } 
} // end ConnectToServer 


} // end MainActivity 

下面是用戶界面的樣子:

https://gyazo.com/dbf99c78ca7874939a404ef7d23d8ff8

+1

你應該看看GCM(谷歌雲通訊) – AnixPasBesoin

+0

如果電話不總是連接到互聯網,我建議SignalR爲你的情況。 – BNK

回答

0

有兩個aproaches。

  1. 定期檢查服務器上是否有新消息等待此方法稱爲提取 - 這是非常耗電和數據不友好的情況。

  2. 保持開放連接所有時間aka推送消息 - 這是現代的方式去。谷歌處理這個Android和iOS與GCM

相關問題