我正在为android编写应用程序。当我打开我的应用程序时首先获取异常消息。这对我来说是不同的,因为我制作了很多应用程序并且没有这样的东西。现在我正在试图获得用户的许可,毫无例外。当我在Android Studio上运行按钮时。模拟器给我带有请求权限框的错误消息。在我进入我的活动之后,我只想从用户那里获取请求权限而不会收到异常消息。通过这种方式我是如此初学者这个主题。我怎么能这样做?
我的xml:
<uses-permission android:name="android.permission.CAMERA" />
我的代码:
ActivityCompat.requestPermissions(this,
new String[]{ Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE },
PERMISSIONS_MULTIPLE_REQUEST);
答案 0 :(得分:2)
int requestCode = 100;
// Check if permission is already granted. If Granted you need not ask
if (ContextCompat
.checkSelfPermission(getActivity(),
Manifest.permission.CAMERA) != (int)PackageManager.PERMISSION_GRANTED &&
ContextCompat.checkSelfPermission(getActivity(),
Manifest.permission.WRITE_EXTERNAL_STORAGE)
!= (int)PackageManager.PERMISSION_GRANTED) {
// Check if user has opted "Never show again"
if (ActivityCompat.shouldShowRequestPermissionRationale(getActivity(), Manifest.permission.CAMERA) ||
ActivityCompat.shouldShowRequestPermissionRationale(getActivity(), Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
requestPermissions(new String[] {
Manifest.permission.CAMERA,
Manifest.permission.WRITE_EXTERNAL_STORAGE
}, requestCode);
}
} else {
// Application has the requested permission.
// Write your further logic, what's next after permission is granted
}