facebook twitter hatena line email

Android/権限

提供: 初心者エンジニアの簡易メモ
移動: 案内検索

権限チェック

if (ContextCompat.checkSelfPermission(this,
      Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) {
    Log.i("TAG1", "NOT READ_PHONE_STATE");
} else {
    Log.i("TAG1", "READ_PHONE_STATE");
}
if (ContextCompat.checkSelfPermission(this,
      Manifest.permission.INTERNET) != PackageManager.PERMISSION_GRANTED) {
    Log.i("TAG1", "NOT INTERNET");
} else {
    Log.i("TAG1", "INTERNET");
}

参考:https://qiita.com/KIRIN3qiita/items/9ea8317b908d7f9d68c3

READ_PHONE_STATEエラーとなる場合

android.telephony.TelephonyManager.getNetworkType()で、SecurityExceptionが起こる。

原因は、READ_PHONE_STATEが許可されていないため。

AndroidRuntime: FATAL EXCEPTION: ConnectivityThread
java.lang.SecurityException: getDataNetworkTypeForSubscriber: uid 10430 does not have android.permission.READ_PHONE_STATE.

Android6からAndroidManifestにpermissionを記載しても、許可されなくなった、ユーザに聞いて、許可を得ないと権限が与えられない。

以下の通りshowPhoneStatePermission()をActivityから実行する。

サンプル

public class MainActivity extends AppCompatActivity {
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        showPhoneStatePermission();
    }
    private void showExplanation(String title,
                                 String message,
                                 final String permission,
                                 final int permissionRequestCode) {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle(title)
                .setMessage(message)
                .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                        requestPermission(permission, permissionRequestCode);
                    }
                });
        builder.create().show();
    }

    private void requestPermission(String permissionName, int permissionRequestCode) {
        ActivityCompat.requestPermissions(this,
                new String[]{permissionName}, permissionRequestCode);
    }

    private void showPhoneStatePermission() {
        int REQUEST_PERMISSION_PHONE_STATE = 1;
        int permissionCheck = ContextCompat.checkSelfPermission(
                this, Manifest.permission.READ_PHONE_STATE);
        if (permissionCheck != PackageManager.PERMISSION_GRANTED) {
            if (ActivityCompat.shouldShowRequestPermissionRationale(this,
                    Manifest.permission.READ_PHONE_STATE)) {
                showExplanation("Permission Needed", "Rationale", Manifest.permission.READ_PHONE_STATE, REQUEST_PERMISSION_PHONE_STATE);
            } else {
                requestPermission(Manifest.permission.READ_PHONE_STATE, REQUEST_PERMISSION_PHONE_STATE);
            }
        } else {
            Toast.makeText(MainActivity.this, "Permission (already) Granted!", Toast.LENGTH_SHORT).show();
        }
    }

参考:ActivityCompat.requestPermissionsがダイアログボックスを表示しない https://www.366service.com/jp/qa/d704ecb7ccb765d4358431c6a7e2eaee

参考:Android 6.0からリクエストが必要なパーミッション一覧 (Dangerous パーミッション) https://feel-log.net/android/developer/marshmallow-permissions-runtime-request/