Java 请求权限错误
我正在做一个地图活动。在这个地图活动中,我想显示一个“当前位置”按钮,所以要显示这个按钮,我需要请求位置权限 由于某些原因,我试图请求的所有请求权限都没有显示 请求窗口甚至没有出现。有什么问题吗 这是我的代码:Java 请求权限错误,java,android,permissions,android-permissions,Java,Android,Permissions,Android Permissions,我正在做一个地图活动。在这个地图活动中,我想显示一个“当前位置”按钮,所以要显示这个按钮,我需要请求位置权限 由于某些原因,我试图请求的所有请求权限都没有显示 请求窗口甚至没有出现。有什么问题吗 这是我的代码: public class MapsActivity extends FragmentActivity implements OnMapReadyCallback { private static final String FIREBASE_URL="https://****.fireba
public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {
private static final String FIREBASE_URL="https://****.firebaseio.com/";
private Firebase firebaseRef;
private LocationManager locationManager;
private GoogleMap mMap;
SupportMapFragment mapFrag;
boolean bPermissionGranted;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps2);
Firebase.setAndroidContext(this);
firebaseRef = new Firebase(FIREBASE_URL);
// Obtain the SupportMapFragment and get notified when the map is ready to be used.
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
setUpMapIfNeeded();
}
private void setUpMapIfNeeded() {
if (mMap == null) {
// Try to obtain the map from the SupportMapFragment.
mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map))
.getMap();
if (mMap != null) {
CameraUpdate center = CameraUpdateFactory.newLatLng(new LatLng(32.065483, 34.824550));
CameraUpdate zoom = CameraUpdateFactory.zoomTo(10);
mMap.moveCamera(center);
mMap.animateCamera(zoom);
}
}
}
public static final int MY_PERMISSIONS_REQUEST_LOCATION = 99;
public void checkLocationPermission() {
if (ContextCompat.checkSelfPermission(this,
Manifest.permission.ACCESS_FINE_LOCATION)
!= PackageManager.PERMISSION_GRANTED) {
// Should we show an explanation?
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (shouldShowRequestPermissionRationale(Manifest.permission.ACCESS_FINE_LOCATION)) {
showMessageOKCancel("You need to accept location permissions for using these services!,
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
{
Toast.makeText(MapsActivity.this, "came here 1", Toast.LENGTH_SHORT).show();
requestPermissions(new String[]{Manifest.permission.WRITE_CONTACTS},
MY_PERMISSIONS_REQUEST_LOCATION);
}
}
});
return;
}
requestPermissions(new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
MY_PERMISSIONS_REQUEST_LOCATION);
Toast.makeText(MapsActivity.this, "came here 2", Toast.LENGTH_SHORT).show();
return;
}
else
{
mMap.setMyLocationEnabled(true);
Toast.makeText(MapsActivity.this, "came here 3", Toast.LENGTH_SHORT).show();
}
}
}
private void showMessageOKCancel(String message, DialogInterface.OnClickListener okListener) {
new AlertDialog.Builder(MapsActivity.this)
.setMessage(message)
.setPositiveButton("OK", okListener)
.setNegativeButton("Cancel", null)
.create()
.show();
}
@Override
public void onRequestPermissionsResult(int requestCode,
String permissions[], int[] grantResults) {
switch (requestCode) {
case MY_PERMISSIONS_REQUEST_LOCATION: {
// If request is cancelled, the result arrays are empty.
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// permission was granted, yay!
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED &&
ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
mMap.setMyLocationEnabled(true);
}
} else {
// permission denied, boo! Disable the
// functionality that depends on this permission.
Toast.makeText(this, "Permission Denied", Toast.LENGTH_LONG).show();
}
return;
}
}
}
@Override
public void onMapReady(final GoogleMap googleMap) {
mMap=googleMap;
mMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
checkLocationPermission();
}
我需要在舱单上写点什么吗?除了这些行:
在我的日志中,我发现:
E/linker:readlink('/proc/self/fd/22')失败:权限被拒绝[fd=22]
E/linker:warning:无法获取库的realpath”/data/data/com.tranzmate/app_dx/v1/Generated_-470173342.dex”。将使用给定的名称。
这也出现了:
GoogleService未能初始化,状态:10,缺少用于初始化google服务的预期资源:“R.string.google\u app\u id”。可能的原因是缺少google-services.json或com.google.gms.google-services gradle插件。
04-11 00:18:20.228 15674-15674/? E/GMPM:未设置计划程序。未记录错误/警告。
04-11 00:18:20.254 15674-15692/? E/GMPM:无法上载。应用程序测量已禁用
也许是gradle档案里的东西
或者可能是SDK中需要安装的东西?必须签入清单文件,您可以在其中调用此权限no add 无历史=真实
必须签入您的清单文件,您将此权限称为“无添加” 无历史=真实
@BobMalooga我知道,正因为如此,在我使用bew权限请求系统之前,我会检查Android版本。如果低于6.0,它将使用旧的方法。@DanielNugent如果我删除这一行,应用程序将崩溃。请看我上面添加的日志行。@Bobmaloga我知道,正因为如此,在我使用bew权限请求系统之前,我会检查Android版本。如果低于6.0,它将使用旧的方法。@DanielNugent如果我删除这一行,应用程序将崩溃。请看我上面添加的日志行。