在特定时间间隔后获取当前设备位置

问题描述:

我想在特定时间间隔(比如说30分钟)后捕获android设备的当前位置(纬度和经度)..我的课程(或服务??不知道要使用什么)当设备引导完成时到LocationManagerListener。什么是实施这个最好的方法?我们如何在这种情况下使用locationChanged()方法?在特定时间间隔后获取当前设备位置

这是我认为它可以去:

监听启动完成的事件,并设置报警服务:

public class OnBootReceiver extends BroadcastReceiver { 
    private static final int PERIOD=1800000; // 30 minutes 

    @Override 
    public void onReceive(Context context, Intent intent) { 

    AlarmManager mgr=(AlarmManager)context.getSystemService(Context.ALARM_SERVICE); 
    Intent i=new Intent(context, OnAlarmReceiver.class); 
    PendingIntent pi=PendingIntent.getBroadcast(context, 0, 
               i, 0); 

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

监听报警服务,并启动位置捕获类或服务:

public class OnAlarmReceiver extends BroadcastReceiver { 
    @Override 
    public void onReceive(Context context, Intent intent) {  
     WakefulIntentService.acquireStaticLock(context); 
     context.startService(new Intent(context, locationCapture.class)); 
     or 
     new locationCapture().classmethod(); 
    } 
    } 

我不知道locationCapture类应该如何实现。它应该是普通的Java类还是Service类?

任何帮助将不胜感激。

此服务类,你可以用它

public class ServiceLocation extends Service{ 
    private LocationManager locMan; 
    private Boolean locationChanged; 
    private Handler handler = new Handler(); 

    public static Location curLocation; 
    public static boolean isService = true; 

    LocationListener gpsListener = new LocationListener() { 
     public void onLocationChanged(Location location) { 
      if (curLocation == null) { 
       curLocation = location; 
       locationChanged = true; 
      }else if (curLocation.getLatitude() == location.getLatitude() && curLocation.getLongitude() == location.getLongitude()){ 
       locationChanged = false; 
       return; 
      }else 
       locationChanged = true; 

      curLocation = location; 

      if (locationChanged) 
       locMan.removeUpdates(gpsListener); 

     } 
     public void onProviderDisabled(String provider) { 
     } 

     public void onProviderEnabled(String provider) { 
     } 

     public void onStatusChanged(String provider, int status,Bundle extras) { 
      if (status == 0)// UnAvailable 
      { 
      } else if (status == 1)// Trying to Connect 
      { 
      } else if (status == 2) {// Available 
      } 
     } 

    }; 

    @Override 
    public void onCreate() { 
     super.onCreate(); 

     curLocation = getBestLocation(); 

     if (curLocation == null) 
      Toast.makeText(getBaseContext(),"Unable to get your location", Toast.LENGTH_SHORT).show(); 
     else{ 
      //Toast.makeText(getBaseContext(), curLocation.toString(), Toast.LENGTH_LONG).show(); 
     } 

     isService = true; 
    } 
    final String TAG="LocationService"; 
    @Override 
    public int onStartCommand(Intent intent, int flags, int startId) { 
    return super.onStartCommand(intent, flags, startId); 
    } 
    @Override 
    public void onLowMemory() { 
     super.onLowMemory(); 
    } 

    @Override 
    public void onStart(Intent i, int startId){ 
     handler.postDelayed(GpsFinder,1); 
    } 

    @Override 
    public void onDestroy() { 
     handler.removeCallbacks(GpsFinder); 
     handler = null; 
     Toast.makeText(this, "Stop services", Toast.LENGTH_SHORT).show(); 
     isService = false; 
    } 

    public IBinder onBind(Intent arg0) { 
     return null; 
    } 

    public Runnable GpsFinder = new Runnable(){ 
    public void run(){ 

     Location tempLoc = getBestLocation(); 
     if(tempLoc!=null) 
      curLocation = tempLoc; 
     tempLoc = null; 
     handler.postDelayed(GpsFinder,1000);// register again to start after 1 seconds...   
    } 
    }; 

    private Location getBestLocation() { 
     Location gpslocation = null; 
     Location networkLocation = null; 

     if(locMan==null){ 
      locMan = (LocationManager) getApplicationContext() .getSystemService(Context.LOCATION_SERVICE); 
     } 
     try { 
      if(locMan.isProviderEnabled(LocationManager.GPS_PROVIDER)){ 
       locMan.requestLocationUpdates(LocationManager.GPS_PROVIDER,1000, 1, gpsListener);// here you can set the 2nd argument time interval also that after how much time it will get the gps location 
       gpslocation = locMan.getLastKnownLocation(LocationManager.GPS_PROVIDER); 

      } 
      if(locMan.isProviderEnabled(LocationManager.NETWORK_PROVIDER)){ 
       locMan.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,1000, 1, gpsListener); 
       networkLocation = locMan.getLastKnownLocation(LocationManager.NETWORK_PROVIDER); 
      } 
     } catch (IllegalArgumentException e) { 
      //Log.e(ErrorCode.ILLEGALARGUMENTERROR, e.toString()); 
      Log.e("error", e.toString()); 
     } 
     if(gpslocation==null && networkLocation==null) 
      return null; 

     if(gpslocation!=null && networkLocation!=null){ 
      if(gpslocation.getTime() < networkLocation.getTime()){ 
       gpslocation = null; 
       return networkLocation; 
      }else{ 
       networkLocation = null; 
       return gpslocation; 
      } 
     } 
     if (gpslocation == null) { 
      return networkLocation; 
     } 
     if (networkLocation == null) { 
      return gpslocation; 
     } 
     return null; 
    } 
} 

您可以设置一次进入处理程序或进入requestLocationUpdates()。你需要从家里开始这项服务。由于我已经在处理程序和requestLocationUpdate()中设置了1秒以获得1秒后的位置并更新我。

编辑: 由于此服务获取用户的当前位置,您可以从家庭活动开始,也可以从启动时开始。有了家庭活动,如果服务被用户使用另一个应用程序(如任务杀手)停止,那么当用户启动应用程序时,然后从家中重新启动该服务,启动服务,您可以这样做

startService(new Intent(YourActivity.this,ServiceLocation.class)); 

当您需要停止服务时,它会调用onDestroy(),以便处理程序可以取消线程以继续获取位置。

由于GPS设置为1秒(1000),这将每1秒获得gps位置,但为了获取该位置,您需要每次打电话,在我的情况下,我已将其设置为1秒,并根据您的要求设置为30秒。所以你每1秒获得一次位置,并在处理器中使用这个服务设置30分钟。为了节省电池使用时间,您还可以设置不同的时间到GPS请求方法,以便节省电池寿命。

您可以删除位置和当前位置的比较部分,因为每当位置发生变化时,每次调用locationlistener时,都可以在应用程序中的任意位置使用curLocation来获取当前位置,但请确保首先必须开始此操作服务第一,然后后就可以使用,否则你得到空指针异常,当你进入此对象的纬度和经度

希望你有一些想法,我得到的回答你的疑问

+0

嘿pratik - 非常感谢您的时间。我有几个疑问 - – Swati

+0

好吧问问题。你有什么疑问? – Pratik

+0

嘿pratik - 非常感谢您的时间。我有几个疑问 - 1)你建议这个服务在OnAlarmReceiver类或直接在OnBootReceiver类下启动(请参考上面的代码)2)何时和谁将调用此服务的OnDestroy()? 3)为什么我们在每隔1秒后在代码处理程序.postDelayed(GpsFinder,1000)之后得到“最后的位置”... requestLocationUpdates()会在每隔一秒钟后调用OnLocationChanged(),我们将比较旧位置和当前位置这不够吗? – Swati

本指南以源代码各种API水平来源:

http://android-developers.blogspot.com/search/label/Location