java - 应用程序请求未显示

标签 java android android-permissions

我的应用程序中有一个 map Activity 。我正在尝试请求其位置权限,但请求权限未显示。我不知道为什么...

这是我的完整代码:

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();


 }

最后, toast “来到这里2出现了”。我想这是因为请求没有出现。 除了这些行之外,我还需要在 list 中写一些东西吗?:

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />

也许我需要写一些东西来使用这些权限?

再次查看 logcat 后,我​​发现了此日志 I/Choreographer: Skipped 46 帧!应用程序可能在其主线程上做了太多工作。

这是什么意思?

最佳答案

花了几天时间研究这个问题,我发现请求权限没有显示的原因是 TabHost 的原因。

我尝试从 map Activity 请求权限,该 Activity 是 MainActivity 中 TabHost 的一部分。现在我明白了,不可能从 TabHost 中的 Activity 请求权限。

在我将请求权限代码从 MapActivity 移至 MainActivity 后,它起作用了。

关于java - 应用程序请求未显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36529635/

相关文章:

android - 应用程序未经许可写入外部存储

java - Choco 求解器传播和搜索策略交互

Java将文本解析为整数

java - 有没有办法使用父实体字段(而不是父对象)将子实体映射到父实体?

Android:在生成签名 apk 期间出现此错误

java - 如何以编程方式更改密码?

java - 如何更改 “Press ESC to exit fullscreen”

java - Android:直接截屏到字节 - 跳过保存到文件

java - ACTION_SENDTO Intent 在抽屉导航 fragment 中不起作用

android - 如何在android中获取第二个sim的信息