2013-03-23 66 views
4

我完成了使用LocationManager并正常工作的经纬度。在背景中每10分钟使用gps获取纬度和经度android

要求:

GET纬度和每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

越来越经度和纬度。

+0

Harshid,你能上传你的代码吗? – Lucifer 2013-03-23 04:42:47

+0

@Lucifer查看我更新的代码 – Harshid 2013-03-23 04:49:31

回答

0

我正在使用这种方式并解决我的问题。

PendingIntent pi=PendingIntent.getBroadcast(context, 0, i, 0); 
    alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), PERIOD , pi); 

我不知道这是对还是错,但为我工作。 谢谢@ Lucifer,@ Maya mAku和@piotrpo为您的指导。

0

尝试这种方式,

,而不是这一行

alarmManager.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, 
     SystemClock.elapsedRealtime()+60000,PERIOD,pi); 

试试我的逻辑,

alarmManager.set(AlarmManager.RTC_WAKEUP, 60 * 1000, pi); 

您还可以看到的AlarmManager一个很好的例子。

+0

我正在尝试更改我的代码。 – Harshid 2013-03-23 05:01:09

+0

为什么pendingIntent不在后台调用。 – Harshid 2013-03-23 05:23:10

1

您的位置真的会不会保存在数据库中,因为您的每一次位置更新(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); 
} 
0

我没有看到任何服务最终确定命令,而你正在试图让只在启动命令的位置。

一般来说 - 这是相当复杂的问题: 10分钟的时间段建议使用AlarmManager - 没关系但是... 获取位置有时会持续10分钟以上。 GPS接收器在尝试定位时会消耗非常多的电量,因此有可能在某些情况下,定期开启/关闭GPS会比开启GPS更耗电。 考虑一些“松散压缩” - 结合不同的位置提供商只在真正需要时调用GPS(即时间,粗略位置改变等)

0
Try By this one : 

private void doSomethingRepeatedly() { 
     timer.scheduleAtFixedRate(new TimerTask() { 
      public void run() { 

        try{ 

      //Do your work here 

        } 
        catch (Exception e) { 
         // TODO: handle exception 
        } 

      } 
      }, 0, 10000); 
        }