【问题标题】:Allowing or deny a permission crash the app android允许或拒绝权限会使应用程序 android 崩溃
【发布时间】:2016-03-25 16:28:24
【问题描述】:

我正在尝试为 Android 应用实施最新的权限系统。我触发按钮时弹出对话框(这是预期的行为),但只要我点击允许或拒绝,应用程序就会崩溃,但看起来权限被正确授予或拒绝(取决于点击的按钮)。

这是 onClick 方法:

public void okGetItLocation(View view){

    Log.d(TAG, "BTN FIRED");
    // Should we show an explanation?
    if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.ACCESS_FINE_LOCATION)) {

        Log.d(TAG, "WE HAVE TO SHOW AN EXPLANATION");
        // Create a LocationPermissionError fragment
        NoLocationPermissionsFragment noLocationPermissionsFragment = new NoLocationPermissionsFragment();
        getSupportFragmentManager().beginTransaction()
                .replace(R.id.fragment_container, noLocationPermissionsFragment)
                .commit();

        // Show an expanation to the user *asynchronously* -- don't block
        // this thread waiting for the user's response! After the user
        // sees the explanation, try again to request the permission.

    }else {

        Log.d(TAG, "POPUP SHOWN");

        // No explanation needed, we can request the permission.

        ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, REQUEST_CODE_ASK_PERMISSIONS);

        // REQUEST_CODE_ASK_PERMISSIONS is an
        // app-defined int constant. The callback method gets the
        // result of the request.
    }
}

这是获得用户选择的方法:

@Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
    switch (requestCode) {
        case REQUEST_CODE_ASK_PERMISSIONS: {
            // If request is cancelled, the result arrays are empty.
            if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {

                Log.d(TAG, "PERMISSIONS GRANTED");

                // permission was granted, yay! Do the
                // contacts-related task you need to do.

            } else {

                Log.d(TAG, "PERMISSIONS DENIED");

                // Create a LocationPermissionError fragment
                NoLocationPermissionsFragment noLocationPermissionsFragment = new NoLocationPermissionsFragment();
                getSupportFragmentManager().beginTransaction()
                        .replace(R.id.fragment_container, noLocationPermissionsFragment)
                        .commit();

                // permission denied, boo! Disable the
                // functionality that depends on this permission.
            }
            return;
        }

        // other 'case' lines to check for other
        // permissions this app might request
    }
}

我的日志:

03-25 12:19:38.673 2944-2944/com.danajeremy.egullapireading D/FIRSTACTIVITY: BTN FIRED
03-25 12:19:38.673 2944-2944/com.danajeremy.egullapireading D/FIRSTACTIVITY: POPUP SHOWN
03-25 12:19:39.375 2944-2963/com.danajeremy.egullapireading E/Surface: getSlotFromBufferLocked: unknown buffer: 0xeb16d800

【问题讨论】:

  • 给你,刚刚添加了它
  • 这不是 Java 堆栈跟踪。如果您的应用程序崩溃,将会有一个堆栈跟踪。
  • 即使在详细日志级别,这些行之后也没有打印堆栈跟踪。
  • 没人知道该怎么做?

标签: android android-permissions android-6.0-marshmallow


【解决方案1】:

对于那些想知道的人,这是因为我在我的 MainActivity(在 Manifest 中)添加了 NOHISTORY。现在它就像一个魅力。

【讨论】:

  • 一点点 OverKill - 工作但不符合大多数应用程序:NoHistory 指定是否应将活动保留在其历史堆栈中。如果设置了这个属性,那么一旦用户离开活动,活动就会结束,他们将无法再返回。
猜你喜欢
  • 2016-08-10
  • 2017-01-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-26
  • 2017-08-07
  • 2019-06-25
  • 2013-05-12
相关资源
最近更新 更多