您可以使用以下代码检查是否启用了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();    }

在清单文件中,您需要添加以下权限

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐