我正在尝试请求ACCESS_FINE_LOCATION权限以获取用户的当前位置.
我的日志记录表明我的应用程序在查询ContextCompat.checkSelfPermission()时当前没有此权限,但在调用ActivityCompat.requestPermissions()时,不会显示任何内容.
我的Google地图代码(实现OnMapReadyCallback和ActivityCompat.OnRequestPermissionsResultCallback())位于FragmentActivity中.
我已经设法让requestPermissions()函数在应用程序中的其他活动中成功运行,它只是带有Google地图的那个.放置在Activity的onCreate()方法或onMapReady()(它需要去的地方)时,它不起作用.
if(ContextCompat.checkSelfPermission(LocationActivity.this,android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) { Log.d(TAG,"not granted"); final String[] permissions = new String[] {android.Manifest.permission.ACCESS_FINE_LOCATION}; if(ActivityCompat.shouldShowRequestPermissionRationale(this,android.Manifest.permission.ACCESS_FINE_LOCATION)) { Log.d(TAG,"rationale"); // Explain to the user why permission is required,then request again AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setMessage("We need permissions") .setCancelable(false) .setPositiveButton("OK",new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog,int id) { ActivityCompat.requestPermissions(LocationActivity.this,permissions,1); } }); AlertDialog alert = builder.create(); alert.show(); } else { Log.d(TAG,"request" + android.Manifest.permission.ACCESS_FINE_LOCATION); // If permission has not been denied before,request the permission ActivityCompat.requestPermissions(LocationActivity.this,1); } } else { Log.d(TAG,"granted"); }
有任何想法吗?它与我的Activity类(FragmentActivity)有关,还是可能是异步调用权限请求的Google地图?