Android6.0动态处理权限BaseActivity封装

    科技2022-07-10  196

    import android.app.Activity; import android.app.AlertDialog; import android.content.DialogInterface; import android.content.pm.PackageManager; import android.os.Build; import androidx.annotation.NonNull; import androidx.appcompat.app.AppCompatActivity; import androidx.core.app.ActivityCompat; import androidx.core.content.ContextCompat; import java.util.ArrayList; import java.util.List; public class BaseActivity extends AppCompatActivity { public final static int PERMISSION_CODE = 10001; private PermissionCallback mPermissionCallback; public boolean checkPermissions(String... permissions) { for (String p : permissions) { if (ContextCompat.checkSelfPermission(this, p) != PackageManager.PERMISSION_GRANTED) { return false; } } return true; } public void requestPermission(PermissionCallback callback, final String... permissions) { if (permissions == null || permissions.length == 0) { return; } this.mPermissionCallback = callback; // 如果设备的系统版本小于23,那么不调用授权对话框 if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M || checkPermissions(permissions)) { if (mPermissionCallback != null) { mPermissionCallback.hasPermission(); mPermissionCallback = null; } return; } if (shouldShowRequestPermissionRationale(permissions)) { /*1. 第一次请求权限时,用户拒绝了,下一次:shouldShowRequestPermissionRationale() 返回 true,应该显示一些为什么需要这个权限的说明 2. 第二次请求权限时,用户拒绝了,并选择了“不在提醒”的选项时:shouldShowRequestPermissionRationale() 返回 false 3. 设备的策略禁止当前应用获取这个权限的授权:shouldShowRequestPermissionRationale() 返回 false*/ //如果用户之前拒绝过此权限,再提示一次准备授权相关权限 new AlertDialog.Builder(this) .setTitle("提示").setMessage("需要这些权限才能正常运行") .setPositiveButton("授权", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { ActivityCompat.requestPermissions(getContext(), permissions, PERMISSION_CODE); } }).show(); } else { ActivityCompat.requestPermissions(this, permissions, PERMISSION_CODE); } } public boolean shouldShowRequestPermissionRationale(String... permissions) { for (String p : permissions) { if (ActivityCompat.shouldShowRequestPermissionRationale(this, p)) { return true; } } return false; } private ArrayList<String> verifyPermission(@NonNull String[] permissions, @NonNull int[] grantResults) { ArrayList<String> deniedPermissions = new ArrayList<>(); // 检查有哪些权限被拒绝授权 for (int i = 0; i < permissions.length; i++) { if (grantResults[i] == PackageManager.PERMISSION_DENIED) { deniedPermissions.add(permissions[i]); } } return deniedPermissions; } @Override public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { super.onRequestPermissionsResult(requestCode, permissions, grantResults); if (PERMISSION_CODE == requestCode) { List<String> deniedPermissions = verifyPermission(permissions, grantResults); if (mPermissionCallback != null) { if (deniedPermissions.size() == 0) { mPermissionCallback.hasPermission(); } else { mPermissionCallback.noPermission(deniedPermissions.toArray(new String[deniedPermissions.size()])); } mPermissionCallback = null; } } } public Activity getContext() { return this; } interface PermissionCallback { void hasPermission(); void noPermission(String... deniedPermissions); } }

     

    使用案例:发送短信

    send_sms.xml

    <?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginStart="32dp" android:layout_marginLeft="32dp" android:layout_marginTop="76dp" android:text="电话号码:" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> <EditText android:id="@+id/phoneNumber" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginStart="24dp" android:layout_marginLeft="24dp" android:layout_marginTop="52dp" android:layout_marginEnd="76dp" android:layout_marginRight="76dp" android:ems="10" android:hint="请输入" android:inputType="textPersonName" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintHorizontal_bias="0.0" app:layout_constraintStart_toEndOf="@+id/textView2" app:layout_constraintTop_toTopOf="parent" /> <TextView android:id="@+id/textView3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginStart="60dp" android:layout_marginLeft="60dp" android:layout_marginTop="52dp" android:text="信息:" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/textView2" /> <EditText android:id="@+id/message" android:layout_width="212dp" android:layout_height="129dp" android:layout_marginStart="24dp" android:layout_marginLeft="24dp" android:layout_marginTop="36dp" android:ems="10" android:gravity="start|top" android:hint="请输入" android:inputType="textMultiLine" app:layout_constraintStart_toEndOf="@+id/textView3" app:layout_constraintTop_toBottomOf="@+id/phoneNumber" /> <Button android:id="@+id/send" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginStart="248dp" android:layout_marginLeft="248dp" android:layout_marginTop="68dp" android:onClick="handleSend" android:text="发送" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/message" /> </androidx.constraintlayout.widget.ConstraintLayout> import android.Manifest; import android.os.Bundle; import android.telephony.SmsManager; import android.view.View; import android.widget.EditText; import android.widget.Toast; import androidx.annotation.Nullable; import java.util.ArrayList; import java.util.Arrays; public class PermisssionActivity extends BaseActivity { @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.send_sms); } public void sendSms(String phone, String msg) { SmsManager smsManager = SmsManager.getDefault(); ArrayList<String> stringList = smsManager.divideMessage(msg); for (String cotentFragment : stringList) { smsManager.sendTextMessage(phone, null, cotentFragment, null, null); } Toast.makeText(this, "发送成功", Toast.LENGTH_SHORT).show(); } public void handleSend(View view) { final EditText phoneNumber = findViewById(R.id.phoneNumber); final EditText message = findViewById(R.id.message); requestPermission(new PermissionCallback() { @Override public void hasPermission() { sendSms(phoneNumber.getText().toString().trim(), message.getText().toString().trim()); } @Override public void noPermission(String... deniedPermissions) { Toast.makeText(getContext(), "没有授权:" + Arrays.toString(deniedPermissions), Toast.LENGTH_LONG).show(); } }, Manifest.permission.SEND_SMS); } }

     

    Processed: 0.025, SQL: 8