ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • 안드로이드6.0 사용자에게 권한 요청하기
    ANDROID 2017. 1. 11. 12:23
    반응형



    이미 여러곳을 보고 온거라고 생각합니다..!


    https://developer.android.com/training/permissions/requesting.html?hl=ko#perm-check



    확인해보시고


    전체적인 코드는 


    //-------------------------------------- AuthorityCheck---------------------------------------------//
    private static final int MY_PERMISSIONS_REQUEST_READ_PHONE_STATE = 1;
    String PhoneNumber ="";
    //-------------------------------------- AuthorityCheck---------------------------------------------//
    AuthorityCheck();


    //--- 권한 여부 확인 ---//
    public void AuthorityCheck(){

    if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) {

    // Should we show an explanation?
    if (ActivityCompat.shouldShowRequestPermissionRationale(this,
    Manifest.permission.READ_PHONE_STATE)) {
    //sdk 23버전이상
    //Toast.makeText(mContext,"23버전이상",Toast.LENGTH_SHORT).show();
    // Show an expanation to the user *asynchronously* -- don't block
    // this thread waiting for the user's response! After the user
    // sees the explanation, try again to request the permission.
    //PhoneNumber = getPhoneNumber(mContext,"-");
    Toast.makeText(mContext,"권한요청을 사용자가 거절",Toast.LENGTH_SHORT).show();
    } else {
    // No explanation needed, we can request the permission.
    ActivityCompat.requestPermissions(this,
    new String[]{Manifest.permission.READ_PHONE_STATE},
    MY_PERMISSIONS_REQUEST_READ_PHONE_STATE);

    // MY_PERMISSIONS_REQUEST_READ_CONTACTS is an
    // app-defined int constant. The callback method gets the
    // result of the request.
    Toast.makeText(mContext,"첫실행 -> 권한요청",Toast.LENGTH_SHORT).show();
    }
    }else {
    PhoneNumber = Common.getPhoneNumber(mContext,"-");
    Toast.makeText(mContext,"권한있음==="+PhoneNumber,Toast.LENGTH_SHORT).show();
    }
    Log.d("PhoneNumber",PhoneNumber);
    }

    걍 복붙!! 하시면 됩니다! 이게 편안하니까요..ㅋㅋㅋ


    getphonenumber 함수는 빼버리시면 됩니다.. 제가 만든거니까여 ㅎㅎ


    만약 v4 임포트 오류가 발생한다면 아래글 보기!  을 확인해주세요.







    반응형

    댓글

Designed by Tistory.