MainActivityPermissionsDispatcher 找不到
1、引用
annotationProcessor 'com.github.hotchemi:permissionsdispatcher-processor:2.1.3'
2、在使用的界面上
@RuntimePermissions public class MainActivity extends AppCompatActivity {}
3、make project一下
这个库会自动生成[Activity Name] + PermissionsDispatcher这样的一个类。当要调用使用权限的方法的时候,不直接调用我们直接添加了@NeedsPermission的方法,而是调用这个类生成的XXXWithCheck的方法。XXX就是添加了@NeedsPermission的方法名。
整个代码:
package com.example.java.mypermissions;
import android.Manifest;
import android.annotation.SuppressLint;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import permissions.dispatcher.NeedsPermission;
import permissions.dispatcher.OnNeverAskAgain;
import permissions.dispatcher.OnPermissionDenied;
import permissions.dispatcher.OnShowRationale;
import permissions.dispatcher.PermissionRequest;
import permissions.dispatcher.RuntimePermissions;
@RuntimePermissions
public class MainActivity extends AppCompatActivity {
private Button btn_call;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate (savedInstanceState);
setContentView (R.layout.activity_main);
btn_call = findViewById (R.id.btn_call);
btn_call.setOnClickListener (new View.OnClickListener () {
@Override
public void onClick(View v) {
MainActivityPermissionsDispatcher.callWithCheck(MainActivity.this);
}
});
}
@SuppressLint("MissingPermission")
@NeedsPermission(Manifest.permission.CALL_PHONE)
void call() {
Intent intent= new Intent (Intent.ACTION_CALL);
Uri data= Uri.parse("tel:" + "10080");
intent.setData (data);
startActivity (intent);
}
@OnShowRationale(Manifest.permission.CALL_PHONE)
void showWhy(final PermissionRequest request) {
new AlertDialog.Builder (this).setMessage ("提示用户为何开启权限")
.setPositiveButton ("知道了", new DialogInterface.OnClickListener () {
@Override
public void onClick(DialogInterface dialog, int which) {
request.proceed ();
}
}).show ();
}
@OnPermissionDenied(Manifest.permission.CALL_PHONE)
void showDenied() {
Toast.makeText(this, "用户选择拒绝时的提示", Toast.LENGTH_SHORT).show ();
}
@OnNeverAskAgain(Manifest.permission.CALL_PHONE)
void showNotAsk() {
new AlertDialog.Builder (this).setMessage ("该功能需要访问电话的权限,不开启将无法正常工作!")
.setPositiveButton ("确定", new DialogInterface.OnClickListener () {
@Override
public void onClick(DialogInterface dialog, int which) {
}
}).show ();
}
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissons, int[] grantResults) {
super.onRequestPermissionsResult (requestCode, permissons, grantResults);
MainActivityPermissionsDispatcher.onRequestPermissionsResult(this, requestCode, grantResults);
}
}
app build.grade:
applyplugin:'com.android.application'
android{
compileSdkVersion28
defaultConfig{
javaCompileOptions{
annotationProcessorOptions{
includeCompileClasspathtrue
}
}
applicationId"com.example.java.mypermissions"
minSdkVersion19
targetSdkVersion28
versionCode1
versionName"1.0"
testInstrumentationRunner"android.support.test.runner.AndroidJUnitRunner"
}
buildTypes{
release{
minifyEnabledfalse
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'),'proguard-rules.pro'
}
}
compileOptions{
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}
dependencies{
implementation fileTree(include:['*.jar'],dir:'libs')
implementation'com.android.support:appcompat-v7:27.1.1'
implementation'com.android.support:support-v4:27.1.1'
implementation'com.android.support.constraint:constraint-layout:1.1.3'
testImplementation'junit:junit:4.12'
androidTestImplementation'com.android.support.test:runner:1.0.2'
androidTestImplementation'com.android.support.test.espresso:espresso-core:3.0.2'
implementation'com.github.hotchemi:permissionsdispatcher:2.1.3'
annotationProcessor'com.github.hotchemi:permissionsdispatcher-processor:2.1.3'
}
progect build.grade:
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript{
repositories{
google()
jcenter()
}
dependencies{
classpath'com.android.tools.build:gradle:3.3.0'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects{
repositories{
google()
jcenter()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
代码:https://github.com/acridin/myPermissions