代码如下,中间有一段GPS取位置的可以不用在意,注意问题就是我按发送按钮就会发送信息,但是再按一次程序就会崩溃,求帮忙!谢谢!
package com.example.zhb.wifi3;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.net.Socket;
import java.io.OutputStream;
import android.os.Bundle;
import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.view.View.OnClickListener;
public class MainActivity extends AppCompatActivity {
private Button sbutton = null;
private Button getlocation = null;
private TextView locations = null;
private double latitude;
private double longitude;
public Thread t=new Thread(new MyThread());
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
//setSupportActionBar(toolbar);
sbutton = (Button)findViewById(R.id.Button01);
getlocation = (Button)findViewById(R.id.Button02);
locations = (TextView)findViewById(R.id.textView);
getlocation.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
getLocation();
}
});
sbutton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
t.start();
}
});
}
public void getLocation(){
LocationManager locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
if(locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)){
try{Location location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if(location != null){
latitude = location.getLatitude();
longitude = location.getLongitude();
}
} catch(SecurityException e){}
}else{
LocationListener locationListener = new LocationListener() {
// Provider的状态在可用、暂时不可用和无服务三个状态直接切换时触发此函数
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
// Provider被enable时触发此函数,比如GPS被打开
@Override
public void onProviderEnabled(String provider) {
}
// Provider被disable时触发此函数,比如GPS被关闭
@Override
public void onProviderDisabled(String provider) {
}
//当坐标改变时触发此函数,如果Provider传进相同的坐标,它就不会被触发
@Override
public void onLocationChanged(Location location) {
if (location != null) {
Log.e("Map", "Location changed : Lat: "
+ location.getLatitude() + " Lng: "
+ location.getLongitude());
}
}
};
try{Location location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,1000, 0,locationListener);
if(location != null){
latitude = location.getLatitude(); //经度
longitude = location.getLongitude(); //纬度
}
} catch(SecurityException e){}
}
locations.setText("当前经纬度:"+latitude+";"+longitude);
}
public class MyThread implements Runnable {
@Override
public void run() {
// TODO Auto-generated method stub
try{
//建立连接到远程服务器的Socket
Socket socket=new Socket("192.168.1.218",8080);
//OutputStream os = socket.getOutputStream();
String a=String.valueOf(latitude);
String b=String.valueOf(longitude);
Writer writer=new OutputStreamWriter(socket.getOutputStream());
writer.write("latitude:"+a+" longitude:"+b+"\n");
writer.flush();
writer.close();
socket.close();
}catch(IOException e){
System.out.println("出错了");
}
}
}
}