【问题标题】:requestPermission in Fragment not show dialog片段中的请求权限不显示对话框
【发布时间】:2018-03-19 07:41:54
【问题描述】:

我有一个带有 ImageView 的片段。 当用户单击此图像时,我想请求本地化权限,但是尽管执行了“requestPermission()”,但未显示请求对话框。

我的片段 onCreateView 方法

public View onCreateView(LayoutInflater inflater, ViewGroup container,
                            Bundle savedInstanceState) {

    View center = inflater.inflate(R.layout.board, container, false);
    position = (ImageView) center.findViewById(R.id.position);

    position.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {

                if (ContextCompat.checkSelfPermission(getActivity(),
                        Manifest.permission.ACCESS_FINE_LOCATION)
                        != PackageManager.PERMISSION_GRANTED) {

                    if (ActivityCompat.shouldShowRequestPermissionRationale(getActivity(),
                            Manifest.permission.ACCESS_FINE_LOCATION)) {

                        Toast.makeText(getContext(),
                                "I need the permission pls",
                                Toast.LENGTH_LONG).show();
                    }

                    requestPermissions(new String[]{Manifest.permission.ACCESS_FINE_LOCATION,}, LOCALIZATION_PERMISSION);

                }
            }
        }
    });
    return center;
}

注意 :: shouldShowRequestPermissionRationale 永远不会是真的。我用调试器验证了这一点。

我也在 Manifest 中管理了权限

<?xml version="1.0" encoding="utf-8"?>

    <manifest
        xmlns:android="http://schemas.android.com/apk/res/android"
        package="it.peoople">

    <uses-permissions android:name="android.permissions.INTERNET" />
    <uses-permissions android:name="android.permissions.READ_EXTERNAL_STORAGE"/>
    <uses-permissions android:name="android.permissions.WRITE_EXTERNAL_STORAGE" />
    <uses-permissions android:name="android.permission.ACCESS_FINE_LOCATION"/>
    <uses-permissions android:name="android.permission.ACCESS_COARSE_LOCATION"/>

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity
            android:name="it.peoople.main.MainActivity"
            android:label="@string/app_name"
            android:screenOrientation="portrait"
            android:windowSoftInputMode="stateHidden|adjustPan" >

            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <activity android:name="it.peoople.floating.Messaging"
            android:screenOrientation="portrait">
        </activity>

        <activity android:name="it.peoople.floating.Registration"
            android:screenOrientation="portrait">
        </activity>
    </application>

</manifest>

注意 :: 权限对话框从未出现过,所以我从不勾选“不再询问”。

每次请求权限执行都会自动拒绝,Android logcat 会打印这一行

10-09 18:22:50.019 2223-2359/it.peoople D/AppTracker: App Event: stop
10-09 18:22:50.020 2223-2254/it.peoople V/FA: Recording user engagement, ms: 5411
10-09 18:22:50.020 2223-2254/it.peoople V/FA: Using measurement service
10-09 18:22:50.020 2223-2254/it.peoople V/FA: Connecting to remote service
10-09 18:22:50.025 2223-2254/it.peoople V/FA: Activity paused, time: 57774748
10-09 18:22:50.030 2223-2254/it.peoople D/FA: Logging event (FE): user_engagement(_e), Bundle[{firebase_event_origin(_o)=auto, engagement_time_msec(_et)=5411, firebase_screen_class(_sc)=Registration, firebase_screen_id(_si)=-1569558894460098554}]
10-09 18:22:50.043 2223-2254/it.peoople V/FA: Using measurement service
10-09 18:22:50.043 2223-2254/it.peoople V/FA: Connection attempt already in progress
10-09 18:22:50.050 2223-2254/it.peoople D/FA: Connected to remote service
10-09 18:22:50.050 2223-2254/it.peoople V/FA: Processing queued up service tasks: 2
10-09 18:22:50.082 2223-2254/it.peoople V/FA: Activity resumed, time: 57774811
10-09 18:22:50.082 2223-2518/it.peoople D/AppTracker: App Event: start

请帮帮我。

真的很感谢。

【问题讨论】:

  • 你的目标sdk版本是什么?
  • 目标 SDK 25,minSDK 21
  • 和测试设备的构建版本?它对我来说工作正常
  • 我的代码适合你吗?我在 OnePlus3 7.1.1 上测试
  • 是的,同样的代码对我来说工作正常。

标签: android android-permissions android-gps


【解决方案1】:

我认为您应该将其移至 Activity 并实现一个新的侦听器以从 Fragment 与它进行交互。

您的问题是您从不执行刚刚构建的 toast,而是在其上调用 .show()。

但您要查找的对话框不是 android 的内置功能,您需要自己构建它,如下所示:

        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setMessage(R.string.permission_prompt_location);
        builder.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                ActivityCompat.requestPermissions(
                        SwipeActivity.this,
                        new String[]{Manifest.permission.ACCESS_FINE_LOCATION,
                                Manifest.permission.ACCESS_COARSE_LOCATION},
                        REQUEST_LOCATION);
            }
        });
        builder.create().show();

【讨论】:

  • 我忘记了 Toast 的 .show,但我验证了调试器的 shouldShowRequestPermissionRationale 永远不会为真。
  • 这是真机还是模拟机?
  • 是真机
  • 尝试从应用设置中打开位置权限,看看 if 语句是否会改变它的行为。也尝试注释掉sdk版本的if语句
  • 伙计,您的第二个 if 语句中的“else”在哪里?帮个忙,试试我在这个答案中写的代码,它对我有用
【解决方案2】:
猜你喜欢
  • 1970-01-01
  • 2017-05-06
  • 1970-01-01
  • 2018-05-30
  • 1970-01-01
  • 2021-10-24
  • 1970-01-01
  • 1970-01-01
  • 2021-11-20
相关资源
最近更新 更多