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
}
}