使用AddPermission,獲取Android6.o許可權,附上一個自定義的dialog,簡單實用。
先來看看效果。。
1.新增AddPermission的依賴
gitbub地址:https://github.com/yanzhenjie/AndPermission
implementation ‘com.yanzhenjie:permission:2.0.0-rc12’
推薦先看AddPermission的文件:http://www.yanzhenjie.com/AndPermission/cn/runtime/
2.基本使用
AndPermission.with(this)
.runtime()
.permission(Permission.WRITE_EXTERNAL_STORAGE)
.onGranted(new Action() {
@Override
public void onAction(List permissions) {
// TODO 許可權同意
}
}).onDenied(new Action() {
@Override
public void onAction(List permissions) {
// TODO 許可權拒絕
}
}).start();
被拒絕第一次後,第二次開啟應用再次請求呼叫
public static Rationale<List<String>> mRationale = new Rationale<List<String>>() { @Override public void showRationale(Context context, List<String> data, RequestExecutor executor) { showDialog(context, executor, permissions); } };
/*、
* Dialog,提示是否繼續申請許可權,繼續 executor.execute();取消 executor.cancel();
* */
public static void showDialog(Context context, final RequestExecutor executor, List<String> permissions) { /*把許可權轉化成文字*/ String permissionText = TextUtils.join(",\n", permissions); final MyDialog dialog = new MyDialog(context,R.style.customDialog,R.layout.my_dialog,new int[]{R.id.dialog_cancel,R.id.dialog_sure}); dialog.setOnCenterItemClickListener(new MyDialog.OnCenterItemClickListener() { @Override public void setOnMyDialogClickListener(MyDialog dialog, View view) { switch (view.getId()){ case R.id.dialog_cancel: Log.d(TAG, "setOnMyDialogClickListener: 取消"); executor.cancel(); break; case R.id.dialog_sure: Log.d(TAG, "setOnMyDialogClickListener: 確定"); executor.execute(); break; } } }); Log.d(TAG, "showDialog: "); dialog.show(); dialog.setContentText("雲圖書館想獲取"+permissionText+"許可權"); }
/*、
* 獲取許可權
* @param context
* @param permissions 許可權組合,可以寫多個許可權
* */
public void requestPermission(final Context context,final String… permissions)
AndPermission.with(context)
.runtime()
.permission(permissions)
.rationale(mRationale)/
許可權被拒絕後再下一次啟動後再次申請獲取許可權/
/許可權同意/
.onGranted(new Action<List>() {
@Override
public void onAction(List data) {
}
})
/許可權拒絕/
.onDenied(new Action<List>() {
@Override
public void onAction(List data) {
/許可權總是被拒絕/ if(AndPermission.hasAlwaysDeniedPermission(context,permissions)) {
//可以在這裡讓進入系統設定許可權
}
Toast.makeText(context, “許可權拒絕”, Toast.LENGTH_SHORT).show();
}
}).start();
自定義的Dialog
package com.example.his.cloudlibaray.utils;
import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.Display;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;
import com.example.his.cloudlibaray.R;
public class MyDialog extends Dialog implements View.OnClickListener{
private Context mContext;
private int layoutRes;
private int[] listenedItem;
private TextView contentTv;
public MyDialog(@NonNull Context context, int themeResId, int layoutRes, int[] listenedItem) {
super(context, themeResId);
this.mContext = context;
this.layoutRes = layoutRes;
this.listenedItem = listenedItem;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Window win = getWindow();
win.setGravity(Gravity.CENTER);/*設定對話方塊居中*/
setContentView(layoutRes);
contentTv = findViewById(R.id.dialog_text);
WindowManager window = ((Activity) mContext).getWindowManager();
Display display = window.getDefaultDisplay();
WindowManager.LayoutParams lp = getWindow().getAttributes();
/*設定dialog寬度為螢幕的4/5*/
lp.width = display.getWidth() * 3 / 5;
getWindow().setAttributes(lp);
/*獲取當前view,實現圓角*/
View view = getWindow().getDecorView();
ViewStyleSetter viewStyleSetter = new ViewStyleSetter(view);
viewStyleSetter.setRound(Utils.dip2px(mContext,10));
//遍歷控制元件,新增點選監聽事件
for (int id : listenedItem) {
TextView tv = (TextView)findViewById(id);
tv.setOnClickListener(this);
}
}
private OnCenterItemClickListener listener;
@Override
public void onClick(View v) {
dismiss();
listener.setOnMyDialogClickListener(this,v);
}
public interface OnCenterItemClickListener{
void setOnMyDialogClickListener(MyDialog dialog,View view);
}
public void setOnCenterItemClickListener(OnCenterItemClickListener listener){
this.listener = listener;
}
/*設定對話方塊的內容*/
public void setContentText(String text){
contentTv.setText(text);
}
/*
* 點選dialog外部消失*/
public void setCancelTouch(boolean cancel) {
setCanceledOnTouchOutside(cancel);
}
}
<?xml version="1.0" encoding="utf-8"?>對應的佈局檔案
<TextView
android:id="@+id/dialog_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="20dp"
android:layout_marginTop="20dp"
android:gravity="center"
android:text="需要寫的資訊"
android:textColor="@android:color/black"
android:textSize="13sp" />
<View
android:layout_width="match_parent"
android:layout_height="1px"
android:background="@color/title_bar"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="30dp"
android:orientation="horizontal">
<TextView
android:id="@+id/dialog_cancel"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:text="取消"
android:clickable="true"
android:background="@drawable/btn_back"
android:textColor="@color/light_blue_2"
android:textSize="15sp" />
<View
android:layout_height="match_parent"
android:layout_width ="1px"
android:background="@color/title_bar"/>
<TextView
android:id="@+id/dialog_sure"
android:layout_width="0dp"
android:clickable="true"
android:background="@drawable/btn_back"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:text="確定"
android:textColor="@color/light_blue_2"
android:textSize="15sp" />
</LinearLayout>
<color name="light_blue_2">#6c91c4</color>
<color name="title_bar">#9b9b9b</color>
實現圓角所需要的類在這裡
https://www.cnblogs.com/Dionexin/p/5727297.html