应用程序请求未显示 [英] App requests aren't showing up

查看:113
本文介绍了应用程序请求未显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用中有地图活动。我正在尝试请求它的位置权限,但请求权限没有显示。我不知道为什么......

I have a map activity in my app. I'm trying to request location permissions on it, but the request permissions are not showing up. I have no idea why...

这是我的完整代码:

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


 }

最后,吐司来了这里2出现。我想这是因为请求没有显示出来。
除了这些行之外,我是否需要在清单中写一些东西?:

In the end, the toast "came here 2 showing up". I guess it is because the requests are not showing up. Do I need to write something in the manifest except of these line?:

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

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

Maybe I need to write something to use these permissions?

再看一下logcat,我发现这个日志 I / Choreographer:跳过了46帧!应用程序可能在其主线程上做了太多工作。

After looking again in the logcat, I've found this log I/Choreographer: Skipped 46 frames! The application may be doing too much work on its main thread.

这是什么意思?

推荐答案

在这个问题上花了好几天之后,我发现请求权限没有显示的原因是因为TabHost。

After spending days about this issue, I found out that the reason that the request permissions are not showing up is because of the TabHost.

我试图从一个Map活动中请求权限,这是MainActivity中TabHost的一部分。现在我明白不可能从TabHost中的活动请求权限。

I've tried to request permissions from a Map Activity which was a part of TabHost in a MainActivity. Now I understood that it is impossible requesting permissions from an activity in TabHost.

在我将请求权限代码从MapActivity移动到MainActivity之后,它运行了。

After I've moved the request permissions code from the MapActivity to the MainActivity, It worked.

这篇关于应用程序请求未显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆