![](https://s5-media.51cto.com/ost/pc/static/noavatar.gif)
回复
1.在项目根目录下的 build.gradle
文件中,
// 添加maven仓库
repositories {
maven {
url 'https://s01.oss.sonatype.org/content/repositories/release/'
}
}
2.在entry模块的 build.gradle
文件中,
// 添加依赖库
dependencies {
implementation 'com.gitee.chinasoft_ohos:easypermissions:1.0.0'
}
在sdk6,DevEco Studio2.2 beta1下项目可直接运行 如无法运行,删除项目.gradle,.idea,build,gradle, build.gradle文件, 并依据自己的版本创建新项目,将新项目的对应文件复制到根目录下
public class MainAbility extends Ability {
MainAbilitySlice abilitySlice;
@Override
public void onStart(Intent intent) {
super.onStart(intent);
super.setMainRoute(MainAbilitySlice.class.getName());
}
...
@Override
public void onRequestPermissionsFromUserResult(int requestCode, String[] permissions, int[] grantResults) {
super.onRequestPermissionsFromUserResult(requestCode, permissions, grantResults);
//传递到AbilitySlice里面,onRequestPermissionsResult(可以自定义)
abilitySlice.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
...
}
//在AbilitySlice里面添加
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
EasyPermissions.onRequestPermissionsResult(requestCode, permissions, grantResults, this);
}
权限请求
下面的示例显示了如何为需要这两者的方法请求权限“相机”和“定位”的权限。有几件事需要注意:
使用EasyPermissions.hasPermissions()检查应用程序是否已经有所需的权限。这个方法可以接受任意数量的权限作为参数。
使用EasyPermissions.requestPermissions()请求权限,调出出用户授权界面。
用户授权回调
/**
*perms String[] 请求的权限
*/
if (EasyPermissions.somePermissionPermanentlyDenied(mAbility, perms)) {
PermissionDialog dialog = new PermissionDialog(mContext, requestCode, str);
dialog.setTitle("弹窗标题");
dialog.setContent("弹窗内容");
//弹窗的确定和取消的点击shi'jian2
dialog.setOnClickListener(new PermissionDialog.OnClickListener() {
@Override
public void ok(IDialog myDialog) {
//点击确定 跳转到设置页面
Intent intent = new Intent();
intent.setAction(IntentConstants.ACTION_APPLICATION_DETAILS_SETTINGS);
intent.setUri(Uri.parse("package:" + mContext.getBundleName()));
mContext.startAbility(intent, REQUEST_SETTING);
}
@Override
public void cancel(IDialog myDialog) {
//点击取消
}
});
dialog.show();
}
CodeCheck代码测试无异常
CloudTest代码测试无异常
病毒安全检测通过
当前版本demo功能与原组件基本无差异
Copyright 2017 Google
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.