問題描述
我在其他帖子中發現了這個答案的零碎,但我想在這里記錄下來以供其他人使用.
I've found bits and pieces of this answer scattered through other posts, but I wanted to record it here for others.
我怎樣才能簡單地請求用戶的 GPS 和/或網絡位置,并且如果他們尚未啟用該服務,則提示他們這樣做?
How can I simply request the user's GPS and/or Network location and, if they haven't enabled the service, prompt them to do so?
推薦答案
如果您想在按鈕按下時捕獲位置,請按照以下步驟操作.如果用戶沒有啟用定位服務,這會將他們發送到設置菜單以啟用它.
If you want to capture the location on a button push, here's how you'd do it. If the user does not have a location service enabled, this will send them to the settings menu to enable it.
首先,您必須在清單中添加權限android.permission.ACCESS_COARSE_LOCATION".如果需要GPS(網絡定位不夠靈敏),添加權限android.permission.ACCESS_FINE_LOCATION",將Criteria.ACCURACY_COARSE"改為Criteria.ACCURACY_FINE"
First, you must add the permission "android.permission.ACCESS_COARSE_LOCATION" to your manifest. If you need GPS (network location isn't sensitive enough), add the permission "android.permission.ACCESS_FINE_LOCATION" instead, and change the "Criteria.ACCURACY_COARSE" to "Criteria.ACCURACY_FINE"
Button gpsButton = (Button)this.findViewById(R.id.buttonGPSLocation);
gpsButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// Start loction service
LocationManager locationManager = (LocationManager)[OUTERCLASS].this.getSystemService(Context.LOCATION_SERVICE);
Criteria locationCritera = new Criteria();
locationCritera.setAccuracy(Criteria.ACCURACY_COARSE);
locationCritera.setAltitudeRequired(false);
locationCritera.setBearingRequired(false);
locationCritera.setCostAllowed(true);
locationCritera.setPowerRequirement(Criteria.NO_REQUIREMENT);
String providerName = locationManager.getBestProvider(locationCritera, true);
if (providerName != null && locationManager.isProviderEnabled(providerName)) {
// Provider is enabled
locationManager.requestLocationUpdates(providerName, 20000, 100, [OUTERCLASS].this.locationListener);
} else {
// Provider not enabled, prompt user to enable it
Toast.makeText([OUTERCLASS].this, R.string.please_turn_on_gps, Toast.LENGTH_LONG).show();
Intent myIntent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
[OUTERCLASS].this.startActivity(myIntent);
}
}
});
我的外部班級設置了這個監聽器
My outer class has this listener set up
private final LocationListener locationListener = new LocationListener() {
@Override
public void onLocationChanged(Location location) {
[OUTERCLASS].this.gpsLocationReceived(location);
}
@Override
public void onProviderDisabled(String provider) {}
@Override
public void onProviderEnabled(String provider) {}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {}
};
然后,每當您想停止收聽時,請調用它.您至少應該在活動的 onStop 方法期間進行此調用.
Then, whenever you want to stop listening call this. You should at least make this call during your activity's onStop method.
LocationManager locationManager = (LocationManager)this.getSystemService(Context.LOCATION_SERVICE);
locationManager.removeUpdates(this.locationListener);
這篇關于如果禁用,Android 獲取位置或提示啟用位置服務的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!