android检测是否开启定位服务,如何检查是否启用了位置服务?
您可以使用以下代码检查是否启用了gps提供程序和网络提供程序。LocationManagerlm=(LocationManager)context.getSystemService(Context.LOCATION_SERVICE);booleangps_enabled=false;booleannetwork_enabled=false;try{gps_enabled=l...
您可以使用以下代码检查是否启用了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(); }
在清单文件中,您需要添加以下权限
更多推荐
所有评论(0)