Java 如何在Android Studio中申请位置许可?
我尝试过很多代码和解决方案,但都没有成功。我想请求用户的许可并获得弹出窗口。即使我使用了代码,窗口也不会显示?我认为代码可以更改,但我不知道确切的位置。另外,我是Android Studio的新手Java 如何在Android Studio中申请位置许可?,java,android,google-maps,permissions,location,Java,Android,Google Maps,Permissions,Location,我尝试过很多代码和解决方案,但都没有成功。我想请求用户的许可并获得弹出窗口。即使我使用了代码,窗口也不会显示?我认为代码可以更改,但我不知道确切的位置。另外,我是Android Studio的新手 公共类MainActivity扩展了AppCompatActivity在MapreadyCallback上实现的AppCompatActivity{ 私有谷歌地图; 私有LatLngBounds PODGORICA=新LatLngBounds( 新LatLng(42.400334,19.227705
公共类MainActivity扩展了AppCompatActivity在MapreadyCallback上实现的AppCompatActivity{
私有谷歌地图;
私有LatLngBounds PODGORICA=新LatLngBounds(
新LatLng(42.400334,19.227705),新LatLng(42.470269,19.323107));
私人抽屉布局mDrawerLayout;
私人行动bardrawertoggle mtogle;
私有静态最终字符串TAG=“MapActivity”;
地点经理地点经理;
字符串提供者;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mDrawerLayout=(抽屉布局)findViewById(R.id.DroperLayout);
mToggle=newactionBarDrawerToggle(this,mDrawerLayout,R.string.open,R.string.close);
mDrawerLayout.addDrawerListener(MTOGLE);
mToggle.syncState();
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
//获取SupportMapFragment,并在地图准备好使用时收到通知。
SupportMapFragment mapFragment=(SupportMapFragment)getSupportFragmentManager().findFragmentById(R.id.map);
getMapAsync(这个);
按钮btnQR=(按钮)findViewById(R.id.skener);
btnQR.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
意向意向=新意向(MainActivity.this、Main2Activity.class);
星触觉(意向);
}
});
if(ActivityCompat.checkSelfPermission(this,Manifest.permission.ACCESS\u FINE\u LOCATION)!=PackageManager.permission\u已授予和&ActivityCompat.checkSelfPermission(this,Manifest.permission.ACCESS\u LOCATION)!=PackageManager.permission\u已授予){
if(Build.VERSION.SDK\u INT>=Build.VERSION\u code.M){
requestPermissions(新字符串[]{Manifest.permission.ACCESS\u rough\u LOCATION,Manifest.permission.ACCESS\u FINE\u LOCATION,Manifest.permission.INTERNET}
,10);
}
返回;
}
}
@凌驾
公共布尔值onOptionsItemSelected(菜单项项){
如果(MTOGLE.onOptionsItemSelected(项目)){
返回true;
}
返回super.onOptionsItemSelected(项目);
}
@凌驾
4月1日公开作废(谷歌地图谷歌地图){
mMap=谷歌地图;
mMap.Setlatlngbundsforcameratatarget(PODGORICA);
mMap.setMinZoomPreference(5.0f);
mMap.setMaxZoomPreference(25.0f);
CameraPosition CameraPosition=新建CameraPosition.Builder()
.目标(新LatLng(42.441332,19.262953))
.zoom(15)
.轴承(0)
//.倾斜(45)
.build();
mMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
//mMap.setMyLocationEnabled(真);
}
我甚至尝试了谷歌官方页面上的代码,但得到了相同的结果…从API级别23(Android 6.0棉花糖)我们需要向用户端请求运行时权限。您可以分两步完成
if (ContextCompat.checkSelfPermission(thisActivity,
Manifest.permission.READ_CONTACTS)
!= PackageManager.PERMISSION_GRANTED) {
// Permission is not granted
// Should we show an explanation?
if (ActivityCompat.shouldShowRequestPermissionRationale(thisActivity,
Manifest.permission.ACCESS_FINE_LOCATION)) {
} else {
ActivityCompat.requestPermissions(thisActivity,
new String[]{Manifest.permission.ACCESS_COARSE_LOCATION});
}
} else {
// Permission has already been granted
}
1) 在AndroidManifest.xml
文件中添加这两个权限
例如:
<manifest xlmns:android...>
...
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<application ...
</manifest>
检查这不是新api版本的正确解决方案良好编辑:)将在您更改READ\u联系人后更新您的答案
if ( Build.VERSION.SDK_INT >= 23){
if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) !=
PackageManager.PERMISSION_GRANTED ){
requestPermissions(new String[]{
android.Manifest.permission.ACCESS_FINE_LOCATION},
REQUEST_CODE_ASK_PERMISSIONS);
return ;
}
}
getLocation();
}
//get access to location permission
final private int REQUEST_CODE_ASK_PERMISSIONS = 123;
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
switch (requestCode) {
case REQUEST_CODE_ASK_PERMISSIONS:
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
getLocation();
} else {
// Permission Denied
Toast.makeText( this,"your message" , Toast.LENGTH_SHORT)
.show();
}
break;
default:
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
}
//Get location
public void getLocation(){
LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
Location myLocation=locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (myLocation == null)
{
myLocation = locationManager.getLastKnownLocation(LocationManager.PASSIVE_PROVIDER);
}
}