【Android】PermissionsDispatcher動的パーミッションライブラリを使用したパーミッション適合
9940 ワード
PermissionsDispatcher
PermissionsDispatcherは、必要なメソッドに対応する注釈を追加し、適切なときに注釈された対応するメソッドを呼び出す注釈に基づいて書かれたライブラリです.
PermissionsDispatcher公式解釈:PermissionsDispatcher provides a simple annotation-based API to handle runtime permissions.
1.プロジェクトにPermissionsDispatcherを導入する
app moduleの
プロジェクトの
2.AndroidManifest.xmlファイルでのアプリケーションの構成に必要な権限
3.注釈の追加(ここではActivityでの使用例)
PermissionsDispatcher introduces only a few annotations, keeping its general API concise:
NOTE: Annotated methods must not be private.
PermissionsDispatcherは現在、次の5つの注釈をサポートしています.
Annotation(注記)
Required(必須かどうか)
Description(説明/説明)
✓
Register an Activity or Fragment(we support both) to handle permissions.
✓
Annotate a method which performs the action that requires one or more permissions.
Annotate a method which explains why the permission/s is/are needed. It passes in a PermissionRequest object which can be used to continue or abort the current permission request upon user input.
Annotate a method which is invoked if the user doesn't grant the permissions.
Annotate a method which is invoked if the user chose to have the device "never ask again"about a permission. .
注意:権限設定ページに移動することもできます.
4.生成に権限管理を委任する方法(使用&ステータスコールバック)プロアクティブに権限の取得を要求コンパイル中、PermissionsDispatcherは([Activity Name]+PermissionsDispatcher)の命名規則に従ってクラスを生成し、これらの保護されたメソッドに安全にアクセスできます.
注意:MainActivityが生成したクラス名は
では、次の方法でカメラ権限を取得できます. onRequestPermissionsResultにコールバック を追加
MainActivty.JAvaコード
PermissionsDispatcherは、必要なメソッドに対応する注釈を追加し、適切なときに注釈された対応するメソッドを呼び出す注釈に基づいて書かれたライブラリです.
PermissionsDispatcher公式解釈:PermissionsDispatcher provides a simple annotation-based API to handle runtime permissions.
// PermissionsDispatcher API 。
This library lifts the burden that comes with writing a bunch of check statements whether a permission has been granted or not from you, in order to keep your code clean and safe. , , 。
1.プロジェクトにPermissionsDispatcherを導入する
app moduleの
build.gradle
ファイルに内容を追加します(${latest.version}
は最新バージョン番号に変更):dependencies {
compile("com.github.hotchemi:permissionsdispatcher:${latest.version}") {
// if you don't use android.app.Fragment you can exclude support for them
// andorid.app.Fragment, 。
exclude module: "support-v13"
}
annotationProcessor "com.github.hotchemi:permissionsdispatcher-processor:${latest.version}"
}
プロジェクトの
build.gradle
に次の内容を追加します.repositories {
jcenter()
maven { url 'http://oss.jfrog.org/artifactory/oss-snapshot-local/' }
}
2.AndroidManifest.xmlファイルでのアプリケーションの構成に必要な権限
3.注釈の追加(ここではActivityでの使用例)
PermissionsDispatcher introduces only a few annotations, keeping its general API concise:
// PermissionsDispatcher , API
NOTE: Annotated methods must not be private.
// : 。
PermissionsDispatcherは現在、次の5つの注釈をサポートしています.
Annotation(注記)
Required(必須かどうか)
Description(説明/説明)
@RuntimePermissions
✓
Register an Activity or Fragment(we support both) to handle permissions.
@NeedsPermission
✓
Annotate a method which performs the action that requires one or more permissions.
@OnShowRationale
Annotate a method which explains why the permission/s is/are needed. It passes in a PermissionRequest object which can be used to continue or abort the current permission request upon user input.
@OnPermissionDenied
Annotate a method which is invoked if the user doesn't grant the permissions.
@OnNeverAskAgain
Annotate a method which is invoked if the user chose to have the device "never ask again"about a permission.
@RuntimePermissions
注釈を追加:これは使用する必要がある注釈であり、権限申請をしたいActivityまたはFragmentで使用する:// 1.Register an Activity or Fragment(we support both) to handle permissions.
// Activity Fragment( ) 。[ ]
@RuntimePermissions
public class MainActivity extends AppCompatActivity {
}
@NeedsPermission
注釈を追加:使用する必要がある注釈は、権限を取得する方法に表示され、カッコ内に申請する必要がある1つ以上の権限が入力され、権限を取得するとメソッドがコールバックされます.@RuntimePermissions
public class MainActivity extends AppCompatActivity {
// 2.Annotate a method which performs the action that requires one or more permissions.
// 。[ ]
@NeedsPermission(Manifest.permission.CAMERA)
void showCamera() {
Toast.makeText(this, " ", Toast.LENGTH_SHORT).show();
}
}
@OnShowRationale
注釈を追加:必要でない注釈.注釈は、ユーザが初めて拒否した後、再び権限を取得するときに、なぜ権限を取得する必要があるのかを説明する方法である. // 3.Annotate a method which explains why the permission/s is/are needed. It passes in a
// PermissionRequest object which can be used to continue or abort the current
// permission request upon user input.
// ( , )。 PermissionRequest ,
// 。[ ]
@OnShowRationale(Manifest.permission.CAMERA)
void showRationaleForCamera(final PermissionRequest request) {
new AlertDialog.Builder(this)
.setMessage(R.string.permission_camera_rationale)
.setPositiveButton(" ", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
request.proceed();
}
})
.setNegativeButton(" ", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
request.cancel();
}
})
.show();
}
@OnPermissionDenied
注釈を追加:ユーザーが許可を拒否したときにコールバックする方法を注釈する必要はありません. // 4.Annotate a method which is invoked if the user doesn't grant the permissions
// 。[ ]
@OnPermissionDenied(Manifest.permission.CAMERA)
void showDeniedForCamera() {
Toast.makeText(this, " , 。", Toast.LENGTH_SHORT).show();
}
@OnNeverAskAgain
注釈を追加:必要でない注釈、1人のユーザーが「質問しない」を選択したときに提示するコールバック方法を注釈します.注意:権限設定ページに移動することもできます.
// 5.Annotate a method which is invoked if the user chose to have the device "never ask again"
// about a permission
// " " 。[ ]
@OnNeverAskAgain(Manifest.permission.CAMERA)
void showNeverAskForCamera() {
Toast.makeText(this, " , 。", Toast.LENGTH_SHORT).show();
}
4.生成に権限管理を委任する方法(使用&ステータスコールバック)
注意:MainActivityが生成したクラス名は
MainActivityPermissionsDispatcher
です.では、次の方法でカメラ権限を取得できます.
// NOTE: delegate the permission handling to generated method.
// : 。
MainActivityPermissionsDispatcher.showCameraWithPermissionCheck(MainActivity.this);
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
// NOTE: delegate the permission handling to generated method
// : 。
MainActivityPermissionsDispatcher.onRequestPermissionsResult(this, requestCode, grantResults);
}
MainActivty.JAvaコード
// 1.Register an Activity or Fragment(we support both) to handle permissions.
// Activity Fragment( ) 。[ ]
@RuntimePermissions
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.main_btn_camera).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// NOTE: delegate the permission handling to generated method.
// : 。
MainActivityPermissionsDispatcher.showCameraWithPermissionCheck(MainActivity.this);
}
});
}
// 2.Annotate a method which performs the action that requires one or more permissions.
// 。[ ]
@NeedsPermission(Manifest.permission.CAMERA)
void showCamera() {
Toast.makeText(this, " ", Toast.LENGTH_SHORT).show();
}
// 3.Annotate a method which explains why the permission/s is/are needed. It passes in a
// PermissionRequest object which can be used to continue or abort the current
// permission request upon user input.
// ( )。 PermissionRequest ,
// 。[ ]
@OnShowRationale(Manifest.permission.CAMERA)
void showRationaleForCamera(final PermissionRequest request) {
new AlertDialog.Builder(this)
.setMessage(R.string.permission_camera_rationale)
.setPositiveButton(" ", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
request.proceed();
}
})
.setNegativeButton(" " new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
request.cancel();
}
})
.show();
}
// 4.Annotate a method which is invoked if the user doesn't grant the permissions
// 。[ ]
@OnPermissionDenied(Manifest.permission.CAMERA)
void showDeniedForCamera() {
Toast.makeText(this, " , 。", Toast.LENGTH_SHORT).show();
}
// 5.Annotate a method which is invoked if the user chose to have the device "never ask again"
// about a permission
// " " 。[ ]
@OnNeverAskAgain(Manifest.permission.CAMERA)
void showNeverAskForCamera() {
Toast.makeText(this, " , 。", Toast.LENGTH_SHORT).show();
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
// NOTE: delegate the permission handling to generated method
// : 。
MainActivityPermissionsDispatcher.onRequestPermissionsResult(this, requestCode, grantResults);
}
}