Android多個頁面共享socket的方法有三種:
1.單例模式
2.Application實體類
3.socket封裝在service中
這裏只用了第二種方法,即封裝一個Application實體類。具體實現如下:
package com.example.wisdomclassroom;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.net.UnknownHostException;
import android.app.Application;
import android.os.Looper;
import android.util.Log;
import android.widget.Toast;
public class WiFiApplication extends Application {
private static Socket socket;
private static OutputStream out = null;
private static InputStream in = null;
private static String IP = "192.168.4.1";
private static int PORT = 8080;
private static boolean Connect_flag;
private static String msg_send;
public void init() throws IOException, Exception{
new Thread(new Runnable() {
@Override
public void run() {
try {
if(socket==null) {
socket = new Socket(IP, PORT);
out = new DataOutputStream(socket.getOutputStream());
in = new DataInputStream(socket.getInputStream());
Connect_flag = true;
Log.d("slllllll", "789789789789");
}
else if(socket!=null){
socket.close();
socket = null;
Connect_flag = false;
Log.d("slllllll", "7123123322626");
}
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}).start();
}
public void SendData() throws IOException{
new Thread(new Runnable() {
@Override
public void run() {
if(socket != null){
try{
if (msg_send != null && msg_send.length()>0){
out.write(msg_send.getBytes("utf-8"));
out.flush();
msg_send="";
}
}
catch (IOException e){
e.printStackTrace();
}
}
}
}).start();
}
public Socket getSocket() { return socket; }
public void setSocket(Socket socket) { this.socket = socket; }
public OutputStream getOut() { return out; }
public void setOut(OutputStream out) { this.out = out; }
public InputStream getIn() { return in; }
public void setIn(InputStream in) { this.in = in; }
public Boolean getflag(){
return Connect_flag;
}
public void setMsg_send(String msg_send){
this.msg_send = msg_send;
}
public String getMsg_send(){
return msg_send;
}
}
將建立和斷開連接、發送數據都封裝在Application實體類中,當我們在不同Activity中要使用該連接時,只需要先獲取該類的對象,然後調用相應的函數即可。
第一次建立連接
final WiFiApplication WFApp = (WiFiApplication)CatalogActivity.this.getApplication();
link_btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(socket==null) {
try {
WFApp.init();
socket = WFApp.getSocket();
outer = WFApp.getOut();
inner = WFApp.getIn();
}catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
if(socket!=null){
try {
WFApp.init();
}catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
if(WFApp.getflag()==false) {
link_btn.setBackgroundColor(Color.GREEN);
link_btn.setText("斷開連接");
light_btn.setEnabled(true);
fan_btn.setEnabled(true);
music_btn.setEnabled(true);
}
else if(WFApp.getflag()==true){
link_btn.setText("點擊連接");
link_btn.setBackgroundColor(Color.GRAY);
Toast.makeText(CatalogActivity.this,"連接已斷開",Toast.LENGTH_LONG).show();
light_btn.setEnabled(false);
fan_btn.setEnabled(false);
music_btn.setEnabled(false);
}
}
});
獲取唯一實例
final WiFiApplication WFApp = (WiFiApplication)MusicActivity.this.getApplication();
socket = WFApp.getSocket();
outer = WFApp.getOut();
發送數據
M_playfirst_btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
try {
msg_send = "music01\n";
WFApp.setMsg_send(msg_send);
WFApp.SendData();
M_play_btn.setBackgroundColor(getResources().getColor(R.color.colorSkyBlue));
M_suspend_btn.setBackground(getResources().getDrawable(R.drawable.border_btn));
} catch (Exception e) {
e.printStackTrace();
}
}
});