2011-10-25 57 views
3

的Android requestLocationUpdates如何使用使用的PendingIntent与广播接收器

requestLocationUpdates(long minTime, float minDistance, Criteria criteria, 
                  PendingIntent intent) 

在BroadcastReciver让我能继续得到GPS坐标。

我是否必须为LocationListener创建一个单独的类?

我的项目目标是当我收到BOOT_COMPLETED开始获取GPS lats和longs定期。

代码我想的是:

public class MobileViaNetReceiver extends BroadcastReceiver { 

    LocationManager locmgr = null; 
    String android_id; 
    DbAdapter_GPS db; 

    @Override 
    public void onReceive(Context context, Intent intent) { 
     if ("android.intent.action.BOOT_COMPLETED".equals(intent.getAction())) { 
      startGPS(context); 
     } else { 
      Log.i("MobileViaNetReceiver", "Received unexpected intent " 
       + intent.toString()); 
     } 
    } 

    public void startGPS(Context context) { 
     Toast.makeText(context, "Waiting for location...", Toast.LENGTH_SHORT) 
       .show(); 
     db = new DbAdapter_GPS(context); 
     db.open(); 
     android_id = Secure.getString(context.getContentResolver(), 
       Secure.ANDROID_ID); 
     Log.i("MobileViaNetReceiver", "Android id is _ _ _ _ _ _" + android_id); 
     locmgr = (LocationManager) context 
       .getSystemService(Context.LOCATION_SERVICE); 
     locmgr.requestLocationUpdates(LocationManager.GPS_PROVIDER, 5000, 5, 
       onLocationChange); 
    } 

    LocationListener onLocationChange = new LocationListener() { 

     public void onLocationChanged(Location loc) { 
      // sets and displays the lat/long when a location is provided 
      Log.i("MobileViaNetReceiver", "In onLocationChanged ....."); 
      String latlong = "Lat: " + loc.getLatitude() + " Long: " 
       + loc.getLongitude(); 
      // Toast.makeText(this, latlong, Toast.LENGTH_SHORT).show(); 
      Log.i("MobileViaNetReceiver", latlong); 
      try { 
       db.insertGPSCoordinates(android_id, 
         Double.toString(loc.getLatitude()), 
         Double.toString(loc.getLongitude())); 
      } catch (Exception e) { 
       Log.i("MobileViaNetReceiver", 
         "db error catch _ _ _ _ " + e.getMessage()); 
      } 
     } 

     public void onProviderDisabled(String provider) {} 

     public void onProviderEnabled(String provider) {} 

     public void onStatusChanged(String provider, int status, Bundle extras) {} 
    };  
    //pauses listener while app is inactive 
    /*@Override 
    public void onPause() { 
     super.onPause(); 
     locmgr.removeUpdates(onLocationChange); 
    } 

    //reactivates listener when app is resumed 
    @Override 
    public void onResume() { 
     super.onResume(); 
     locmgr.requestLocationUpdates(LocationManager.GPS_PROVIDER, 5000, 5, onLocationChange); 
    }*/ 
} 
+0

您可能想要接受下面的答案。 –

回答

5

这样做有两种方式:

  1. 使用您的方法和注册BroadcastReceiver,其具有的意图相匹配的意图过滤器在您的PendingIntent(2.3+)以内,或者如果您只对单个位置提供商感兴趣,则可以使用requestLocationUpdates (String provider, long minTime, float minDistance, PendingIntent intent)(1.5+)。
  2. 使用requestLocationUpdates (String provider, long minTime, float minDistance, LocationListener listener)方法LocationManager注册LocaltionListener

我认为你开始有点困惑,因为你可以使用要么处理位置更新的BroadcastReceiver一个LocationListener - 你不需要两者。注册更新的方法非常相似,但您如何接收它们实际上是非常不同的。

A BroadcastReceiver即使当前未运行,您的应用程序/服务也会被唤醒。在服务未运行时关闭服务将显着降低您对用户电池的影响,并最大限度地减少Task Killer应用终止服务的机会。

LocationListener将要求您保持服务正常运行,否则当服务关闭时您的LocationListener将会死亡。如果您使用这种方法,您将面临极度偏见的Task Killer应用程序导致您的服务中断的风险。

从你的问题,我怀疑你需要使用BroadcastReceiver方法。

+0

我想我感到困惑。我添加了试过的代码。那有什么问题? – user533844

+0

你的'LocationListener'需要在一个'Service'中,它必须永久保存,否则你根本不会收到任何更新。正如我在回答中所说的那样,您最好使用另一种处理位置更新的方法,方法是注册一个PendingIntent,这将在每次发生位置更新时发送广播以唤醒您的BroadcastReceiver。尝试阅读http://stackoverflow.com/questions/5240246/broadcastreceiver-for-location/5240774#5240774了解更多信息。 –

+0

我想我正在接受你说的话。我应该把GPS代码作为活动(包括locationlistener)放在课堂上。在广播接收器中,将该意图设置为待定意图。我对吗 ?你有一个例子吗?谢谢你。 – user533844

1
public class MobileViaNetReceiver extends BroadcastReceiver { 

    private static final String TAG = "MobileViaNetReceiver"; // please 

    @Override 
    public void onReceive(Context context, Intent intent) { 
     if ("android.intent.action.BOOT_COMPLETED".equals(intent.getAction())){ 
      Log.i(TAG, "Boot : registered for location updates"); 
      LocationManager lm = (LocationManager) context 
           .getSystemService(Context.LOCATION_SERVICE); 
      Intent intent = new Intent(context, this.getClass()); 
      PendingIntent pi = PendingIntent.getBroadcast(context, 0, intent, 
             PendingIntent.FLAG_UPDATE_CURRENT); 
      lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 5000,5,pi); 
     } else { 
      String locationKey = LocationManager.KEY_LOCATION_CHANGED; 
      if (intent.hasExtra(locationKey)) { 
       Location loc = (Location) intent.getExtras().get(locationKey); 
       Log.i(TAG, "Location Received"); 
       try { 
        DbAdapter_GPS db = new DbAdapter_GPS(context);//what's this 
        db.open(); 
        String android_id = Secure.getString(
         context.getContentResolver(), Secure.ANDROID_ID); 
        Log.i(TAG, "Android id is :" + android_id); 
        db.insertGPSCoordinates(android_id, 
         Double.toString(loc.getLatitude()), 
         Double.toString(loc.getLongitude())); 
       } catch (Exception e) { // NEVER catch generic "Exception" 
        Log.i(TAG, "db error catch :" + e.getMessage()); 
       } 
      } 
     } 
    } 
}