我有一个需要 CAMERA 和 WRITE_SETTINGS 权限的手电筒,我需要先处理这些我已经知道如何操作的权限,然后如果它被授予,例如,开始我的主要活动......(对不起,我是新手安卓)
问问题
2378 次
2 回答
6
在你的 gradle 文件中从github添加这个库
比添加此代码
PermissionListener permissionlistener = new PermissionListener() {
@Override
public void onPermissionGranted() {
//The user have conceded permission
Toast.makeText(MainActivity.this, "Permission Granted", Toast.LENGTH_SHORT).show();
}
@Override
public void onPermissionDenied(ArrayList<String> deniedPermissions) {
//close the app or do whatever you want
Toast.makeText(MainActivity.this, "Permission Denied\n" + deniedPermissions.toString(), Toast.LENGTH_SHORT).show();
}
};
new TedPermission(this)
.setPermissionListener(permissionlistener)
.setDeniedMessage("If you reject permission,you can not use this service\n\nPlease turn on permissions at [Setting] > [Permission]")
.setPermissions(Manifest.permission.READ_CONTACTS, Manifest.permission.ACCESS_FINE_LOCATION)
.check();
或者,如果您更喜欢在本地进行,请查看 google文档
于 2016-07-13T21:21:47.970 回答
1
在您的清单文件中添加权限:
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.CALL_PHONE" />
在“ onCreate ”内的主要活动java文件中添加以下代码
String[] PERMISSIONS = new String[]{
Manifest.permission.ACCESS_FINE_LOCATION,
Manifest.permission.CALL_PHONE};
if (
(ContextCompat.checkSelfPermission(MapsActivity.this, PERMISSIONS[0]) != PackageManager.PERMISSION_GRANTED) ||
(ContextCompat.checkSelfPermission(MapsActivity.this, PERMISSIONS[1]) != PackageManager.PERMISSION_GRANTED)
)
{
ActivityCompat.requestPermissions(MapsActivity.this, PERMISSIONS, REQUEST_INTERNET);
finish();
}
于 2018-09-20T12:23:31.210 回答