太從GPS接收數據,你應該使用這樣的代碼:
locationMan= (LocationManager)this.getSystemService(Context.LOCATION_SERVICE);
locationMan.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
locationMan.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);
LocationListener locationListener= new LocationListener() {
public void onLocationChanged(Location location) {
//TODO be sure the new position is really better then the one already had.
lastPos= location;
}
public void onStatusChanged(String provider, int status, Bundle extras) {
}
public void onProviderEnabled(String provider) {
}
public void onProviderDisabled(String provider) {
}
};
這是一個簡單的偵聽器,每一個安卓知道currente位置的改變,它會調用所有偵聽時間,這包括你的應用程序和其他應用程序。因此,無論將多少個監聽器添加到LOCATION_SEVICE,發現位置的過程只運行一次。
一旦你與GPS使用
locationMan.removeUpdates(locationListener);
進行刪除您的聽衆
感謝WarrenFaith的答案,我想嘗試一種更有效的方式來使用這個服務,這是一些不會重複調用系統服務的東西。 – rahul 2012-02-09 04:47:11
對不起,我正在使用\t locationManager =(LocationManager)getSystemService(Context.LOCATION_SERVICE); \t \t locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,0,意外地發佈了NotificationManager,對不起。 – rahul 2012-02-09 04:59:37