2013-03-23 56 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); 
        }