問(wèn)題描述
public void onCreate() {
locationListener = new GeoUpdateHandler();
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
try {
gps_enabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
network_enabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
} catch (Exception ex) {
}
if(gps_enabled) {
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, TIME_DURATION, 100, locationListener);
} else if(network_enabled) { // Checking for GSM
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, TIME_DURATION, DISTANCE, locationListener);
}
}
public class GeoUpdateHandler implements LocationListener {
private void getPresentLocation(Location location) {
...
//Fetch the locations
...
}
@Override
public void onProviderDisabled(String provider) { }
@Override
public void onProviderEnabled(String provider) { }
@Override
public void onStatusChanged(String provider, int status, Bundle extras) { }
@Override
public void onLocationChanged(Location location) {
getPresentLocation(location);
}
}
這是我的代碼片段,我的問(wèn)題是,一旦在 GPS 開(kāi)啟的情況下安裝了應(yīng)用程序,那么一旦我們關(guān)閉 GPS,它就不會(huì)切換到 GSM 來(lái)獲取位置,反之亦然.
this is my code snippet and my issue is that once the application is installed with GPS Switched ON, then it is not switching to GSM to fetch location once we OFF GPS, and vice versa.
所以請(qǐng)告訴我一種根據(jù)位置提供商的可用性在 GPS 和 GSM 之間有效切換的方法.
So please tell me a way to switch efficiently between GPS and GSM according to the availability of the location provider.
注意:我的應(yīng)用是一項(xiàng)服務(wù).
NOTE: My app is a service.
謝謝.
我有一個(gè)廣播接收器,這就是為什么我假設(shè)一旦 GPS 開(kāi)/關(guān)發(fā)生就會(huì)調(diào)用 onCreate().
I got a BroadcastReceiver, thats why i assume the onCreate() will be invoked once the GPS ON/OFF happen.
我還嘗試在 onLocationChanged(Location location) 中檢查提供程序的可用性.但它沒(méi)有幫助.
I also tried checking the provider availability in onLocationChanged(Location location). But it didnt helped.
我修改了我的 onProviderDisabled &onProviderEnabled 像這樣,請(qǐng)告訴我在這里做錯(cuò)了什么..
I modified my onProviderDisabled & onProviderEnabled like this, Please tell what i am doing wrong here..
public void onProviderDisabled(String provider) {
locationManager.removeUpdates(locationListener);
if (provider.toLowerCase().equals(LocationManager.GPS_PROVIDER)) {
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, TIME_DURATION, 1, locationListener);
} else {
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, TIME_DURATION, 1, locationListener);
}
}
@Override
public void onProviderEnabled(String provider) {
locationManager.removeUpdates(locationListener);
if (provider.toLowerCase().equals(LocationManager.GPS_PROVIDER)) {
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, TIME_DURATION, 1, locationListener);
} else {
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, TIME_DURATION, 1, locationListener);
}
}
推薦答案
得到了解決方案:
public void onProviderDisabled(String provider) {
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
} else {
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);
}
}
@Override
public void onProviderEnabled(String provider) {
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
} else {
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);
}
}
我錯(cuò)過(guò)的是locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);在 onProviderEnabled() 和 onProviderDisabled() 中.
what i missed is locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); in onProviderEnabled() and onProviderDisabled().
謝謝
這篇關(guān)于根據(jù)可用性在 gps 和網(wǎng)絡(luò)提供商之間切換的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!