Android获取位置或提示以启用位置服务(如果禁用) [英] Android get location or prompt to enable location service if disabled
问题描述
我发现这个答案的零散部分是通过其他帖子散播的,但我想在此为其他人进行录音。
如何简单地请求用户GPS和/或网络的位置,如果他们还没有启用服务,提示他们这样做? 想要在按钮按钮上捕捉位置,请按照以下步骤操作。如果用户没有启用位置服务,则会将它们发送到设置菜单以启用它。
首先,您必须添加权限android.permission。 ACCESS_COARSE_LOCATION到您的清单。如果您需要GPS(网络位置不够敏感),请改为添加权限android.permission.ACCESS_FINE_LOCATION,并将Criteria.ACCURACY_COARSE更改为Criteria.ACCURACY_FINE。
Button gpsButton =(Button)this.findViewById(R.id.buttonGPSLocation);
gpsButton.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v){
//开始loction服务
LocationManager locationManager =(LocationManager)[OUTERCLASS] .this.getSystemService(Context.LOCATION_SERVICE);
条件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);
$ b $ if(providerName!= null&& locationManager.isProviderEnabled(providerName)){
// Provider is enabled
locationManager.requestLocationUpdates(providerName,20000,100,[OUTERCLASS] .this.lo cationListener);
} else {
// Provider未启用,提示用户启用它
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);
}
}
});
我的外部类设置了此侦听器
< pre $
private final LocaleListener locationListener = new LocationListener(){
@Override
public void onLocationChanged(Location location){
[OUTERCLASS] .this.gpsLocationReceived(位置);
}
@Override
public void onProviderDisabled(String provider){}
$ b @Override
public void onProviderEnabled(String provider){ }
@Override
public void onStatusChanged(String provider,int status,Bundle extras){}
};
然后,每当你想停止监听时调用它。
LocationManager locationManager =(LocationManager)this.getSystemService(Context.LOCATION_SERVICE );
locationManager.removeUpdates(this.locationListener);
I've found bits and pieces of this answer scattered through other posts, but I wanted to record it here for others.
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.
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) {}
};
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获取位置或提示以启用位置服务(如果禁用)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!