在棉花糖中引入的Android中的运行时权限(由于这些运行时权限),用户可以在运行时向应用授予权限,同时还可以编写运行时权限的代码。有时,处理请求权限的任务变得如此困难,开发人员不得不编写大量代码。因此,我们将看到使用Dexter在Android中实现运行时权限。
Android中的Dexter是什么?
Dexter是一个库,可以帮助我们简化此任务,以便在Android中处理运行时权限。现在,我们将在我们的Android应用程序中看到此实现。
我们将在本文中构建什么?
我们将构建一个简单的应用程序,向用户显示一个按钮。单击该按钮后,我们将向用户显示运行时权限。因此,如果用户拒绝该权限,那么我们将显示打开设置屏幕并授予权限的选项。下面是一个简短的视频,我们将在其中看到我们将要构建的内容。注意,我们将使用Java语言实现该项目。
分步实施
步骤1:创建一个新项目
要在Android Studio中创建新项目,请参阅如何在Android Studio中创建/启动新项目。请注意,选择Java作为编程语言。
步骤2:在build.gradle文件中添加Dexter运行时权限的依赖性
导航到gradle脚本,然后构建到build.gradle(Module)级别。在依赖性部分的build.gradle文件中添加以下行。
implementation ‘com.karumi:dexter:6.2.2’
添加此依赖项后,现在同步您的项目。让我们转向XML部分。
步骤3:使用activity_main.xml文件
转到activity_main.xml文件,并参考以下代码。以下是activity_main.xml文件的代码。
XML
Java
import android.Manifest;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.provider.Settings;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import com.karumi.dexter.Dexter;
import com.karumi.dexter.MultiplePermissionsReport;
import com.karumi.dexter.PermissionToken;
import com.karumi.dexter.listener.DexterError;
import com.karumi.dexter.listener.PermissionRequest;
import com.karumi.dexter.listener.PermissionRequestErrorListener;
import com.karumi.dexter.listener.multi.MultiplePermissionsListener;
import java.util.List;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// initializing our button and adding on click listener to it.
Button requestPermissionsBtn = findViewById(R.id.idBtnRequestPermission);
requestPermissionsBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// inside on click listener calling
// method to request permission
requestPermissions();
}
});
}
private void requestPermissions() {
// below line is use to request
// permission in the current activity.
Dexter.withActivity(this)
// below line is use to request the number of
// permissions which are required in our app.
.withPermissions(Manifest.permission.CAMERA,
// below is the list of permissions
Manifest.permission.ACCESS_FINE_LOCATION,
Manifest.permission.READ_CONTACTS)
// after adding permissions we are
// calling an with listener method.
.withListener(new MultiplePermissionsListener() {
@Override
public void onPermissionsChecked(MultiplePermissionsReport multiplePermissionsReport) {
// this method is called when all permissions are granted
if (multiplePermissionsReport.areAllPermissionsGranted()) {
// do you work now
Toast.makeText(MainActivity.this, "All the permissions are granted..", Toast.LENGTH_SHORT).show();
}
// check for permanent denial of any permission
if (multiplePermissionsReport.isAnyPermissionPermanentlyDenied()) {
// permission is denied permanently,
// we will show user a dialog message.
showSettingsDialog();
}
}
@Override
public void onPermissionRationaleShouldBeShown(List list, PermissionToken permissionToken) {
// this method is called when user grants some
// permission and denies some of them.
permissionToken.continuePermissionRequest();
}
}).withErrorListener(new PermissionRequestErrorListener() {
// this method is use to handle error
// in runtime permissions
@Override
public void onError(DexterError error) {
// we are displaying a toast message for error message.
Toast.makeText(getApplicationContext(), "Error occurred! ", Toast.LENGTH_SHORT).show();
}
})
// below line is use to run the permissions
// on same thread and to check the permissions
.onSameThread().check();
}
// below is the shoe setting dialog
// method which is use to display a
// dialogue message.
private void showSettingsDialog() {
// we are displaying an alert dialog for permissions
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
// below line is the title
// for our alert dialog.
builder.setTitle("Need Permissions");
// below line is our message for our dialog
builder.setMessage("This app needs permission to use this feature. You can grant them in app settings.");
builder.setPositiveButton("GOTO SETTINGS", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// this method is called on click on positive
// button and on clicking shit button we
// are redirecting our user from our app to the
// settings page of our app.
dialog.cancel();
// below is the intent from which we
// are redirecting our user.
Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
Uri uri = Uri.fromParts("package", getPackageName(), null);
intent.setData(uri);
startActivityForResult(intent, 101);
}
});
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// this method is called when
// user click on negative button.
dialog.cancel();
}
});
// below line is used
// to display our dialog
builder.show();
}
}
XML
步骤4:使用MainActivity。 Java文件
转到MainActivity。 Java文件并参考以下代码。下面是MainActivity的代码。 Java文件。在代码内部添加了注释,以更详细地了解代码。
Java
import android.Manifest;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.provider.Settings;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import com.karumi.dexter.Dexter;
import com.karumi.dexter.MultiplePermissionsReport;
import com.karumi.dexter.PermissionToken;
import com.karumi.dexter.listener.DexterError;
import com.karumi.dexter.listener.PermissionRequest;
import com.karumi.dexter.listener.PermissionRequestErrorListener;
import com.karumi.dexter.listener.multi.MultiplePermissionsListener;
import java.util.List;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// initializing our button and adding on click listener to it.
Button requestPermissionsBtn = findViewById(R.id.idBtnRequestPermission);
requestPermissionsBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// inside on click listener calling
// method to request permission
requestPermissions();
}
});
}
private void requestPermissions() {
// below line is use to request
// permission in the current activity.
Dexter.withActivity(this)
// below line is use to request the number of
// permissions which are required in our app.
.withPermissions(Manifest.permission.CAMERA,
// below is the list of permissions
Manifest.permission.ACCESS_FINE_LOCATION,
Manifest.permission.READ_CONTACTS)
// after adding permissions we are
// calling an with listener method.
.withListener(new MultiplePermissionsListener() {
@Override
public void onPermissionsChecked(MultiplePermissionsReport multiplePermissionsReport) {
// this method is called when all permissions are granted
if (multiplePermissionsReport.areAllPermissionsGranted()) {
// do you work now
Toast.makeText(MainActivity.this, "All the permissions are granted..", Toast.LENGTH_SHORT).show();
}
// check for permanent denial of any permission
if (multiplePermissionsReport.isAnyPermissionPermanentlyDenied()) {
// permission is denied permanently,
// we will show user a dialog message.
showSettingsDialog();
}
}
@Override
public void onPermissionRationaleShouldBeShown(List list, PermissionToken permissionToken) {
// this method is called when user grants some
// permission and denies some of them.
permissionToken.continuePermissionRequest();
}
}).withErrorListener(new PermissionRequestErrorListener() {
// this method is use to handle error
// in runtime permissions
@Override
public void onError(DexterError error) {
// we are displaying a toast message for error message.
Toast.makeText(getApplicationContext(), "Error occurred! ", Toast.LENGTH_SHORT).show();
}
})
// below line is use to run the permissions
// on same thread and to check the permissions
.onSameThread().check();
}
// below is the shoe setting dialog
// method which is use to display a
// dialogue message.
private void showSettingsDialog() {
// we are displaying an alert dialog for permissions
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
// below line is the title
// for our alert dialog.
builder.setTitle("Need Permissions");
// below line is our message for our dialog
builder.setMessage("This app needs permission to use this feature. You can grant them in app settings.");
builder.setPositiveButton("GOTO SETTINGS", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// this method is called on click on positive
// button and on clicking shit button we
// are redirecting our user from our app to the
// settings page of our app.
dialog.cancel();
// below is the intent from which we
// are redirecting our user.
Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
Uri uri = Uri.fromParts("package", getPackageName(), null);
intent.setData(uri);
startActivityForResult(intent, 101);
}
});
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// this method is called when
// user click on negative button.
dialog.cancel();
}
});
// below line is used
// to display our dialog
builder.show();
}
}
步骤5:在清单文件中添加所需的权限
导航至应用程序> AndroidManifest.xml文件,然后向其添加以下权限。
XML格式
在我们的AndroidManifest.xml文件中添加这些权限之后。现在运行我们的应用程序,然后查看代码输出。