我正在开发一个应用程序在Android操作系统。我不知道如何检查位置服务是否启用。
我需要一个方法,如果它们被启用,返回“true”,如果没有“false”(所以在最后一种情况下,我可以显示一个对话框来启用它们)。
我正在开发一个应用程序在Android操作系统。我不知道如何检查位置服务是否启用。
我需要一个方法,如果它们被启用,返回“true”,如果没有“false”(所以在最后一种情况下,我可以显示一个对话框来启用它们)。
当前回答
迁移到AndroidX并使用
implementation 'androidx.appcompat:appcompat:1.3.0'
并使用LocationManagerCompat
在Java中
private boolean isLocationEnabled(Context context) {
LocationManager locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
return LocationManagerCompat.isLocationEnabled(locationManager);
}
在Kotlin
private fun isLocationEnabled(context: Context): Boolean {
val locationManager = context.getSystemService(Context.LOCATION_SERVICE) as LocationManager
return LocationManagerCompat.isLocationEnabled(locationManager)
}
其他回答
这是一个非常有用的方法,如果启用了Location服务,它将返回“true”:
public static boolean locationServicesEnabled(Context context) {
LocationManager lm = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
boolean gps_enabled = false;
boolean net_enabled = false;
try {
gps_enabled = lm.isProviderEnabled(LocationManager.GPS_PROVIDER);
} catch (Exception ex) {
Log.e(TAG,"Exception gps_enabled");
}
try {
net_enabled = lm.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
} catch (Exception ex) {
Log.e(TAG,"Exception network_enabled");
}
return gps_enabled || net_enabled;
}
您可以使用此代码引导用户到设置,在那里他们可以启用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 lm = (LocationManager)context.getSystemService(Context.LOCATION_SERVICE);
boolean gps_enabled = false;
boolean network_enabled = false;
try {
gps_enabled = lm.isProviderEnabled(LocationManager.GPS_PROVIDER);
} catch(Exception ex) {}
try {
network_enabled = lm.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
} catch(Exception ex) {}
if(!gps_enabled && !network_enabled) {
// notify user
new AlertDialog.Builder(context)
.setMessage(R.string.gps_network_not_enabled)
.setPositiveButton(R.string.open_location_settings, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface paramDialogInterface, int paramInt) {
context.startActivity(new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS));
}
})
.setNegativeButton(R.string.Cancel,null)
.show();
}
在清单文件中,您需要添加以下权限
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
我使用这样的方式NETWORK_PROVIDER,但你可以添加和GPS。
LocationManager locationManager;
在onCreate中
isLocationEnabled();
if(!isLocationEnabled()) {
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setTitle(R.string.network_not_enabled)
.setMessage(R.string.open_location_settings)
.setPositiveButton(R.string.yes,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
startActivity(new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS));
}
})
.setNegativeButton(R.string.cancel,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
AlertDialog alert = builder.create();
alert.show();
}
检查方法
protected boolean isLocationEnabled(){
String le = Context.LOCATION_SERVICE;
locationManager = (LocationManager) getSystemService(le);
if(!locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)){
return false;
} else {
return true;
}
}
kotlin的
private fun isLocationEnabled(mContext: Context): Boolean {
val lm = mContext.getSystemService(Context.LOCATION_SERVICE) as LocationManager
return lm.isProviderEnabled(LocationManager.GPS_PROVIDER) || lm.isProviderEnabled(
LocationManager.NETWORK_PROVIDER)
}
对话框
private fun showLocationIsDisabledAlert() {
alert("We can't show your position because you generally disabled the location service for your device.") {
yesButton {
}
neutralPressed("Settings") {
startActivity(Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS))
}
}.show()
}
像这样调用
if (!isLocationEnabled(this.context)) {
showLocationIsDisabledAlert()
}
提示:对话框需要以下导入(android studio应该为你处理这个)
import org.jetbrains.anko.alert
import org.jetbrains.anko.noButton
在清单中,您需要以下权限
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>