Mer*_*yth 208 service android location
我正在Android OS上开发一款应用.我不知道如何检查是否启用了位置服务.
我需要一个方法,如果它们被启用则返回"true",否则返回"false"(所以在最后一种情况下我可以显示一个对话框来启用它们).
Sha*_*wal 342
您可以使用以下代码检查是否启用了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();
}
Run Code Online (Sandbox Code Playgroud)
在清单文件中,您需要添加以下权限
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
Run Code Online (Sandbox Code Playgroud)
小智 221
我使用此代码进行检查:
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);
}
}
Run Code Online (Sandbox Code Playgroud)
len*_*nik 33
您可以使用此代码将用户定向到设置,在那里他们可以启用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();
}
Run Code Online (Sandbox Code Playgroud)
May*_*rma 31
截至2019年
最新,最好和最短的方法是
public static Boolean isLocationEnabled(Context context)
{
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
// This is new method provided in API 28
LocationManager lm = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
return lm.isLocationEnabled();
} else {
// This is Deprecated in API 28
int mode = Settings.Secure.getInt(context.getContentResolver(), Settings.Secure.LOCATION_MODE,
Settings.Secure.LOCATION_MODE_OFF);
return (mode != Settings.Secure.LOCATION_MODE_OFF);
}
}
Run Code Online (Sandbox Code Playgroud)
ZaB*_*anc 15
解决上面的答案,在API 23中,您需要添加"危险"权限检查以及检查系统本身:
public static boolean isLocationServicesAvailable(Context context) {
int locationMode = 0;
String locationProviders;
boolean isAvailable = false;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT){
try {
locationMode = Settings.Secure.getInt(context.getContentResolver(), Settings.Secure.LOCATION_MODE);
} catch (Settings.SettingNotFoundException e) {
e.printStackTrace();
}
isAvailable = (locationMode != Settings.Secure.LOCATION_MODE_OFF);
} else {
locationProviders = Settings.Secure.getString(context.getContentResolver(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
isAvailable = !TextUtils.isEmpty(locationProviders);
}
boolean coarsePermissionCheck = (ContextCompat.checkSelfPermission(context, Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED);
boolean finePermissionCheck = (ContextCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED);
return isAvailable && (coarsePermissionCheck || finePermissionCheck);
}
Run Code Online (Sandbox Code Playgroud)
Sun*_*nny 10
迁移到Android X并使用
implementation 'androidx.appcompat:appcompat:1.1.0'
Run Code Online (Sandbox Code Playgroud)
并使用LocationManagerCompat
在java中
private boolean isLocationEnabled(Context context) {
LocationManager locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
return LocationManagerCompat.isLocationEnabled(locationManager);
}
Run Code Online (Sandbox Code Playgroud)
在科特林
private fun isLocationEnabled(context: Context): Boolean {
val locationManager = context.getSystemService(Context.LOCATION_SERVICE) as LocationManager
return LocationManagerCompat.isLocationEnabled(locationManager)
}
Run Code Online (Sandbox Code Playgroud)
如果未启用提供程序,则"被动"是返回的最佳提供程序.请参阅/sf/answers/316359011/
public boolean isLocationServiceEnabled() {
LocationManager lm = (LocationManager)
this.getSystemService(Context.LOCATION_SERVICE);
String provider = lm.getBestProvider(new Criteria(), true);
return (StringUtils.isNotBlank(provider) &&
!LocationManager.PASSIVE_PROVIDER.equals(provider));
}
Run Code Online (Sandbox Code Playgroud)
是的你可以查看以下代码:
public boolean isGPSEnabled(Context mContext)
{
LocationManager lm = (LocationManager)
mContext.getSystemService(Context.LOCATION_SERVICE);
return lm.isProviderEnabled(LocationManager.GPS_PROVIDER);
}
Run Code Online (Sandbox Code Playgroud)
拥有清单文件中的权限:
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
Run Code Online (Sandbox Code Playgroud)
在 Android 8.1 或更低版本上,用户可以从 启用“省电”模式
Settings > Location > Mode > Battery Saving。
此模式仅使用WiFi, Bluetooth or mobile data而不是 GPS 来确定用户位置。
这就是为什么你必须检查网络提供商是否启用并且locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)还不够。
如果您使用androidx此代码,将检查您正在运行的 SDK 版本并调用相应的提供程序:
public boolean isLocationEnabled(Context context) {
LocationManager manager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
return manager != null && LocationManagerCompat.isLocationEnabled(manager);
}
Run Code Online (Sandbox Code Playgroud)
这个if子句容易检查我认为位置服务是否可用:
LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
if(!locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER) && !locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) {
//All location services are disabled
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
217350 次 |
| 最近记录: |