android定时获取gps,在后台android中每10分钟使用gps获取经度和经度

您的位置确实不会保存在数据库中,因为每次更新您的位置时(通过onLocationChanged(位置位置))

你应该做的是在你的OnCreate()方法中使用你的timerinitialization.然后声明这些变量.

double lat = location.getLatitude();

double lng = location.getLongitude();

double alt = location.getAltitude();

作为全局并在onLocationChanged(位置位置)方法中更新它们.这样,只要计时器调用数据库中的持久性,lat,lng,alt值就会可用并根据您的最新位置进行更新.

//decalred as global variables

String curTime;

double lat;

double lng;

double alt;

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

LocationManager locationManager;

String context = Context.LOCATION_SERVICE;

locationManager = (LocationManager)getSystemService(context);

Criteria criteria = new Criteria();

criteria.setAccuracy(Criteria.ACCURACY_FINE);

criteria.setAltitudeRequired(false);

criteria.setBearingRequired(false);

criteria.setCostAllowed(true);

criteria.setPowerRequirement(Criteria.POWER_LOW);

String provider = locationManager.getBestProvider(criteria,true);

//Initialize timer

Timer timer = new Timer();

timer.schedule(new TimerTask(){

@Override

public void run(){

db.execSQL("INSERT INTO location (longitude,latitude,altitude,tgl_buat) VALUES " +

"('"+lng+"','"+lat+"','"+alt+"','"+curTime+"')");

db.close();

}

},10*60*1000,10*60*1000);

updateWithNewLocation(null);

locationManager.requestLocationUpdates(provider,(10*60*1000),10,locationListener);

}

private final LocationListener locationListener = new LocationListener() {

public void onLocationChanged(Location location) {

updateWithNewLocation(location);

}

public void onProviderDisabled(String provider){

updateWithNewLocation(null);

}

public void onProviderEnabled(String provider){ }

public void onStatusChanged(String provider,int status,Bundle extras){ }

};

public void updateWithNewLocation(Location location) {

if (location != null) {

Dbhelper helper = new Dbhelper(this);

final SQLiteDatabase db = helper.getWritableDatabase();

long time = System.currentTimeMillis();

SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd kk:mm:ss");

curTime = df.format(time);

lat = location.getLatitude();

lng = location.getLongitude();

alt = location.getAltitude();

System.out.println(lat);

System.out.println(lng);

System.out.println(alt);

}

}

添加: – 如果您绘制路径,则使用此代码

/** Called when the activity is first created. */

private List mapOverlays;

private Projection projection;

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

linearLayout = (LinearLayout) findViewById(R.id.zoomview);

mapView = (MapView) findViewById(R.id.mapview);

mapView.setBuiltInZoomControls(true);

mapOverlays = mapView.getOverlays();

projection = mapView.getProjection();

mapOverlays.add(new MyOverlay());

}

@Override

protected boolean isRouteDisplayed() {

return false;

}

class MyOverlay extends Overlay{

public MyOverlay(){

}

public void draw(Canvas canvas,MapView mapv,boolean shadow){

super.draw(canvas,mapv,shadow);

Paint mPaint = new Paint();

mPaint.setDither(true);

mPaint.setColor(Color.RED);

mPaint.setStyle(Paint.Style.FILL_AND_STROKE);

mPaint.setStrokeJoin(Paint.Join.ROUND);

mPaint.setStrokeCap(Paint.Cap.ROUND);

mPaint.setStrokeWidth(2);

GeoPoint gP1 = new GeoPoint(19240000,-99120000);

GeoPoint gP2 = new GeoPoint(37423157,-122085008);

Point p1 = new Point();

Point p2 = new Point();

Path path = new Path();

projection.toPixels(gP1,p1);

projection.toPixels(gP2,p2);

path.moveTo(p2.x,p2.y);

path.lineTo(p1.x,p1.y);

canvas.drawPath(path,mPaint);

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值