2011-10-25 54 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()); 
       } 
      } 
     } 
    } 
}