當(dāng)前手機(jī)各種系統(tǒng)權(quán)限比較多枣接,很多時(shí)候需要?jiǎng)討B(tài)的獲取一些系統(tǒng)權(quán)限盒至,添加其中拴曲,為此在寫很多APP的時(shí)候需要在APP首個(gè)界面檢查添加權(quán)限争舞,所以自己封裝了一個(gè)根據(jù)類。
PermissionUtils類
package com.fly.fengtaobao.utils;
import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build;
import android.provider.Settings;
import android.support.v4.content.ContextCompat;
import java.util.ArrayList;
import java.util.List;
/**
* <pre>
* .----.
* _.'__ `.
* .--(Q)(OK)---/$\
* .' @ /$$$\
* : , $$$$$
* `-..__.-' _.-\$$$/
* `;_: `"'
* .'"""""`.
* /, FLY ,\
* // \\
* `-._______.-'
* ___`. | .'___
* (______|______)
* </pre>
* 包 名 : com.fly.fengtaobao.utils
* 作 者 : FLY
* 創(chuàng)建時(shí)間 : 2017/8/3
* <p>
* 描述: 動(dòng)態(tài)獲取權(quán)限
*/
public class PermissionUtils {
public static final int SETTING_REQUEST_CODE = 1111;
/**
* 獲取請(qǐng)求權(quán)限中需要授權(quán)的權(quán)限
* @param context
* @param permissions 一個(gè)或多個(gè)權(quán)限
* @return
*/
public static List<String> getDeniedPermissions(Context context,String... permissions){
List<String> deniedPermissions = new ArrayList<String>();
for (String permission : permissions) {
//判斷權(quán)限是否授予
if (ContextCompat.checkSelfPermission(context,permission)== PackageManager.PERMISSION_DENIED){
deniedPermissions.add(permission);
}
}
return deniedPermissions;
}
/**
* 授權(quán)權(quán)限
* @param activity
* @param permission_request_code
* @param permissions
* @return
*/
@TargetApi(Build.VERSION_CODES.M)
public static boolean isPermissionsGranted(Activity activity,int permission_request_code,String... permissions){
List<String> deniedPermissions = PermissionUtils.getDeniedPermissions(activity,permissions);
if (deniedPermissions != null && deniedPermissions.size()>0){
activity.requestPermissions(
deniedPermissions.toArray(new String[deniedPermissions.size()])
,permission_request_code);
//授權(quán)Activity回調(diào)
}else {
//權(quán)限通過(guò)
return true;
}
return false;
}
/**
* 驗(yàn)證所有權(quán)限是否都已經(jīng)授權(quán)
* @param grantResults
* @return
*/
public static boolean verifyPermissions(int[] grantResults){
for (int grantResult:grantResults) {
if (grantResult != PackageManager.PERMISSION_DENIED){
return false;
}
}
return true;
}
/**
* 顯示提示對(duì)話框
* @param context
*/
public static void showTipsDialog(Activity context){
LibDialogUtils.showPermissionTipsDialog(context);
}
public static void startAppSettings(Activity context){
Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
intent.setData(Uri.parse("package:"+context.getPackageName()));
//得到新打開(kāi)Activity關(guān)閉后返回的數(shù)據(jù)
//第二個(gè)參數(shù)為請(qǐng)求碼澈灼,可以根據(jù)業(yè)務(wù)需求自己編號(hào)
context.startActivityForResult(intent,SETTING_REQUEST_CODE);
context.startActivity(intent);
}
}
LibDialogUtils類
package com.fly.fengtaobao.utils;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import com.fly.fengtaobao.R;
/**
* <pre>
* .----.
* _.'__ `.
* .--(Q)(OK)---/$\
* .' @ /$$$\
* : , $$$$$
* `-..__.-' _.-\$$$/
* `;_: `"'
* .'"""""`.
* /, FLY ,\
* // \\
* `-._______.-'
* ___`. | .'___
* (______|______)
* </pre>
* 包 名 : com.fly.fengtaobao.utils
* 作 者 : FLY
* 創(chuàng)建時(shí)間 : 2017/8/4
* <p>
* 描述: Dialog工具類
*/
public class LibDialogUtils {
public static void showPermissionTipsDialog(final Activity activity){
new AlertDialog.Builder(activity)
.setTitle(R.string.LibDialogUtils_showPermissionTipsDialog_title)
.setMessage(R.string.LibDialogUtils_showPermissionTipsDialog_message)
.setPositiveButton(R.string.LibDialogUtils_showPermissionTipsDialog_yes, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
PermissionUtils.startAppSettings(activity);
}
}).show();
}
}
SplashActivity使用地方
package com.fly.fengtaobao.View;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.view.WindowManager;
import com.fly.fengtaobao.R;
import com.fly.fengtaobao.View.base.BaseActivity;
import com.fly.fengtaobao.View.common.config.Config;
import com.fly.fengtaobao.utils.PermissionUtils;
/**
* <pre>
* .----.
* _.'__ `.
* .--(Q)(OK)---/$\
* .' @ /$$$\
* : , $$$$$
* `-..__.-' _.-\$$$/
* `;_: `"'
* .'"""""`.
* /, FLY ,\
* // \\
* `-._______.-'
* ___`. | .'___
* (______|______)
* </pre>
* 創(chuàng)建時(shí)間 : 2017/7/26
* <p>
* 描述: View --> 閃屏
* 1.動(dòng)態(tài)權(quán)限的獲取
* 2.判斷是否可以正常獲取到 手機(jī)串號(hào)
*/
public class SplashActivity extends BaseActivity {
/**
* 要?jiǎng)討B(tài)獲取權(quán)限時(shí),延時(shí)100毫秒,進(jìn)入下一個(gè)頁(yè)面
*/
private static final long DELAY_MILLIS_PERMISSION = 100;
/**
* 正常情況下,延時(shí)1300毫秒,進(jìn)入下一個(gè)頁(yè)面
*/
private static final long DELAY_MILLIS = 500;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//取消狀態(tài)欄
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_splash);
/**
* 獲取權(quán)限
*/
takePermission();
}
private void init() {
}
private void takePermission() {
if (PermissionUtils.isPermissionsGranted(this, Config.SPLASH_ACTIVITY_PERMISSION_REQUEST_CODE
, Config.mPermissionsSplashActivity)) {
onPermissionsGranted();
}
}
private void onPermissionsGranted() {
init();
}
private void onPermissionsDenied() {
PermissionUtils.showTipsDialog(this);
}
/**
* 申請(qǐng)權(quán)限的回調(diào)
*/
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == Config.SPLASH_ACTIVITY_PERMISSION_REQUEST_CODE) {
if (PermissionUtils.verifyPermissions(grantResults)) {
onPermissionsGranted();
} else {
onPermissionsDenied();
}
}
}
/**
* 從設(shè)置界面回來(lái)之后的回調(diào),重新驗(yàn)證權(quán)限是否獲取到
*/
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == PermissionUtils.SETTING_REQUEST_CODE) {
takePermission();
}
}
}