更新 PermissionUtils.java
parent
a5e081a0a1
commit
b64cb19e17
|
|
@ -3,8 +3,10 @@ package com.nbee.echolink.utils;
|
|||
import android.Manifest;
|
||||
import android.app.Activity;
|
||||
import android.app.AlertDialog;
|
||||
import android.content.DialogInterface;
|
||||
import android.content.ComponentName;
|
||||
import android.content.Context;
|
||||
import android.content.pm.PackageManager;
|
||||
import android.provider.Settings;
|
||||
import android.widget.Toast;
|
||||
|
||||
import androidx.core.app.ActivityCompat;
|
||||
|
|
@ -17,7 +19,7 @@ import timber.log.Timber;
|
|||
|
||||
public class PermissionUtils {
|
||||
private static final int PERMISSION_REQUEST_CODE = 1;
|
||||
private Activity activity;
|
||||
private final Activity activity;
|
||||
|
||||
public PermissionUtils(Activity activity) {
|
||||
this.activity = activity;
|
||||
|
|
@ -30,11 +32,10 @@ public class PermissionUtils {
|
|||
Manifest.permission.READ_CALL_LOG,
|
||||
Manifest.permission.READ_CONTACTS,
|
||||
Manifest.permission.RECEIVE_SMS
|
||||
|
||||
};
|
||||
|
||||
if (!hasPermissions(permissions)) {
|
||||
Timber.d("请求以下权限:\n" + Arrays.toString(permissions));
|
||||
Timber.d("请求以下权限: %s", Arrays.toString(permissions));
|
||||
ActivityCompat.requestPermissions(activity, permissions, PERMISSION_REQUEST_CODE);
|
||||
}
|
||||
}
|
||||
|
|
@ -42,7 +43,7 @@ public class PermissionUtils {
|
|||
private boolean hasPermissions(String... permissions) {
|
||||
for (String permission : permissions) {
|
||||
if (ContextCompat.checkSelfPermission(activity, permission) != PackageManager.PERMISSION_GRANTED) {
|
||||
Timber.d("缺少权限:\n" + permission);
|
||||
Timber.d("缺少权限: %s", permission);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
|
@ -62,8 +63,8 @@ public class PermissionUtils {
|
|||
|
||||
if (deniedPermissions.length() > 0) {
|
||||
// 显示对话框
|
||||
Timber.d("权限被拒绝:" + deniedPermissions.toString());
|
||||
showAlert("以下权限被拒绝:\n" + deniedPermissions.toString());
|
||||
Timber.d("权限被拒绝:%s", deniedPermissions);
|
||||
showAlert("以下权限被拒绝:\n" + deniedPermissions);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -71,15 +72,27 @@ public class PermissionUtils {
|
|||
private void showAlert(String message) {
|
||||
new AlertDialog.Builder(activity)
|
||||
.setMessage(message)
|
||||
.setPositiveButton("确认", new DialogInterface.OnClickListener() {
|
||||
public void onClick(DialogInterface dialog, int which) {
|
||||
dialog.dismiss();
|
||||
}
|
||||
})
|
||||
.setPositiveButton("确认", (dialog, which) -> dialog.dismiss())
|
||||
.create()
|
||||
.show();
|
||||
}
|
||||
private void showToast(String message) {
|
||||
Toast.makeText(activity, message, Toast.LENGTH_SHORT).show();
|
||||
}
|
||||
|
||||
public boolean isNotificationServiceEnabled(Context context) {
|
||||
String packageName = context.getPackageName();
|
||||
String flat = Settings.Secure.getString(context.getContentResolver(), "enabled_notification_listeners");
|
||||
if (flat != null && !flat.isEmpty()) {
|
||||
final String[] activeListeners = flat.split(":");
|
||||
for (String activeListener : activeListeners) {
|
||||
ComponentName cn = ComponentName.unflattenFromString(activeListener);
|
||||
if (cn != null && cn.getPackageName().equals(packageName)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue