我有一个带有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>
注意::权限对话框从未出现,所以我从不检查“不要再问”#34;。
每次执行请求权限都会被自动拒绝,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 :(得分:0)
我认为你应该将它移到活动中并实现一个新的监听器来从片段中与它进行交互。
你的问题是,你永远不会执行你刚刚建立它的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();
答案 1 :(得分:0)