package com.example.administrator.myeasypermissions;
import android.Manifest;import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;import android.view.View;
import android.widget.TextView;import android.widget.Toast;
import java.util.List;
import pub.devrel.easypermissions.AfterPermissionGranted;
import pub.devrel.easypermissions.AppSettingsDialog;
import pub.devrel.easypermissions.EasyPermissions;
public class Main3Activity extends AppCompatActivity implements EasyPermissions.PermissionCallbacks {
private static final int CALL_PHONE = 0x01;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.test_activity);
TextView mTvCallPhone = (TextView) findViewById(R.id.tv_contacts);
mTvCallPhone.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
requestCallPhonePermission();
}
});
}
@AfterPermissionGranted(CALL_PHONE)
public void requestCallPhonePermission() {
String[] perms = {Manifest.permission.CALL_PHONE};
if (EasyPermissions.hasPermissions(this, perms)) {
// call();
} else {
EasyPermissions.requestPermissions(this, "xxx請(qǐng)求撥打電話權(quán)限蹬竖!", CALL_PHONE, perms);
}
}
@Override
public void onPermissionsGranted(int requestCode, List perms) {//權(quán)限同意 Toast.makeText(this, "你允許了本權(quán)限", Toast.LENGTH_SHORT).show();
}
@Override
public void onPermissionsDenied(int requestCode, List perms) {//權(quán)限拒絕
if (EasyPermissions.somePermissionPermanentlyDenied(this, perms)) {
new AppSettingsDialog.Builder(this, "為了您能使用電話功能林艘,請(qǐng)開啟打電話權(quán)限灰伟!").setTitle("提示").setPositiveButton("去設(shè)置").setNegativeButton("取消", null).setRequestCode(CALL_PHONE).build().show();
}else{
Toast.makeText(this, "你拒絕了本權(quán)限呆奕,將無法使用部分功能", Toast.LENGTH_SHORT).show();
}
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
EasyPermissions.onRequestPermissionsResult(requestCode, permissions, grantResults, this);
}}
觸發(fā)點(diǎn)擊事件峰尝,先調(diào)用
EasyPermissions.requestPermissions(this,"xxx請(qǐng)求撥打電話權(quán)限偏窝!",CALL_PHONE,perms);
后武学,EasyPermissions會(huì)去向系統(tǒng)請(qǐng)求相應(yīng)的權(quán)限祭往,第一次請(qǐng)求,系統(tǒng)會(huì)彈出警告框:
不論點(diǎn)擊哪個(gè)選項(xiàng)火窒,均會(huì)走Activity的方法
onRequestPermissionsResult(intrequestCode,@NonNullString[] permissions,@NonNullint[] grantResults)
硼补,
然后轉(zhuǎn)交給
EasyPermissions.onRequestPermissionsResult(requestCode,permissions,grantResults,this)
;
如果選擇的是允許,則會(huì)走Activity的
onPermissionsGranted(intrequestCode,Listperms)
;
方法熏矿,表示用戶允許相應(yīng)權(quán)限已骇,然后就可以做后續(xù)的操作;
如果選擇的是拒絕票编,則走
onPermissionsDenied(intrequestCode,Listperms)
;
方法褪储,表示用戶拒絕相應(yīng)權(quán)限,
EasyPermissions.somePermissionPermanentlyDenied(this,perms)
;
方法表示這個(gè)權(quán)限是否被“不再提醒”了(就是把那個(gè)小勾勾點(diǎn)上了慧域,這個(gè)東西鲤竹,只會(huì)在第一次拒絕后,再申請(qǐng)權(quán)限的時(shí)候出現(xiàn))昔榴,所以此時(shí)是走else的辛藻;
如果第一次已經(jīng)拒絕了對(duì)應(yīng)權(quán)限碘橘,則第二次申請(qǐng)權(quán)限時(shí)(因?yàn)榇藭r(shí)還沒有選過“不再提醒”),會(huì)彈出EasyPermissions定義的提醒框吱肌,見圖:
點(diǎn)擊取消:走Activity的
onPermissionsDenied(intrequestCode,Listperms)
痘拆;
點(diǎn)擊確定:彈出對(duì)話框:
點(diǎn)擊拒絕(不點(diǎn)勾勾):走Activity的
onRequestPermissionsResult(intrequestCode,@NonNullString[] permissions,@NonNullint[] grantResults)
;
再走
onPermissionsDenied(intrequestCode,Listperms)
;
點(diǎn)擊拒絕(點(diǎn)勾勾):走Activity的
onRequestPermissionsResult(intrequestCode,@NonNullString[] permissions,@NonNullint[] grantResults)
;
再走onPermissionsDenied(intrequestCode,Listperms)
氮墨;
此時(shí)
EasyPermissions.somePermissionPermanentlyDenied(this,perms)
错负;
會(huì)生效,走其中的語句:
newAppSettingsDialog.Builder(this,"為了您能使用電話功能勇边,請(qǐng)開啟打電話權(quán)限犹撒!").setTitle("提示").setPositiveButton("去設(shè)置").setNegativeButton("取消",null).setRequestCode(CALL_PHONE).build().show()
;
彈出EasyPermissions的對(duì)話框:
點(diǎn)取消:代表取消申請(qǐng)了;
點(diǎn)去設(shè)置:跳到系統(tǒng)的app設(shè)置中
在這用戶手動(dòng)設(shè)置權(quán)限就可以了粒褒;
如果前面的申請(qǐng)都拒絕了识颊,只要沒有選過小勾勾,則走的流程都和第二次申請(qǐng)的流程一樣奕坟;
如果前面的申請(qǐng)都拒絕了祥款,只要有選過小勾勾,則流程也和第二次申請(qǐng)的流程類似月杉,只是會(huì)少了刃跛,第一個(gè)彈框,即只會(huì)沒有下面這個(gè)彈框(推測(cè)是EasyPermissions做了判斷苛萎,如果沒有勾選“不再提醒”則彈框桨昙,否則不彈框),后續(xù)步驟一樣:
看完這篇腌歉,還不是很清楚的蛙酪,可以看我另一片文章,里面的代碼可以直接復(fù)制來用
EasyPermissions的流程翘盖,更加詳細(xì)