android客户端在多个界面切换时保持socket的链接.doc
android客户端在多个界面切换时保持socket的链接
研究一个socket在多个界面切换时保持链接的问题,现在我提供客户端的源码给有需要的人参考。
1、ApplicationUtil类:
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import .Socket;
import .UnknownHostException;
import android.app.Application;
public class ApplicationUtil extends Application{
private Socket socket;
private DataOutputStream out = null;
private DataInputStream in = null;
public void init() throws IOException, Exception{
this.socket = new Socket("04",10202);
this.out = new DataOutputStream(socket.getOutputStream());
this.in = new DataInputStream(socket.getInputStream());
}
public Socket getSocket() {
return socket;
}
public void setSocket(Socket socket) {
this.socket = socket;
}
public DataOutputStream getOut() {
return out;
}
public void setOut(DataOutputStream out) {
this.out = out;
}
public DataInputStream getIn() {
return in;
}
public void setIn(DataInputStream in) {
this.in = in;
}
}
2、AndroidMainifest.xml中application的配置:
package="com.login"
android:versionCode="1"
android:versionName="1.0" >
android:minSdkVersion="10"
android:targetSdkVersion="15" />
android:name="com.util.ApplicationUtil"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
android:name=".LoginActivity"
android:label="@string/title_activity_main" >