/*
* Copyright Google Inc. All Rights Reserved.
*
* 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.
*/
package pub.devrel.easypermissions;
import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Context;
import android.content.DialogInterface;
import android.content.pm.PackageManager;
import android.os.Build;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.annotation.RequiresApi;
import android.support.annotation.StringRes;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AlertDialog;
import android.util.Log;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/**
* Utility to request and check System permissions for apps targeting Android M (API >= 23).
*/
public class EasyPermissions {
private static final String TAG = "EasyPermissions";
private static final String DIALOG_TAG = "RationaleDialogFragmentCompat";
public interface PermissionCallbacks extends
ActivityCompat.OnRequestPermissionsResultCallback {
void onPermissionsGranted(int requestCode, List<String> perms);
void onPermissionsDenied(int requestCode, List<String> perms);
}
/**
* Check if the calling context has a set of permissions.
*
* @param context
* the calling context.
* @param perms
* one ore more permissions, such as {@code android.Manifest.permission.CAMERA}.
* @return true if all permissions are already granted, false if at least one permission is not yet granted.
*/
public static boolean hasPermissions(@NonNull Context context, @NonNull String... perms) {
// Always return true for SDK < M, let the system deal with the permissions
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
Log.w(TAG, "hasPermissions: API version < M, returning true by default");
return true;
}
for (String perm : perms) {
boolean hasPerm = (ContextCompat.checkSelfPermission(context, perm) ==
PackageManager.PERMISSION_GRANTED);
if (!hasPerm) {
return false;
}
}
return true;
}
/**
* Request a set of permissions, showing rationale if the system requests it.
*
* @param object
* Activity or Fragment requesting permissions. Should implement
* {@link android.support.v4.app.ActivityCompat.OnRequestPermissionsResultCallback}
* or {@code android.support.v13.app.FragmentCompat.OnRequestPermissionsResultCallback}
* @param rationale
* a message explaining why the application needs this set of permissions, will be displayed if the user rejects the request the first
* time.
* @param requestCode
* request code to track this request, must be < 256.
* @param perms
* a set of permissions to be requested.
*/
public static void requestPermissions(@NonNull final Object object, @NonNull String rationale,
final int requestCode, @NonNull final String... perms) {
requestPermissions(object, rationale,
android.R.string.ok,
android.R.string.cancel,
requestCode, perms);
}
/**
* Request a set of permissions, showing rationale if the system requests it.
*
* @param object
* Activity or Fragment requesting permissions. Should implement
* {@link android.support.v4.app.ActivityCompat.OnRequestPermissionsResultCallback}
* or {@code android.support.v13.app.FragmentCompat.OnRequestPermissionsResultCallback}
* @param rationale
* a message explaining why the application needs this set of permissions, will be displayed if the user rejects the request the first
* time.
* @param positiveButton
* custom text for positive button
* @param negativeButton
* custom text for negative button
* @param requestCode
* request code to track this request, must be < 256.
* @param perms
* a set of permissions to be requested.
*/
@SuppressLint("NewApi")
public static void requestPermissions(@NonNull final Object object, @NonNull String rationale,
@StringRes int positiveButton, @StringRes int negativeButton,
final int requestCode, @NonNull final String... perms) {
checkCallingObjectSuitability(object);
// Determine if rationale should be shown (generally when the user has previously
// denied the request);
boolean shouldShowRationale = false;
for (String perm : perms) {
shouldShowRationale =
shouldShowRationale || shouldShowRequestPermissionRationale(object, perm);
}
if (shouldShowRationale) {
// If we can get a FragmentManager, show a RationaleDialogFragmentCompat. Otherwise, show
// an AlertDialog. The Fragment has the advantage of surviving rotations.
if (getSupportFragmentManager(object) != null) {
// Show AppCompatDialogFragment
showRationaleDialogFragmentCompat(getSupportFragmentManager(object),
rationale, positiveButton, negativeButton, requestCode, perms);
} else if (getFragmentManager(object) != null) {
// Show DialogFragment
showRationaleDialogFragment(getFragmentManager(object),
rationale, positiveButton, negativeButton, requestCode, perms);
} else {
// Revert to AlertDialog
showRationaleAlertDialog(object, rationale, positiveButton, negativeButton,
requestCode, perms);
}
} else {
executePermissionsRequest(object, perms, requestCode);
}
}
/**
* Show a {@link RationaleDialogFragmentCompat} explaining permission request rationale.
*/
@RequiresApi(Build.VERSION_CODES.HONEYCOMB)
private static void showRationaleDialogFragmentCompat(
@NonNull final android.support.v4.app.FragmentManager fragmentManager,
@NonNull String rationale, @StringRes int positiveButton, @StringRes int negativeButton,
final int requestCode, @NonNull final String... perms) {
RationaleDialogFragmentCompat fragment = RationaleDialogFragmentCompat
.newInstance(positiveButton, negativeButton, rationale, requestCode, perms);
fragment.show(fragmentManager, DIALOG_TAG);
}
/**
* Show a {@link RationaleDialogFragment} explaining permission request rationale.
*/
@RequiresApi(api = Build.VERSION_CODES.HONEYCOMB)
private static void showRationaleDialogFragment(
@NonNull final android.app.FragmentManager fragmentManager,
@NonNull String rationale, @StringRes int positiveButton, @StringRes int negativeButton,
final int requestCode, @NonNull fin
没有合适的资源?快使用搜索试试~ 我知道了~
官方推荐的android 6.0 权限获取方式(实例demo:easypermissions)
共81个文件
xml:28个
bin:12个
java:10个
5星 · 超过95%的资源 需积分: 10 281 下载量 198 浏览量
2016-11-24
00:37:55
上传
评论
收藏 435KB RAR 举报
温馨提示
博客地址:http://blog.csdn.net/u013100574/article/details/53314209。官方推荐的android 6.0 权限获取方式(实例demo:easypermissions)
资源推荐
资源详情
资源评论
收起资源包目录
android 6.0 permissions.rar (81个子文件)
permissions
gradlew 5KB
settings.gradle 39B
.idea
libraries
support_media_compat_25_0_1.xml 1KB
support_v4_25_0_1.xml 444B
support_fragment_25_0_1.xml 1KB
support_compat_25_0_1.xml 1KB
support_annotations_25_0_1.xml 495B
appcompat_v7_25_0_1.xml 1KB
support_core_utils_25_0_1.xml 1KB
animated_vector_drawable_25_0_1.xml 838B
support_vector_drawable_25_0_1.xml 831B
support_core_ui_25_0_1.xml 1KB
runConfigurations.xml 564B
misc.xml 2KB
compiler.xml 686B
workspace.xml 265KB
gradle.xml 831B
modules.xml 516B
copyright
profiles_settings.xml 76B
gradle
wrapper
gradle-wrapper.properties 233B
gradle-wrapper.jar 52KB
permissions.iml 865B
sample
src
main
AndroidManifest.xml 1KB
res
mipmap-hdpi
ic_launcher.png 3KB
mipmap-xxxhdpi
ic_launcher.png 10KB
mipmap-xhdpi
ic_launcher.png 5KB
mipmap-mdpi
ic_launcher.png 2KB
layout
fragment_main.xml 378B
activity_basic.xml 508B
activity_main.xml 1KB
values-w820dp
dimens.xml 358B
values
colors.xml 208B
strings.xml 978B
styles.xml 383B
dimens.xml 211B
mipmap-xxhdpi
ic_launcher.png 8KB
java
pub
devrel
easypermissions
sample
MainFragment.java 2KB
BasicActivity.java 2KB
MainActivity.java 5KB
sample.iml 11KB
build.gradle 659B
proguard-rules.pro 641B
.gradle
2.14.1
tasks
_sample_compileReleaseJavaWithJavac
localJarClasspathSnapshot
localJarClasspathSnapshot.bin 21KB
localJarClasspathSnapshot.lock 17B
localClassSetAnalysis
localClassSetAnalysis.bin 26KB
localClassSetAnalysis.lock 17B
_sample_compileDebugJavaWithJavac
localJarClasspathSnapshot
localJarClasspathSnapshot.bin 21KB
localJarClasspathSnapshot.lock 17B
localClassSetAnalysis
localClassSetAnalysis.bin 36KB
localClassSetAnalysis.lock 17B
_app_compileDebugJavaWithJavac
localJarClasspathSnapshot
localJarClasspathSnapshot.bin 21KB
localJarClasspathSnapshot.lock 17B
localClassSetAnalysis
localClassSetAnalysis.bin 21KB
localClassSetAnalysis.lock 17B
_easypermissions_compileReleaseJavaWithJavac
localJarClasspathSnapshot
localJarClasspathSnapshot.bin 21KB
localJarClasspathSnapshot.lock 17B
localClassSetAnalysis
localClassSetAnalysis.bin 27KB
localClassSetAnalysis.lock 17B
taskArtifacts
fileSnapshotsToTreeSnapshotsIndex.bin 66KB
cache.properties.lock 17B
fileHashes.bin 571KB
taskArtifacts.bin 278KB
fileSnapshots.bin 8.37MB
cache.properties 31B
local.properties 428B
easypermissions
src
main
AndroidManifest.xml 87B
res
values
strings.xml 78B
java
pub
devrel
easypermissions
RationaleDialogFragmentCompat.java 2KB
AfterPermissionGranted.java 938B
RationaleDialogFragment.java 3KB
RationaleDialogClickListener.java 2KB
AppSettingsDialog.java 7KB
RationaleDialogConfig.java 2KB
EasyPermissions.java 20KB
easypermissions.iml 10KB
build.gradle 534B
proguard-rules.pro 641B
gradlew.bat 2KB
gradle.properties 747B
.gitignore 127B
build.gradle 521B
共 81 条
- 1
资源评论
- zhp0753662019-08-14可以用,谢谢分享
- U_main2020-01-14可以用!谢谢分享!
- 骑驴望星空2019-01-02还可以,以后就用这个框架了获取权限了
- ll4441694872017-12-05还没用,待检测
- 兔子的先生2017-10-16不是很好用
小火柴卖的小孩
- 粉丝: 11
- 资源: 4
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功