为了提示用户启用GPS_PROVIDER和/或NETWORK_PROVIDER,您可以采用以下方法:
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
private void checkLocationProviders() {
LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
boolean isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
boolean isNetworkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
if (!isGPSEnabled && !isNetworkEnabled) {
showLocationSettingsDialog();
}
}
private void showLocationSettingsDialog() {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Location Settings");
builder.setMessage("Please enable GPS_PROVIDER and/or NETWORK_PROVIDER to use this feature.");
builder.setPositiveButton("Settings", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivity(intent);
}
});
builder.setNegativeButton("Cancel", null);
builder.show();
}
checkLocationProviders()
方法。checkLocationProviders();
通过这种方式,当用户未启用GPS_PROVIDER和/或NETWORK_PROVIDER时,将会提示用户进入设置页面进行启用。
DBTalk
云+社区技术沙龙[第28期]
腾讯云GAME-TECH沙龙
企业创新在线学堂
云+社区技术沙龙[第14期]
Elastic 中国开发者大会
DBTalk
云+社区开发者大会 长沙站
DB TALK 技术分享会
领取专属 10元无门槛券
手把手带您无忧上云