React Native - 无法使用PermissionsAndroid

时间:2017-03-16 18:06:44

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

我正在尝试请求运行时位置权限,但我的承诺不断抛出此异常:

“尝试使用权限API,但主机Activity未实现PermissionAwareActivity”

我的代码如下:

  PermissionsAndroid.check(PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION)
  .then((preGranted) => {
    console.log('pre-granted', preGranted)
    if (!preGranted) {
      PermissionsAndroid.request(
        PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION,
        { 'title': 'Enable location', 'message': 'Boss said so..' }
      )
      .then((granted) => {
        if (granted) {
          console.log('GRANTED AFTER ASKING:', granted)
          debugger
        }
      })
    }
  })

在我的清单中我有:

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

有人遇到过这条消息吗?什么是PermissionAwareActivity我无法在网上找到任何关于它的东西。

3 个答案:

答案 0 :(得分:1)

$.getJSON('/world.json', function (geoJSONdata) { var geoJSONgroup = L.geoJSON(geoJSONdata).addTo(map); geoJSONgroup.eachLayer(function (layer) { if (layer.feature.properties.name === "France") { // Zoom to that layer. map.fitBounds(layer.getBounds()); } }); }); 是第三方库中的界面。

https://github.com/wix/react-native-navigation

它不是标准Android SDK的一部分。

可以在文档中找到使用标准SDK请求运行时权限的说明。

https://developer.android.com/training/permissions/requesting.html

答案 1 :(得分:0)

你应该在你的活动中实现PermissionAwareActivity, 并定义Callback&amp;&amp; PermissionListener。

public class ReactNativeIndexActivity extends AppCompatActivity implements DefaultHardwareBackBtnHandler,PermissionAwareActivity {
    private ReactRootView mReactRootView;
    private ReactInstanceManager mReactInstanceManager;
    private @Nullable   Callback mPermissionsCallback;
    private @Nullable PermissionListener mPermissionListener;
 @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ......
}

  @Override
    protected void onResume() {
        super.onResume();

        if (mReactInstanceManager != null) {
            mReactInstanceManager.onHostResume(this, this);
        }
        if (mPermissionsCallback != null) {
            mPermissionsCallback.invoke();
            mPermissionsCallback = null;
        }
    }

 @Override
    @TargetApi(Build.VERSION_CODES.M)
    public void requestPermissions(String[] permissions, int requestCode, PermissionListener listener) {
        mPermissionListener = listener;
        requestPermissions(permissions, requestCode);
    }
    @Override
    public void onRequestPermissionsResult(final int requestCode, @NonNull final String[] permissions, @NonNull final int[] grantResults) {
        mPermissionsCallback = new Callback() {
            @Override
            public void invoke(Object... args) {
                if (mPermissionListener != null && mPermissionListener.onRequestPermissionsResult(requestCode, permissions, grantResults)) {
                    mPermissionListener = null;
                }
            }
        };
    }

答案 2 :(得分:-2)

注意:此答案基于Java而非Reactive native。请参阅React原生特定解决方案的其他答案

  

什么是PermissionAwareActivity

PermissionAwareActivity是一个可以帮助您获取运行时权限的界面。您必须在自己的活动中实施这些方法。

  public interface PermissionAwareActivity {

  int checkPermission(String permission, int pid, int uid);


  int checkSelfPermission(String permission);

  boolean shouldShowRequestPermissionRationale(String permission);

  void requestPermissions(String[] permissions, int requestCode, PermissionListener listener);
}