我正在开发一个应用程序在Android操作系统。我不知道如何检查位置服务是否启用。
我需要一个方法,如果它们被启用,返回“true”,如果没有“false”(所以在最后一种情况下,我可以显示一个对话框来启用它们)。
我正在开发一个应用程序在Android操作系统。我不知道如何检查位置服务是否启用。
我需要一个方法,如果它们被启用,返回“true”,如果没有“false”(所以在最后一种情况下,我可以显示一个对话框来启用它们)。
当前回答
您可以使用此代码引导用户到设置,在那里他们可以启用GPS:
locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
if( !locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER) ) {
new AlertDialog.Builder(context)
.setTitle(R.string.gps_not_found_title) // GPS not found
.setMessage(R.string.gps_not_found_message) // Want to enable?
.setPositiveButton(R.string.yes, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialogInterface, int i) {
owner.startActivity(new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS));
}
})
.setNegativeButton(R.string.no, null)
.show();
}
其他回答
您可以使用此代码引导用户到设置,在那里他们可以启用GPS:
locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
if( !locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER) ) {
new AlertDialog.Builder(context)
.setTitle(R.string.gps_not_found_title) // GPS not found
.setMessage(R.string.gps_not_found_message) // Want to enable?
.setPositiveButton(R.string.yes, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialogInterface, int i) {
owner.startActivity(new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS));
}
})
.setNegativeButton(R.string.no, null)
.show();
}
是的,你可以检查以下是代码:
public boolean isGPSEnabled(Context mContext)
{
LocationManager lm = (LocationManager)
mContext.getSystemService(Context.LOCATION_SERVICE);
return lm.isProviderEnabled(LocationManager.GPS_PROVIDER);
}
在manifest文件中的权限下:
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
我使用下面的代码来检查:
public static boolean isLocationEnabled(Context context) {
int locationMode = 0;
String locationProviders;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT){
try {
locationMode = Settings.Secure.getInt(context.getContentResolver(), Settings.Secure.LOCATION_MODE);
} catch (SettingNotFoundException e) {
e.printStackTrace();
return false;
}
return locationMode != Settings.Secure.LOCATION_MODE_OFF;
}else{
locationProviders = Settings.Secure.getString(context.getContentResolver(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
return !TextUtils.isEmpty(locationProviders);
}
}
我使用第一个代码开始创建方法isLocationEnabled
private LocationManager locationManager ;
protected boolean isLocationEnabled(){
String le = Context.LOCATION_SERVICE;
locationManager = (LocationManager) getSystemService(le);
if(!locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)){
return false;
} else {
return true;
}
}
和我检查条件,如果true打开地图和false给意图ACTION_LOCATION_SOURCE_SETTINGS
if (isLocationEnabled()) {
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
locationClient = getFusedLocationProviderClient(this);
locationClient.getLastLocation()
.addOnSuccessListener(new OnSuccessListener<Location>() {
@Override
public void onSuccess(Location location) {
// GPS location can be null if GPS is switched off
if (location != null) {
onLocationChanged(location);
Log.e("location", String.valueOf(location.getLongitude()));
}
}
})
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Log.e("MapDemoActivity", e.toString());
e.printStackTrace();
}
});
startLocationUpdates();
}
else {
new AlertDialog.Builder(this)
.setTitle("Please activate location")
.setMessage("Click ok to goto settings else exit.")
.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivity(intent);
}
})
.setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
System.exit(0);
}
})
.show();
}
LocationManager lm = (LocationManager)this.getSystemService(Context.LOCATION_SERVICE);
boolean gps_enabled = false;
boolean network_enabled = false;
try {
gps_enabled = lm.isProviderEnabled(LocationManager.GPS_PROVIDER);
} catch(Exception e){
e.printStackTrace();
}
try {
network_enabled = lm.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
} catch(Exception e){
e.printStackTrace();
}
if(!gps_enabled && !network_enabled) {
// notify user
new AlertDialog.Builder(this)
.setMessage("Please turn on Location to continue")
.setPositiveButton("Open Location Settings", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface paramDialogInterface, int paramInt) {
startActivity(new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS));
}
}).
setNegativeButton("Cancel",null)
.show();
}