当前位置:编程学习 > wap >>

android基于Socket的通信,按键按两次才能连接服务器

安卓客户端程序
activity_main.xml文件
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
 >

    <TextView
        android:id="@+id/text1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />

    <Button
        android:id="@+id/but1"
        android:text="查询数据库刘同乐"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        />
   
</LinearLayout>

MainActivity程序
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;

import Thread.selectThread;
import Thread.selectThread2;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.app.Activity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends Activity {

private TextView text1= null;
private Socket s;
private Button but = null;
private Button but2 = null;
private Handler handler;

 
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.text1 = (TextView) super.findViewById(R.id.text1);
this.but = (Button) super.findViewById(R.id.but1);

this.but.setOnClickListener(new OnClickListenerImpl());      // 添加监听


handler = new Handler(){

@Override
public void handleMessage(Message msg) {

String message = (String) msg.obj;               //获取handler的参数值
MainActivity.this.text1.setText(message);
;
}
};


}
private class OnClickListenerImpl implements OnClickListener{

@Override
public void onClick(View arg0) {

selectThread sThread = new selectThread(s,handler);
Thread t = new Thread(sThread);
t.start();


System.out.println("监听开启");

}

}

private class OnClickListenerImpl2 implements OnClickListener{

@Override
public void onClick(View arg0) {

selectThread2 sThread = new selectThread2(s,handler);
Thread t = new Thread(sThread);
t.start();
System.out.println("监听开启");

}

}

}

selectThread
package Thread;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;

import android.os.Handler;
import android.os.Message;

public class selectThread implements Runnable{
private Socket s;
private Handler handler;

public selectThread(Socket s,Handler handler){
this.s = s;
this.handler = handler;
}

@Override
public void run() {
System.out.println("线程开启");
String str  = "null";
String name = "刘同乐";
try {
s = new Socket("192.168.1.100",9091);
DataOutputStream dos = new DataOutputStream(s.getOutputStream());
DataInputStream dis = new DataInputStream(s.getInputStream());
dos.writeUTF(name);
str = dis.readUTF();
Message msg = new Message();
msg.obj = str;
handler.sendMessage(msg);

} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}  
}
}

AndroidManifest.xml中也已经配置权限,
但是运行时要按两下按钮服务器才显示已经连接上,且客户端显示查询的数据... Android socket
补充:移动开发 ,  Android
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,