以下代码检查是否启用了位置.如果未启用,则显示警报对话框.
LocationManager lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE); 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){ AlertDialog.Builder dialog = new AlertDialog.Builder(this); dialog.setMessage(getResources().getString(R.string.gps_network_not_enabled)); dialog.setPositiveButton(getResources().getString(R.string.open_location_settings), new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface paramDialogInterface, int paramInt) { Intent myIntent = new Intent( Settings.ACTION_LOCATION_SOURCE_SETTINGS); Startup.this.startActivity(myIntent); } }); dialog.setNegativeButton(getString(R.string.Cancel), new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface paramDialogInterface, int paramInt) { // TODO Auto-generated method stub } }); dialog.show(); }