LocationListener的派生类class图标上一直有个红色方框
intent启动GPSReport
system.out会输出Service startcommand!
不输出TestLocatioinListener里的位置时间、经纬度等。
之前logcat有报错could not find class com.baidu.platform.comapi.basestruct.Geopoint
referenced from TestLoacatioListener里的OnLoactionChanged()函数
我把jar包换成2_1_3版本的就不再报错了可是红色方框还是在
求大神指教 如何才能成功连接上服务器!
public class GPSReport extends Service {
TestLocationListener testLocationListener = new TestLocationListener();
TelephonyManager telephonyManager = null;
@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}
@Override
public void onCreate() {
super.onCreate();
System.out.println("Service create!!");
// 开始持续定位
LocationManager locationManager = (LocationManager) GPSReport.this.getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,10000, 0, testLocationListener);
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
System.out.println("Service startcommand!!");
return super.onStartCommand(intent, flags, startId);
}
private class TestLocationListener implements LocationListener {
private HttpResponse httpResponse = null;// 服务端响应
private HttpEntity httpEntity = null;// 响应的内容
@Override
// 当位置改变时做的->post方法上报gps信息
public void onLocationChanged(Location location) {
//虚拟机测试无IMEI码,故暂用手动设定值
//System.out.println(telephonyManager.getDeviceId());
//System.out.println("1234567");
System.out.println(location.getTime());
System.out.println(location.getLongitude());
System.out.println(location.getLatitude());
System.out.println(location.getSpeed());
GeoPoint nowPoint = CoordinateConvert.fromWgs84ToBaidu(new GeoPoint((int)(location.getLatitude()*1E6), (int)(location.getLongitude()*1E6)));
System.out.println("baidu坐标:");
System.out.println(nowPoint.getLatitudeE6()/1000000.0);
System.out.println(nowPoint.getLongitudeE6()/1000000.0);
/////////自动监测上报条件语句。为了调试方便暂时新增两个按钮,手动点选上报
//if(location.getSpeed()*3.6f>RTApplication.minBusSpeed&&location.getSpeed()*3.6f<RTApplication.maxBusSpeed){}
// 获取相关参数,存于NameValurPairs中
//String userID = telephonyManager.getDeviceId();
String userID = "86302014796";
String time = Long.toString(location.getTime());
String longitude = Double.toString(nowPoint.getLongitudeE6()/1000000.0);
String latitude = Double.toString(nowPoint.getLatitudeE6()/1000000.0);
String speed = Float.toString(location.getSpeed());
NameValuePair idPair= new BasicNameValuePair("userID", userID);
NameValuePair timePair = new BasicNameValuePair("time", time);
NameValuePair longitudePair = new BasicNameValuePair("longitude",longitude);
NameValuePair latitudePair = new BasicNameValuePair("latitude",latitude);
NameValuePair speedPair = new BasicNameValuePair("speed", speed);
final List<NameValuePair> gpsPairs = new ArrayList<NameValuePair>();
gpsPairs.add(idPair);
gpsPairs.add(timePair);
gpsPairs.add(longitudePair);
gpsPairs.add(latitudePair);
gpsPairs.add(speedPair);
////////////////////////多线程上报
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
// post方法上报
try {
System.out.println("========run()=======");
HttpEntity requestHttpEntity = new UrlEncodedFormEntity(gpsPairs);
HttpPost httpPost = new HttpPost("http://10.10.185.31:80");//======URL========
httpPost.setEntity(requestHttpEntity);
HttpClient httpClient = new DefaultHttpClient();
// 获取服务端响应及内容
InputStream inputStream = null;
try {
httpResponse = httpClient.execute(httpPost);
httpEntity = httpResponse.getEntity();
inputStream = httpEntity.getContent();
// 循环取出响应内容
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
String result = "";
String line = "";
while ((line = reader.readLine()) != null) {
result = result + line;
}
System.out.println(result);
} catch (Exception e) {
System.out.println("ex");
e.printStackTrace();
} finally {
inputStream.close();// 关掉输入流
}
} catch (Exception e) {
e.printStackTrace();
}
}
});
thread.start();
}
@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 void onDestroy() {
// 关闭持续定位
LocationManager locationManager = (LocationManager) GPSReport.this
.getSystemService(Context.LOCATION_SERVICE);
locationManager.removeUpdates(testLocationListener);
super.onDestroy();
System.out.println("Service destroy!!");
}
}