2017-10-07 63 views
0

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

我的片段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是不正确的。我使用调试器验证了这一点。

我在清单管理的权限也

<?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 

请帮助我。

真的很感谢。

+0

你的目标sdk版本是什么? – nomag

+0

target SDK 25,minSDK 21 –

+0

和Build版本的测试设备?它的工作对我来说很好 – nomag

回答

0

我认为你应该将其移动到活动并实现一个新的监听器来与片段交互。

你的问题是,你永远不会执行你刚刚构建它的敬酒,但nevet调用.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(); 
+0

我忘了显示Toast,但我确认shouldShowRequestPermissionRationale永远不会出现在调试器中。 –

+0

这是一个真实的设备或模拟? –

+0

是一个真实的设备 –