2013-04-12 85 views
0

我對Android應用程序開發有點新鮮。我已經創建了一個非常簡單的應用程序來打開/關閉設備的手電筒。 我想嘗試創建一個消息應用程序,您可以在兩個人都連接到Wifi時與朋友聊天。有點像「WhatsApp」,「Viber」等。 如果你能給我指導方針並幫助我瞭解如何開發這樣的應用程序,我會非常高興。創建消息應用程序(Wifi)

非常感謝, Orel。

回答

0

爲此,您需要創建服務器程序,它將認證用戶名&不同用戶之間的密碼和通信..等,您需要根據用戶查詢發送響應。 對於任何的網絡通信應用由兩個部分組成:

  1. 服務器
  2. 客戶

在WhatsApp的Android應用程序,它其中傳達的與WhatsApp的服務器的客戶端應用程序的情況下。

與交流從一個客戶到另一個客戶端的數據,客戶端可以是臺式機,Android手機,iPhone的服務器,...等

一般聊天程序將使用套接字來實現的,其取決於架構和你會選擇的技術。

+0

所以你的意思是我需要一臺服務器與數據庫來存儲所有消息的第一? – Qruez

+0

是的。還有其他的方式,但並沒有建議。 –

+0

我明白了。你能給我一個簡單的應用程序的建議,我可以作爲一個初學者請開發夥伴嗎?謝謝。 – Qruez

0

使用此:

ChatServerSide

import java.io.BufferedReader; 
import java.io.BufferedWriter; 
import java.io.IOException; 
import java.io.InputStreamReader; 
import java.io.OutputStreamWriter; 
import java.io.PrintWriter; 
import java.net.InetAddress; 
import java.net.NetworkInterface; 
import java.net.ServerSocket; 
import java.net.Socket; 
import java.net.SocketException; 
import java.net.UnknownHostException; 
import java.util.Enumeration; 

import android.app.Activity; 
import android.os.Bundle; 
import android.os.Handler; 
import android.view.View; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.TextView; 
import android.widget.Toast; 

public class ChatServerSide extends Activity implements View.OnClickListener { 

private ServerSocket serverSocket; 
Handler updateConversationHandler; 
Thread serverThread = null; 
Button btnSendMsg; 
EditText etInputMessage; 
TextView tvOutputMessage; 
TextView tvMyIp; 
Socket socket; 
public static final int SERVERPORT = 6000; 

@Override 
public void onCreate(Bundle savedInstanceState) { 

    super.onCreate(savedInstanceState); 
    setContentView(R.layout.chat_main_activity); 
    iniUi(); 
    iniListener(); 

    updateConversationHandler = new Handler(); 

    this.serverThread = new Thread(new ServerThread()); 
    this.serverThread.start(); 
    tvMyIp.setText("Waiting devices on " + getIpAddress()); 

} 

private void iniUi() { 
    tvMyIp = (TextView) findViewById(R.id.tvMyIp); 
    btnSendMsg = (Button) findViewById(R.id.btnSendMsg); 
    etInputMessage = (EditText) findViewById(R.id.etInputMesage); 
    tvOutputMessage = (TextView) findViewById(R.id.tvOutputMessage); 
} 

private void iniListener() { 
    btnSendMsg.setOnClickListener(this); 
} 

@Override 
protected void onStop() { 
    super.onStop(); 

} 

@Override 
protected void onDestroy() { 
    super.onDestroy(); 

} 

@Override 
public void onClick(View view) { 
    try { 

     String str = etInputMessage.getText().toString(); 
     PrintWriter out = new PrintWriter(new BufferedWriter(
       new OutputStreamWriter(socket.getOutputStream())), 
       true 
     ); 
     tvOutputMessage.setText(tvOutputMessage.getText().toString() + "You Says: " + str + "\n"); 
     etInputMessage.setText(""); 
     out.println(str); 
    } catch (UnknownHostException e) { 
     e.printStackTrace(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
} 

class ServerThread implements Runnable { 

    public void run() { 
     socket = null; 
     try { 
      serverSocket = new ServerSocket(SERVERPORT); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
     while (!Thread.currentThread().isInterrupted()) { 

      try { 
       socket = serverSocket.accept(); 
       CommunicationThread commThread = new CommunicationThread(socket); 
       new Thread(commThread).start(); 

      } catch (IOException e) { 
       e.printStackTrace(); 
      } 
     } 
    } 
} 

class CommunicationThread implements Runnable { 

    private Socket clientSocket; 
    private BufferedReader input; 

    public CommunicationThread(Socket clientSocket) { 
     this.clientSocket = clientSocket; 

     try { 
      this.input = new BufferedReader(new InputStreamReader(this.clientSocket.getInputStream())); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
    } 

    public void run() { 
     while (!Thread.currentThread().isInterrupted()) { 
      String read = null; 
      try { 

       read = input.readLine(); 
       if (read == null) { 
        read = "Client has leave the chat"; 
        socket.close(); 
        updateConversationHandler.post(new updateUIThread(read)); 
        break; 
       } 
       updateConversationHandler.post(new updateUIThread("Client Says: " + read)); 


     }catch(IOException e){ 
      e.printStackTrace(); 
     } 

    } 
} 

} 

class updateUIThread implements Runnable { 
private String msg; 

public updateUIThread(String str) { 
    this.msg = str; 
} 

@Override 
public void run() { 
    tvOutputMessage.setText(tvOutputMessage.getText().toString() + msg + "\n"); 
} 

} 

private String getIpAddress() { 
    String ip = ""; 
    try { 
     Enumeration<NetworkInterface> enumNetworkInterfaces = NetworkInterface 
       .getNetworkInterfaces(); 
     while (enumNetworkInterfaces.hasMoreElements()) { 
      NetworkInterface networkInterface = enumNetworkInterfaces 
        .nextElement(); 
      Enumeration<InetAddress> enumInetAddress = networkInterface 
        .getInetAddresses(); 
      while (enumInetAddress.hasMoreElements()) { 
       InetAddress inetAddress = enumInetAddress.nextElement(); 

       if (inetAddress.isSiteLocalAddress()) { 
        ip += inetAddress.getHostAddress() + "\n"; 
       } 

      } 

     } 

    } catch (SocketException e) { 
     e.printStackTrace(); 
     ip += "Something Wrong! " + e.toString() + "\n"; 
    } 

    return ip; 
    } 
} 

ChatClientSide

import android.app.Activity; 
import android.content.Intent; 
import android.os.Bundle; 
import android.os.Handler; 
import android.view.View; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.TextView; 

import java.io.BufferedReader; 
import java.io.BufferedWriter; 
import java.io.IOException; 
import java.io.InputStreamReader; 
import java.io.OutputStreamWriter; 
import java.io.PrintWriter; 
import java.net.Socket; 
import java.net.UnknownHostException; 

public class ChatClientSide extends Activity implements View.OnClickListener { 

private Handler handler = new Handler(); 
private Socket socket; 
Button btnSendMsg; 
EditText etInputMessage; 
TextView tvOutputMessage; 
Handler updateConversationHandler; 
TextView tvMyIp; 
private static final int SERVERPORT = 6000; 
private static String SERVER_IP = ""; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.chat_main_activity); 
    iniUi(); 
    iniListener(); 
    SERVER_IP = ActivityMain.ipToConnect; 
    updateConversationHandler = new Handler(); 
    receiveMsg(); 

} 

@Override 
protected void onStop() { 
    super.onStop(); 
    try { 
     socket.close(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
} 

@Override 
protected void onDestroy() { 
    super.onDestroy(); 
    try { 
     socket.close(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
} 

private String getServerIp(Intent intent) { 
    Bundle bundle = intent.getExtras(); 
    String ip = bundle.getString("destAdress"); 
    tvMyIp.setText("Is connected to " + ip); 
    return ip; 
} 

private void iniUi() { 
    tvMyIp = (TextView) findViewById(R.id.tvMyIp); 
    btnSendMsg = (Button) findViewById(R.id.btnSendMsg); 
    etInputMessage = (EditText) findViewById(R.id.etInputMesage); 
    tvOutputMessage = (TextView) findViewById(R.id.tvOutputMessage); 
} 

private void iniListener() { 
    btnSendMsg.setOnClickListener(this); 
} 

@Override 
public void onClick(View view) { 
    try { 

     String str = etInputMessage.getText().toString(); 
     PrintWriter out = new PrintWriter(new BufferedWriter(
       new OutputStreamWriter(socket.getOutputStream())), 
       true 
     ); 
     tvOutputMessage.setText(tvOutputMessage.getText().toString() + "You Says: " + str + "\n"); 
     etInputMessage.setText(""); 
     out.println(str); 
    } catch (UnknownHostException e) { 
     e.printStackTrace(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
} 


public void receiveMsg() { 
    new Thread(new Runnable() { 
     @Override 
     public void run() { 
      final String host = SERVER_IP; 

      BufferedReader in = null; 
      try { 
       socket = new Socket(host, SERVERPORT); 
      } catch (UnknownHostException e) { 
       e.printStackTrace(); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 

      try { 
       in = new BufferedReader(new InputStreamReader(socket.getInputStream())); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 

      while (true) { 
       String msg = null; 
       try { 
        msg = in.readLine(); 
        //msgList.add(msg); 
       } catch (IOException e) { 
        e.printStackTrace(); 
       } 
       if (msg == null) { 
        break; 
       } else { 
        displayMsg(msg); 
       } 
      } 

     } 
    }).start(); 


} 

public void displayMsg(String msg) { 
    final String mssg = msg; 
    handler.post(new Runnable() { 

     @Override 
     public void run() { 
      tvOutputMessage.setText(tvOutputMessage.getText().toString() + "Server Says: " + mssg + "\n"); 
     } 
    }); 
} 
} 

chat_main_activity.xml

<?xml version="1.0" encoding="utf-8"?> 

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:orientation="vertical" 
android:layout_width="match_parent" 
android:layout_height="match_parent"> 

<LinearLayout 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content"> 
    <TextView 
     android:id="@+id/tvMyIp" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text=""/> 
    </LinearLayout> 

<LinearLayout 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal"> 

    <Button 
     android:id="@+id/btnSendMsg" 
     android:layout_width="wrap_content" 
     android:layout_height="80dp" 
     android:text="send" 
     android:layout_weight="0" /> 

    <EditText 
     android:id="@+id/etInputMesage" 
     android:layout_width="match_parent" 
     android:layout_height="fill_parent" 
     android:layout_weight="1" /> 


</LinearLayout> 

<LinearLayout 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 

    <TextView 
     android:id="@+id/tvOutputMessage" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent"> 

    </TextView> 
</LinearLayout> 
</LinearLayout> 
+0

客戶端如何找到它? ActivityMain.ipToConnect –

相關問題