} public LocationListener mLocationListener01 = new LocationListener(){ @Override public void onLocationChanged(Location location) { // TODO Auto-generated method stub
} @Override public void onProviderDisabled(String provider) { // TODO Auto-generated method stub
} @Override public void onProviderEnabled(String provider) { // TODO Auto-generated method stub
} @Override public void onStatusChanged(String provider, int status, Bundle extras) { // TODO Auto-generated method stub
}
}; @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.imageclient, menu); return true; }}客户端子线程:(其中服务器是C的) public class ClientThread implements Runnable {
final static int ID=1; final static int B_HEADLINE=16; //返回命令协议头长度 final static int B_BODYC=56; //返回数据图片协议中固定字节长度
int indexout;
private String hostName; private int portNum; private Socket socket; //向UI线程发送消息 private Handler handler; //接受UI线程的消息 public Handler reHandler;
Log.i("Socked输出流是否关闭:",socket.isOutputShutdown() +"***true if writing to this socket " + "is not possible anymore, false otherwise"); Log.i("Socked是否被关闭:",socket.isClosed()+ "***true if the socket is closed, false otherwise"); out.write(outdata); out.flush(); Log.i("outdata","发送到服务器"); Log.i("Socked输出流是否关闭:",socket.isOutputShutdown() +"***true if writing to this socket " + "is not possible anymore, false otherwise"); Log.i("Socked是否被关闭:",socket.isClosed()+ "***true if the socket is closed, false otherwise");
我还不是很懂,因为我关了跟没关,结果好像没有区别,我f附程序上来,你帮我看看!public class ImageclientActivity extends Activity {
ImageView imageView1=null, imageView2=null;Button btn;TextView position; TextView infor;
Handler handler;
//定义与服务器通信的子线程
ClientThread clientThread;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_imageclient);
position=(TextView) findViewById(R.id.position);
infor=(TextView) findViewById(R.id.infor);
imageView1 = (ImageView) findViewById(R.id.imageView1);
imageView2 = (ImageView) findViewById(R.id.imageView2);
btn = (Button) findViewById(R.id.button);
handler=new Handler(){
@Override
public void handleMessage(Message msg){
if(msg.what==0x123){
Bitmap bmp1,bmp2;
Bundle imadata=msg.getData();
double lngnow=imadata.getDouble("lng");
double latnow=imadata.getDouble("lat");
String cond=imadata.getString("cond");
bmp1=imadata.getParcelable("bmp1");
bmp2=imadata.getParcelable("bmp2");
imageView1.setImageBitmap(bmp1);
imageView2.setImageBitmap(bmp2);
position.setText("图片相机经度为: "+lngnow+" 纬度为: "+latnow);
infor.setText(cond);
}
}
};
clientThread=new ClientThread(handler);
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
new Thread(clientThread).start(); //这句放在这儿会不会有问题?1. 每按一次按钮会不会产生很多线程 2。是不是应该放在 clientThread=new ClientThread(handler); 之后呢!?.这句话放在这儿,第一次数据好像可以发送出去,第二次就不行了。
showImage();
}
});
Thread.setDefaultUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
@Override
public void uncaughtException(Thread thread, Throwable ex) {
// TODO Auto-generated method stub
System.out.println(ex.getLocalizedMessage());
finish(); }
}); } public void showImage() {
// TODO Auto-generated method stub
LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
3000, 10, mLocationListener01);
locationManager.requestLocationUpdates(
LocationManager.NETWORK_PROVIDER, 3000, 10,
mLocationListener01);
}
public LocationListener mLocationListener01 = new LocationListener(){ @Override
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
double lat = 0; //纬度
double lng = 0; //经度
float rate=0; //速度
float dirt=0; //方向
if (location != null) {
lat = location.getLatitude();
lng = location.getLongitude();
rate=location.getSpeed();
dirt=location.getBearing();
infor.setText(lat+""+lng);
Log.i("location", "经纬度为:"+lng+" "+lat);
}
else{
Toast.makeText(getApplicationContext(), "定位内容为空,请稍等...",
Toast.LENGTH_LONG).show();
}
try{
//将数据封装成Message,然后发送给Handler
Message msg=new Message();
msg.what=0x345;
Bundle gpsdata=new Bundle();
gpsdata.putDouble("lng", lng);
gpsdata.putDouble("lat", lat);
gpsdata.putFloat("rate", rate);
gpsdata.putFloat("dirt", dirt);
msg.setData(gpsdata);
clientThread.reHandler.sendMessage(msg);//???????????????
Log.i("locaton数据到子线程","clientThread.reHandler.sendMessage(msg);");
}
catch (Exception e){
e.printStackTrace();
}
} @Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
} @Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
} @Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
}; @Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.imageclient, menu);
return true;
}}客户端子线程:(其中服务器是C的)
public class ClientThread implements Runnable {
final static int ID=1;
final static int B_HEADLINE=16; //返回命令协议头长度
final static int B_BODYC=56; //返回数据图片协议中固定字节长度
int indexout;
private String hostName;
private int portNum;
private Socket socket;
//向UI线程发送消息
private Handler handler;
//接受UI线程的消息
public Handler reHandler;
DataOutputStream out=null;
BufferedInputStream in=null;
byte[] outdata=new byte[172];{
outdata[0]=(byte) 0x80;
outdata[1]=0x09; //command
outdata[2]=0x02; //type
int outdataline=156;
indexout=encodeIntBigEndian(outdata,outdataline,12,4);
int id=ID;
indexout=encodeIntBigEndian(outdata,id,indexout,4);
outdata[indexout]=1;//0--->上行 1--->下行
//indexout+=4;
}
public ClientThread(Handler handler){
this.handler=handler;
} @Override
public void run() {
// TODO Auto-generated method stub
try{
socket=new Socket();
try{
SocketAddress address=new InetSocketAddress(hostName,portNum);
socket.connect(address, 3000);
Log.i("Socked是否连接:",socket.isConnected()
+"***true if the socket is connected, false otherwise");
}catch(UnknownHostException e){
System.out.println("--->未知名的主机:"+hostName+"异常");
}catch(IOException e){
System.out.println("hostName= " + hostName+" portNum= "+portNum
+"--->IO异常"+e.getMessage() );
}
out = new DataOutputStream(socket.getOutputStream()); //输出流
in = new BufferedInputStream(socket.getInputStream()); //输入流
new Thread(){
@Override
public void run() {
try{
Log.i("Socked输入流是否被关闭:",socket.isInputShutdown()+
"***true if reading from this socket "
+ "is not possible anymore, false otherwise");
//socket.setSoTimeout(1000);
byte backhead[]=new byte[B_HEADLINE];
int len=0;
while(len<B_HEADLINE){
len+=in.read(backhead,len,B_HEADLINE-len);}
Log.i("indata","收到协议头");
//首先接受返回数据中的命令协议头(16个字节)
if(backhead[0]==0xf0&backhead[1]==0x01){
//返回数据成功
int bodyimlen=bytesToInt(backhead,12);//接受数据
byte body[]=new byte[bodyimlen];
int len1=0;
while(len1<bodyimlen){
len1+=in.read(body,len1,bodyimlen-len);}
System.out.println("状态为:"+body[0]);
byte[] cond=new byte[body[1]];//body[1]为状态描述长度
for(int i=0;i<body[1];i++){
cond[i]=body[i+B_BODYC];
}
System.out.println("状态描述为:"+new String(cond));
//读取显示图片相机位置
double lng=getDouble(body,4); //x
double lat=getDouble(body,12); //y
int imalen1=bytesToInt(body,20);//获取第一张图片数据长度
Bitmap bmp1,bmp2=null;
if(imalen1!=0){
ByteArrayOutputStream outPut = new ByteArrayOutputStream();
bmp1 = BitmapFactory.decodeByteArray(body, B_BODYC,
imalen1);
bmp1.compress(CompressFormat.JPEG, 100, outPut);
}else{
bmp1=null;
}
Message msg=handler.obtainMessage();
msg.what=0x123;
Bundle imadata=new Bundle();
imadata.putDouble("lng", lng);
imadata.putDouble("lat", lat);
imadata.putString("cond", new String(cond));
imadata.putParcelable("bmp1", bmp1);
imadata.putParcelable("bmp2", bmp2);
msg.setData(imadata);
msg.sendToTarget();
in.close();
out.close();
}else{
System.out.println("返回数据失败!");
}
} catch (IOException e) {
e.printStackTrace();
}
}
}.start();
Looper.prepare();
reHandler=new Handler(){
@Override
public void handleMessage(Message msg){
if(msg.what==0x345){
try{
indexout=16;
Bundle gps=msg.getData();
double lng=gps.getDouble("lng");
double lat=gps.getDouble("lat");
float rate=gps.getFloat("rate");
float dirt=gps.getFloat("dirt");
indexout=putFloat(outdata,rate,indexout);
indexout=putFloat(outdata,dirt,indexout);
indexout=putDouble(outdata,lng,indexout);
indexout=putDouble(outdata,lat,indexout);
Log.i("子线程收到locaton数据","子线程收到locaton数据"+" "+"Bundle.getData();");
Log.i("Socked输出流是否关闭:",socket.isOutputShutdown()
+"***true if writing to this socket "
+ "is not possible anymore, false otherwise");
Log.i("Socked是否被关闭:",socket.isClosed()+
"***true if the socket is closed, false otherwise");
out.write(outdata);
out.flush();
Log.i("outdata","发送到服务器");
Log.i("Socked输出流是否关闭:",socket.isOutputShutdown()
+"***true if writing to this socket "
+ "is not possible anymore, false otherwise");
Log.i("Socked是否被关闭:",socket.isClosed()+
"***true if the socket is closed, false otherwise");
}catch(Exception e){
e.printStackTrace();
}
}
}
};
Looper.loop();
Log.d("aaaa","Looper.loop()之后.....");
}catch(SocketTimeoutException e1){
System.out.println("网络连接超时!");
}catch(Exception e){
e.printStackTrace();
} finally {
try {
socket.close(); //socket以及流的关闭问题:要不要关闭,放在什么位置比较正确?
} catch (IOException e) {
e.printStackTrace();
}
} //=====
}
}