如何使用requestLocationUpdates / removeLocationUpdates节省电池寿命

时间:2013-07-01 00:50:14

标签: android google-maps

我正在制作一个简单的位置提醒,用户在地图上选择一个地址,然后用它创建一个proximityAlert。

我的第一个想法是,如果甚至可以在该时间范围内到达目的地,则仅搜索新位置。 F.E.如果目标距离是80英里,则不断查询该位置是没有意义的,因为距离车辆大约需要一个小时。 我发现了另一种方法here 它只会在驾驶时获得位置更新。

  1. 我想对这两种方法有所了解。这会真的比使用proximityAlert节省电池寿命吗?
  2. 如果我自己进行这些计算,我是否必须实现我自己的proximityAlert()版本?
  3. 接收位置更新的LocationIntentService是一个单独的类:如何访问位置客户端以删除更新和/或requestLocationUpdates?
  4. 我是否必须使用AlarmManager来唤醒位置更新?
  5. 我几乎陷入了onHandleIntent()方法:我不知道该怎么做。我很高兴任何投入

        public class MyMapActivity extends Activity implements OnInfoWindowClickListener, 
           OnMarkerClickListener, OnMapLongClickListener, 
           GooglePlayServicesClient.ConnectionCallbacks,
           GooglePlayServicesClient.OnConnectionFailedListener
        {
    
           private LocationClient mLocationClient;
           private PendingIntent mPendingIntent = null;
           private LocationRequest mLocationRequest = LocationRequest.create()
              .setInterval(5000)         // 5 seconds
              .setFastestInterval(16)    // 16ms = 60fps
              .setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
    
    
           @Override
       protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_gmaps);
    
                mLocationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
                map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
                map.setMyLocationEnabled(true);
                map.setMapType(GoogleMap.MAP_TYPE_HYBRID);
                map.setInfoWindowAdapter(new MyInfoWindowAdapter(this, getLayoutInflater()));
                map.setOnMapLongClickListener(this);
                map.setOnInfoWindowClickListener(this);
                map.setOnMarkerClickListener(this);
                String locationProvider = findBestLocationProvider();
                if(locationProvider != null) {
                      mMyLocation = mLocationManager.getLastKnownLocation(locationProvider);
                }
                Intent intent = new Intent( getApplicationContext(), LocationIntentService.class);
                intent.putExtra(LOCATION_KEY, mMyLocation);
                mPendingIntent = PendingIntent.getService(getApplicationContext(), 0, intent,              PendingIntent.FLAG_UPDATE_CURRENT);
           }
    
           @Override
           public void onConnected(Bundle arg0) {
                Log.d(TAG, "onConnected()");
                if(mLocationClient != null) {
                    mLocationClient.requestLocationUpdates(mLocationRequest, mPendingIntent);
                }
           }
    
           @Override
           public void onDisconnected() {
                Log.d(TAG, "onDisconnected()");
           }
        }
    

    服务:

      public class LocationIntentService  extends IntentService {
    
         private Location mLocation = null;
    
         @Override
         protected void onHandleIntent(Intent intent) {
             Log.d(tag, "caught an intent");
    
             if(intent.hasExtra(MyMapActivity.LOCATION_KEY)) {
             Location loc = (Location) intent.getExtras().get(MyMapActivity.LOCATION_KEY);
             Log.d(tag, "onHandleIntent(): new location: " + loc.toString());
    
               // do calculation here.
               // how would I get access to the location client to removeUpdates and/or requestLocationUpdates
              // remove locationUpdates and start Alarm to start locationUpdates in X minutes
             mLocation = loc;
            }
         }
    

0 个答案:

没有答案