【导读】
为了方便Android 6.0的权限申请,记录两个工具类的使用。一是通过单独的activity去申请,一是通过BaseActivity去申请。
【步骤】
//布局代码
import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import android.provider.Settings;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import com.technology.myapplication.R;
import com.technology.myapplication.utils.LogUtils;
import java.util.ArrayList;
import java.util.List;
public class PermissionActivity extends AppCompatActivity {
private final String TAG = "PermissionActivity";
/**
* 1. 摄像头: android.permission-group.CAMERA
* android.permission.CAMERA
*
* 2.定位:android.permission-group.LOCATION
* android.permission.ACCESS_FINE_LOCATION
* android.permission.ACCESS_COARSE_LOCATION
*
* 3.麦克风:android.permission-group.MICROPHONE
* android.permission.RECORD_AUDIO
*
* 4.设备相关信息:android.permission-group.PHONE
* android.permission.READ_PHONE_STATE
* android.permission.CALL_PHONE
* android.permission.READ_CALL_LOG
* android.permission.WRITE_CALL_LOG
* com.android.voicemail.permission.ADD_VOICEMAIL
* android.permission.USE_SIP
* android.permission.PROCESS_OUTGOING_CALLS
*
* 5.存储空间:android.permission-group.STORAGE
* android.permission.READ_EXTERNAL_STORAGE
* android.permission.WRITE_EXTERNAL_STORAGE
*
* 6.传感器: group:android.permission-group.SENSORS
* permission:android.permission.BODY_SENSORS
*/
private final String[] mStrings = {
Manifest.permission.CAMERA,
Manifest.permission.ACCESS_FINE_LOCATION,
Manifest.permission.ACCESS_COARSE_LOCATION,
Manifest.permission.RECORD_AUDIO,
Manifest.permission.READ_PHONE_STATE,
Manifest.permission.CALL_PHONE,
Manifest.permission.READ_CALL_LOG,
Manifest.permission.WRITE_CALL_LOG,
// Manifest.permission.ADD_VOICEMAIL,
Manifest.permission.USE_SIP,
// Manifest.permission.PROCESS_OUTGOING_CALLS,
Manifest.permission.READ_EXTERNAL_STORAGE,
Manifest.permission.WRITE_EXTERNAL_STORAGE,
Manifest.permission.BODY_SENSORS,
Manifest.permission.CHANGE_WIFI_STATE
// Manifest.permission.WRITE_SETTINGS
};
private LinearLayout mLinearLayout;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_permission);
mLinearLayout = findViewById(R.id.ll_layout);
findViewById(R.id.bt_quit).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
finish();
}
});
findViewById(R.id.bt_request).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startSystem();
}
});
check();
}
private void check() {
List data = new ArrayList<>();
for (String permission : mStrings) {
if (ActivityCompat.checkSelfPermission(PermissionActivity.this, permission) != PackageManager.PERMISSION_GRANTED) {
//没有权限,申请权限
data.add(permission);
} else {
addView(permission, "已经申请过了");
}
}
String[] strings = new String[data.size()];
data.toArray(strings);
LogUtils.e(TAG, "需要申请的权限 = " + strings.length);
//申请权限,其中RC_PERMISSION是权限申请码,用来标志权限申请的
if (!data.isEmpty()) {
ActivityCompat.requestPermissions(PermissionActivity.this, strings, 101);
} else {
start();
}
}
private void start() {
LogUtils.e(TAG, "--申请权限完成,调整界面--");
}
private void addView(String permission, String title) {
TextView textView = new TextView(this);
textView.setPadding(20, 20, 0, 0);
String s = "失败";
if (!title.contains(s)) {
textView.setTextColor(Color.BLACK);
} else {
textView.setTextColor(Color.RED);
}
textView.setTextSize(16);
textView.setText(permission + ":" + title);
mLinearLayout.addView(textView);
}
private void startSystem() {
Intent intent = new Intent();
intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
Uri uri = Uri.fromParts("package", this.getPackageName(), null);
intent.setData(uri);
startActivity(intent);
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
int code = 101;
if (requestCode == code) {
int length = permissions.length;
int start = 0;
for (int i = 0; i < length; i++) {
String permission = permissions[i];
if (grantResults[i] == PackageManager.PERMISSION_GRANTED) {
//成功
LogUtils.e(TAG, "权限申请成功 = " + permission);
addView(permission, "权限申请成功");
start ++;
} else {
//不成功
LogUtils.e(TAG, "权限申请失败 = " + permission);
addView(permission, "权限申请失败");
}
}
if (start == length){
start();
}
}
}
}
【Base封装】
清单文件权限的申请,参考上面
布局代码,参考上面
import android.content.pm.PackageManager;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.ContextCompat;
public class BasePermissionActivity extends AppCompatActivity {
public int requestCode = 101;
public void showToast(String toast) {
Toast.makeText(this, toast, Toast.LENGTH_LONG).show();
}
/**
* 权限检查
*
* @param neededPermissions 需要的权限
* @return 是否全部被允许
*/
public boolean checkPermissions(String[] neededPermissions) {
if (neededPermissions == null) {
return true;
}
boolean allGranted = true;
for (String permission : neededPermissions) {
boolean p = ContextCompat.checkSelfPermission(this, permission) == PackageManager.PERMISSION_GRANTED;
allGranted &= p;
}
return allGranted;
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (this.requestCode == requestCode) {
boolean isAllGranted = true;
for (int grantResult : grantResults) {
isAllGranted &= (grantResult == PackageManager.PERMISSION_GRANTED);
}
if (isAllGranted) {
permissionsResult();
} else {
showToast("权限被拒绝");
}
}
}
public void permissionsResult() {
}
}
import android.Manifest;
import android.os.Bundle;
import com.technology.myapplication.utils.LogUtils;
public class PermissionItemActivity extends BasePermissionActivity {
private final String TAG = "PermissionItemActivity";
private final String[] mStrings = {
Manifest.permission.CAMERA,
Manifest.permission.ACCESS_FINE_LOCATION,
Manifest.permission.ACCESS_COARSE_LOCATION,
Manifest.permission.RECORD_AUDIO,
Manifest.permission.READ_PHONE_STATE,
Manifest.permission.CALL_PHONE,
Manifest.permission.READ_CALL_LOG,
Manifest.permission.WRITE_CALL_LOG,
Manifest.permission.USE_SIP,
Manifest.permission.READ_EXTERNAL_STORAGE,
Manifest.permission.WRITE_EXTERNAL_STORAGE,
Manifest.permission.BODY_SENSORS,
Manifest.permission.CHANGE_WIFI_STATE
};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
boolean p = checkPermissions(mStrings);
if (p) {
start();
} else {
requestPermissions(mStrings, requestCode);
}
}
@Override
public void permissionsResult() {
start();
}
private void start() {
LogUtils.e(TAG, "--申请权限完成,调整界面--");
}
}
【Kotlin】
清单文件权限的申请,参考上面
布局代码,参考上面
import android.annotation.SuppressLint
import android.content.pm.PackageManager
import android.widget.Toast
import androidx.annotation.Nullable
import androidx.core.content.ContextCompat
import androidx.fragment.app.FragmentActivity
@SuppressLint("Registered")
abstract class BasePermissionActivity : FragmentActivity() {
protected val requestCode: Int = 101
fun showToast(@Nullable resId: Int) {
showToast(getString(resId))
}
fun showToast(toast: String) {
Toast.makeText(this, toast, Toast.LENGTH_LONG).show()
}
/**
* 权限检查
*
* @param neededPermissions 需要的权限
* @return 是否全部被允许
*/
fun checkPermissions(neededPermissions: Array): Boolean {
if (neededPermissions.isEmpty()) {
return true
}
var allGranted = true
for (neededPermission in neededPermissions) {
val n = ContextCompat.checkSelfPermission(
this,
neededPermission
) == PackageManager.PERMISSION_GRANTED
allGranted = allGranted and n
}
return allGranted
}
override fun onRequestPermissionsResult(
requestCode: Int,
permissions: Array,
grantResults: IntArray
) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults)
var isAllGranted = true
for (grantResult in grantResults) {
isAllGranted = isAllGranted and (grantResult == PackageManager.PERMISSION_GRANTED)
}
if (this.requestCode == requestCode) {
if (isAllGranted) {
permissionsResult()
} else {
showToast("权限被拒绝")
}
}
}
open fun permissionsResult() {
}
companion object {
private const val TAG = "BasePermissionActivity"
}
}