我已经完成使用LocationManager的纬度和经度,并且工作正常。
要求:
如果应用程序是否关闭,则每10分钟获取一次经纬度。
我正在尝试服务,线程,AlarmManager等。
我的应用程序可以正常工作1-2个小时,然后自动不获得经度和纬度,但是我的服务仍在运行。
如果有人知道,那么请给我指南如何在后台每10分钟获取一次经纬度。
LocationActivity.java
当用户单击开始按钮时。

Intent i=new Intent(context, OnAlarmReceiver.class);
    PendingIntent pi=PendingIntent.getBroadcast(context, 0, i, 0);
    alarmManager.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,
         SystemClock.elapsedRealtime()+60000,PERIOD,pi);
OnAlarmReceiver.java
我开始我的服务。
public class OnAlarmReceiver extends BroadcastReceiver {
  @Override
  public void onReceive(Context context, Intent intent) {
       context.startService(new Intent(context, MyService.class));
  }
}
MyService.java
@Override
    public void onStart(Intent intent, int startId) {
        // super.onStart(intent, startId);
        new MyTime(mContext);
    }
MyTime.java
经度和纬度。

最佳答案

您的位置实际上不会保存在数据库中,因为每次更新您的位置(via onLocationChanged(Location location))
您应该做的是将timer初始化引入OnCreate()方法中。然后声明这些变量。

double lat = location.getLatitude();
double lng = location.getLongitude();
double alt = location.getAltitude();

为全局,并在onLocationChanged(Location location)方法中对其进行更新。这样,每当计时器调用数据库中的持久性时,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<Overlay> 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);
}

关于android - 每隔10分钟在后台android中使用gps获取经度和纬度,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/15583426/

10-09 20:18
查看更多