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();
}
}
...全文
467 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
击不垮我 2014-03-22
  • 打赏
  • 举报
回复
引用 4 楼 huxiweng 的回复:
[quote=引用 3 楼 u011717237 的回复:] [quote=引用 1 楼 huxiweng 的回复:] 真机测试吗?? 目测没一会能定位一会不能
是真机测试,我测了很多次,大多数情况下能跟踪成功即坐标正常,少数情况下跟踪失败,坐标为0的情况[/quote] 有时候定位确实不准确,有点误差可以理解的[/quote] 亲,误差是有数据但不够准确,我是没数据的呀,坐标为0,0 这是错误了...
tcmakebest 2014-03-22
  • 打赏
  • 举报
回复
这代码这么帖怎么看啊,提示楼主定位过程中需要控制不能让手机休眠
teemai 2014-03-21
  • 打赏
  • 举报
回复
引用 3 楼 u011717237 的回复:
[quote=引用 1 楼 huxiweng 的回复:] 真机测试吗?? 目测没一会能定位一会不能
是真机测试,我测了很多次,大多数情况下能跟踪成功即坐标正常,少数情况下跟踪失败,坐标为0的情况[/quote] 有时候定位确实不准确,有点误差可以理解的
击不垮我 2014-03-21
  • 打赏
  • 举报
回复
引用 1 楼 huxiweng 的回复:
真机测试吗?? 目测没一会能定位一会不能
是真机测试,我测了很多次,大多数情况下能跟踪成功即坐标正常,少数情况下跟踪失败,坐标为0的情况
击不垮我 2014-03-21
  • 打赏
  • 举报
回复
肯定是真机测试。我测了很多次,大多数情况下能跟踪成功即坐标正常,少数情况下跟踪失败,坐标为0的情况
teemai 2014-03-20
  • 打赏
  • 举报
回复
真机测试吗?? 目测没一会能定位一会不能

80,350

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧