2015-12-28 25 views
0

1.在我的应用程序中,我想跟踪用户当前位置每隔1分钟然后保存在本地数据库中。 没有得到正确的位置有时候,当用户步行时获取位置定期使用后台服务,在Android中保存本地数据库

2.对于查找位置,我想使用Google play Services API。

我的需要:

后台服务应得的位置,在本地数据库中保存每个minute.Even应用程序被关闭。

还与低电池使用情况

我试过一些代码一些方法已被弃用,而不是工作。

请建议我任何解决方案。

+0

只需使用谷歌的地方API为Android,它的快速和容易。 它使用http请求,所以它不会是一个问题的android – PauAI

+0

好吧,但没有找到任何与后台服务,使用谷歌Places API查找的例子。 – Kumar

+0

https://developers.google.com/places/android-api/current-place – PauAI

回答

0

这是可以实现它,让你的服务类像下面

这里的途径之一,

与此的ScheduledThreadPoolExecutor您的服务将在后台 ,你将需要的LocationManager

SERVICE.class延长服务实现LocationListener的,可运行

private LocationManager mgr = null; 
    private ScheduledThreadPoolExecutor executor = new ScheduledThreadPoolExecutor(1); 



     @Override 
     public void onCreate() { 
       //check gps is on/off 
       //get locationmanager 
      } 

     @Override 
     public int onStartCommand(Intent intent, int flags, int startId) { 
        //check if internet is on/off 
        setBeforeDelay15(); 
        return super.onStartCommand(intent, flags, startId); 
     } 

     @Override 
     public void onLocationChanged(Location location) { 
        //re-execute at time you need with **scheduleAtFixedRate** 
     } 

     @Override 
     public void run() { 
       //here get the best location from two ways 1>gps and 2>network provider 
       //once you get this insert to database 
       //set delay then request call method again 
     } 

      @Override 
      public void onDestroy() { 
       //if locationmanager is not null then remove all updates 
      } 

     private void setDelay15() { 
      new Handler().postDelayed(new Runnable() { 

       @Override 
       public void run() {    
        // Log.e("srop self by", "delay15"); 
        startWork();   
       } 
      }, 300000);//120000 
     } 

private void startWork() { 
//check here for 1>gps and 2>network provider 
//get location and save it 
} 

使用此,当您需要位置在您的应用程序。

相关问题