0

1.在我的应用程序中,我想每 1 分钟跟踪一次用户当前位置,然后保存在本地数据库中。没有得到正确的位置有时,当用户正在走路时

2.寻找位置,我想使用谷歌播放服务API。

我的需求:

后台服务应该获取位置,每分钟保存在本地数据库中。甚至应用程序已关闭。

还具有低电池使用率

我尝试了一些代码,一些方法已被弃用并且不起作用。

请建议我任何解决方案。

4

1 回答 1

0

这是您可以实现它的方法之一,使您的服务类如下所示

这里,

使用此ScheduledThreadPoolExecutor ,您的服务将在后台开启,您将需要LocationManager

您的SERVICE.class扩展服务实现 LocationListener、Runnable

    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
}

当您在应用程序中需要位置时,请使用它。

于 2015-12-28T08:53:41.167 回答