「Android/権限」の版間の差分
提供: 初心者エンジニアの簡易メモ
(ページの作成:「==権限チェック== <pre> if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) { Log.i("TA...」) |
|||
行16: | 行16: | ||
参考:https://qiita.com/KIRIN3qiita/items/9ea8317b908d7f9d68c3 | 参考:https://qiita.com/KIRIN3qiita/items/9ea8317b908d7f9d68c3 | ||
+ | |||
+ | ==READ_PHONE_STATEエラーとなる場合== | ||
+ | Android6からAndroidManifestにpermissionを記載しても、許可されなくなった、ユーザに聞いて、許可を得ないと権限が与えられない。 | ||
+ | |||
+ | 以下の通りshowPhoneStatePermission()をActivityから実行する。 | ||
+ | |||
+ | サンプル | ||
+ | <pre> | ||
+ | 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(); | ||
+ | } | ||
+ | } | ||
+ | </pre> | ||
+ | 参考:ActivityCompat.requestPermissionsがダイアログボックスを表示しない https://www.366service.com/jp/qa/d704ecb7ccb765d4358431c6a7e2eaee | ||
+ | |||
+ | 参考:Android 6.0からリクエストが必要なパーミッション一覧 (Dangerous パーミッション) https://feel-log.net/android/developer/marshmallow-permissions-runtime-request/ |
2020年11月2日 (月) 16:15時点における版
権限チェック
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エラーとなる場合
Android6からAndroidManifestにpermissionを記載しても、許可されなくなった、ユーザに聞いて、許可を得ないと権限が与えられない。
以下の通りshowPhoneStatePermission()をActivityから実行する。
サンプル
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/