我正在尝试在Android上获取相机和微权限。我在codenameone_library_required.properties文件中添加了:
android.xpermissions=<uses-permission android:name="android.permission.CAMERA" android:required="true"/> <uses-permission android:name="android.permission.RECORD_AUDIO" android:required="true"/> <uses-permission android:name="android.permission.INTERNET" android:required="true"/>
适用于旧版Android,但不适用于Marshmallow。因此,我在代号上阅读了一个开发人员指南,在后来的android版本中要求权限我必须以sdk 23为目标。的确,我在构建提示中添加了:
android.targetSDKVersion=23
但不幸的是,这还不够,当我启动应用程序时,它不会要求权限。
我应该添加一些东西来询问权限吗?也许&#34; Display.getInstance()。setProperty(...)&#34; ?
提前谢谢!
答案 0 :(得分:1)
如果我真的理解你的问题,你应该在运行时(android Marshmallow及以上版本)请求相机权限:
//to check if the user grant the permission before
if(ActivityCompat.checkSelfPermission(getActivity().getApplicationContext(), Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
//if it is not the case, request new permission
ActivityCompat.requestPermissions(getActivity(), new String[]{Manifest.permission.CAMERA},requestCode);
return;
}
//your camera code is here
在Codename One的情况下,正如@mina所说,你无法找到ActivityCompat类,所以试试这段代码:
if(!com.codename1.impl.android.AndroidNativeUtil.checkForPermission(Manifest.permission.READ_PHONE_STATE, "This should be the description shown to the user...")){
// you didn't get the permission, you might want to return here
}
// you have the permission, do what you need