android 定位问题
击不垮我 2014-03-20 01:57:59 我最近在做签到的功能,签到时,等位自己的位置发送到服务器,签到成功后,开启服务,这个服务会在你签退时退出。主要任务是在签到和签退的时间段里每隔10分钟将你的位置发送给服务器。问题出来了,签到定位位置坐标没有问题,服务里的定位时而正常时而定位不到(坐标0,0),定位代码都是一样的,始终没找到原因,有大侠赐教吗?贴上我的服务里的代码:
package com.example.esun_mobile.service;
import java.util.HashMap;
import java.util.Map;
import java.util.Timer;
import java.util.TimerTask;
import org.json.JSONException;
import com.baidu.mapapi.BMapManager;
import com.baidu.mapapi.LocationListener;
import com.example.esun_mobile.Activity.MainActivity;
import com.example.esun_mobile.application.StsApplication;
import com.example.esun_mobile.business.Activity.SetMoreActivity;
import com.example.esun_mobile_bean.comstant.Constant;
import com.example.esun_mobile_bean.utils.HttpRequest;
import com.example.esun_mobile_bean.utils.SharePerfenceUtil;
import com.example.esun_mobile_bean.utils.Utils;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.location.Location;
import android.os.Binder;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.util.Log;
import android.view.View.OnCreateContextMenuListener;
import android.widget.Toast;
public class ServiceDemo extends Service {
private static String CurrentPosition;
private static double Currentlatitude;
private static double Currentlongitude;
StsApplication app;
String callback;// 判断签到是否成功 。1,成功 2,失败
// 百度MapAPI的管理类
public BMapManager mBMapMan = null;
// 循环时间十分钟
private int circulation_time = 600 * 1000;
@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}
// Timer timer;
boolean shouldcollect = true;
@Override
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();
app = (StsApplication) this.getApplication();
if (app.mBMapMan == null) {
app.mBMapMan = new BMapManager(ServiceDemo.this);
app.mBMapMan.init(Constant.KEY, null);
}
app.mBMapMan.start();
app.mBMapMan.getLocationManager().requestLocationUpdates(
mLocationListener);
// timer = new Timer();
// timer.schedule(task, 5000,circulation_time);
shouldcollect = true;
initListener();
}
private void initListener() {
new Thread(new Runnable() {
@Override
public void run() {
try {
while (shouldcollect) {
try {
Thread.sleep(circulation_time);
Message.obtain(mhandler,
100).sendToTarget();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}).start();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
// TODO Auto-generated method stub
app.mBMapMan.getLocationManager().requestLocationUpdates(
mLocationListener);
app.mBMapMan.start();
return Service.START_NOT_STICKY;
// return super.onStartCommand(intent, flags, startId);
}
@Override
public void onStart(Intent intent, int startId) {
super.onStart(intent, startId);
// // 注册定位事件,定位后将地图移动到定位点
if (app != null && app.mBMapMan != null) {
app.mBMapMan.getLocationManager().requestLocationUpdates(
mLocationListener);
app.mBMapMan.start();
}
};
@Override
public void onDestroy() {
Log.i("onDestroy", "" + "onDestroy");
// timer.cancel();
app.mBMapMan.getLocationManager().removeUpdates(mLocationListener);
app.mBMapMan.stop();
shouldcollect = false;
};
// TimerTask task = new TimerTask(){
// @Override
// public void run() {
// // TODO Auto-generated method stub
// // Message message = new Message();
// // message.what = 1;
// // mhandler.sendMessage(message);
// Message.obtain(mhandler, 1).sendToTarget();
// }
// };
// 获得当前位置
public static String getCurrentPosition() {
CurrentPosition = String.valueOf(Currentlongitude) + ","
+ String.valueOf(Currentlatitude);
Log.i("coordinates", "" + CurrentPosition);
return CurrentPosition;
}
// 定位自己的位置
public LocationListener mLocationListener = new LocationListener() {
@Override
public void onLocationChanged(Location location) {
if (location != null) {
Currentlatitude = (double) (location.getLatitude());
Currentlongitude = (double) (location.getLongitude());
Log.i("serjingdu", "" + Currentlatitude);
Log.i("serweidu", "" + Currentlongitude);
} else {
Log.i("lacation", "为空啦");
}
}
};
Handler mhandler = new Handler() {
public void handleMessage(Message msg) {
switch (msg.what) {
case 100:
// getCurrentPosition();
// Toast.makeText(ServiceDemo.this, getCurrentPosition(),
// Toast.LENGTH_SHORT).show();
// Log.i("mhandler", ""+ getCurrentPosition());
sendCurrentMessage();
break;
}
super.handleMessage(msg);
}
};
// 获取本地电话号码
public String getMyPhone() {
String MyPhone = SharePerfenceUtil.getPhone(getApplicationContext());
return MyPhone;
}
// 向服务器发送当前的信息
public void sendCurrentMessage() {
new Thread(new Runnable() {
@Override
public void run() {
HttpRequest httpRequest = new HttpRequest();
Map<String, String> map = new HashMap<String, String>();
map.put("tel", getMyPhone());
map.put("coordinates", getCurrentPosition());
Log.i("tel", "" + getMyPhone());
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Log.i("coordinates", "" + getCurrentPosition());
String res = httpRequest.doPost(Constant.GENZONG_URL,
map);
Log.d("res", "" + res);
try {
callback = Utils.analyCheck(res);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if (callback == null || "2".equals(callback)) {
} else {
}
}
}).start();
}
}