代码如下。在service中运行,求帮忙!!!!先谢过
public class GPSservice extends Service {
public final String ACTION_MOBILE_LOCATOR_SERVICE ="com.DoBest.GPSsevice.GPSsevice";
private LocationClient mLocationClient;
private int DELAY_TIME =60*1000; //设置上传以及刷新GPS的数据的间隔的时间
private String TAG ="GPSservice"; //log的标签
private MyLocationListener mLocationListener; //自定义监听者
private CheckGPSandNet mCheckGPSandNet; //该类用于检测GPS以及网络的工作状态,如果未打开,则发出通知
private Context mContext;
private HttpHelper mhttphelper;
@Override
public void onCreate() {
Log.i(TAG, "-->>GPSservice is onCreate--->>");
mContext= GPSservice.this;
mCheckGPSandNet = new CheckGPSandNet();
mCheckGPSandNet.checkGPSandNetwork(mContext); //检测运行环境
InitLocationClient();
mLocationClient.start();
if (!mLocationClient.isStarted()) {
mLocationClient.start();
Log.e(TAG, "--->> mlocation is not started------");
}
mhttphelper =new HttpHelper();
mhttphelper.httpGetTableId();
}
private void InitLocationClient() {
mLocationClient =new LocationClient(getApplicationContext());
LocationClientOption mLocationClientOption =new LocationClientOption();
mLocationListener = new MyLocationListener();
mLocationClient.registerLocationListener(mLocationListener);
mLocationClientOption.setLocationMode(LocationMode.Hight_Accuracy);
mLocationClientOption.setCoorType("bd09ll");
mLocationClientOption.setScanSpan(DELAY_TIME);
mLocationClientOption.setOpenGps(true);
mLocationClient.setLocOption(mLocationClientOption);
Log.i(TAG, "-->>InitGPS is end ---->>");
}
class MyLocationListener implements BDLocationListener
{
private String TAG ="GPSinfo";
double longitude;
double latitude;
@Override
public void onReceiveLocation(BDLocation location) {
// TODO Auto-generated method stub
if (location==null) {
Log.e(TAG, "--->> CANNOT GET THE LOCATION ---");
return;
}
Log.e(TAG, "--->> THE LOCATION ??? ---");
int locType = location.getLocType();
longitude = location.getLongitude();
latitude = location.getLatitude();
// TODO 调试使用
StringBuffer sb = new StringBuffer(256);
sb.append("time : ");
sb.append(location.getTime());
sb.append("\nerror code : ");
sb.append(location.getLocType());
sb.append("\nlatitude : ");
sb.append(location.getLatitude());
sb.append("\nlontitude : ");
sb.append(location.getLongitude());
sb.append("\nradius : ");
sb.append(location.getRadius());
if (location.getLocType() == BDLocation.TypeGpsLocation){
sb.append("\nspeed : ");
sb.append(location.getSpeed());
sb.append("\nsatellite : ");
sb.append(location.getSatelliteNumber());
sb.append("\ndirection : ");
sb.append("\naddr : ");
sb.append(location.getAddrStr());
sb.append(location.getDirection());
} else if (location.getLocType() == BDLocation.TypeNetWorkLocation){
sb.append("\naddr : ");
sb.append(location.getAddrStr());
//运营商信息
sb.append("\noperationers : ");
sb.append(location.getOperators());
}
GpsLog.writeLogFile(sb.toString());
mhttphelper.httpPostToUpdateMyLocation(location);
}
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.i(TAG, "-->> GPSservice is onStartCommand--->>");
return 0;
}
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
Log.i(TAG, "-->> GPSservice is onBind--->>");
return null;
}
@Override
public void onDestroy() {
// TODO Auto-generated method stub
Log.i(TAG, "-->> GPSservice is onBind--->>");
super.onDestroy();
}
}