마스터Q&A 안드로이드는 안드로이드 개발자들의 질문과 답변을 위한 지식 커뮤니티 사이트입니다. 안드로이드펍에서 운영하고 있습니다. [사용법, 운영진]

앱 실행중에 퍼미션 받는 것 질문..

0 추천

 

protected  void onResume(){
    super.onResume();
    // Local DB , table 생성


    //device얻는 코드.. 문제는 check_permission()호출되서 사용자에게 허락 묻는데..
    //사용자가 허락을 누르기도 전에 deviceID = telephony.getDeviceId(); 코드가 호출이 되서 앱이 멈춰버림. 이것을 방지해야 할 코드를 생각하기...
    check_permission();
    telephony = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
    deviceID  = telephony.getDeviceId();


    local = DB_LocalDB.getInstance(this,this);
    local.create_bookMark_db();
    local.create_bookMark_table();


}

 

check_permission()매서드가 앱 실행중에 위험 권한의 허용여부를 창에 띄워주는 코드입니다. 

문제는,  앱에서 사용자가 권한 승낙을 하기 전에  코드가 

deviceID  = telephony.getDeviceId();

여기까지 내려가버려서 프로그램 자체가 종료가 됩니다....

check_permission()메서드에서 사용자가 승낙 할 때까지 기다리게 할 수 없을까요...

check_permission()매서드는 아래와 같습니다. 

private void check_permission(){

    // Here, thisActivity is the current activity
    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)) {

            // 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.

        } else {

            // No explanation needed, we can request the permission.

            ActivityCompat.requestPermissions(this,
                    new String[]{Manifest.permission.READ_PHONE_STATE},
                    1);


            // MY_PERMISSIONS_REQUEST_READ_CONTACTS is an
            // app-defined int constant. The callback method gets the
            // result of the request.
        }
    }


}

public void onRequestPermissionsResult(int requestCode,
                                       String permissions[], int[] grantResults) {
    switch (requestCode) {
        case 1: {
            // If request is cancelled, the result arrays are empty.
            if (grantResults.length > 0
                    && grantResults[0] == PackageManager.PERMISSION_GRANTED) {

                ispermitted=true;
                // permission was granted, yay! Do the
                // contacts-related task you need to do.

            } else {

                // permission denied, boo! Disable the
                // functionality that depends on this permission.
            }
            return;
        }

        // other 'case' lines to check for other
        // permissions this app might request
    }
}

 

익명사용자 님이 2016년 10월 18일 질문

1개의 답변

0 추천
check_permission() 밑에 있는 코드들을 onRequestPermissionResult 안으로 옮겨 보세요. permission 관련 이벤트가 콜백으로 동작하기 때문에 check_permission()밑의 메소드들이 바로 실행되는 것입니다.
spark (227,930 포인트) 님이 2016년 10월 18일 답변
정확히 무엇을 어디로 옮겨야하는지..말해주실수 있나요

모든 코드를 switch 위에 두니 에러가 나네요.
저는 님의 코드를 다 알 수가 없으니 그건 님이 결정을 하셔야겠죠. 어떤 부분이 콜백이 호출된 다음에 실행되어야 할지는 님이 알고 계셔야 합니다.
...