Android的动态权限申请实例
程序员文章站
2022-03-25 18:46:09
...
- Android 6.0以上的手机使用某些功能时,需要动态申请权限,即弹出对话框,请用户同意开通某项权限,即使在AndroidManifest.xml里面有声明了有关的uses-permission。Android说明为Dangerous的权限包括以下几个,本实例在程序中动态申请,并反馈申请权限的结果。
- AndroidManifest.xm
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
在这里插入代码片
- MainActivity.java
//针对Android6.0以上系统,测试手机是否可以动态申请权限。
package com.lmy.uses_permission;
import android.Manifest;
import android.content.pm.PackageManager;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
private static final int REQUEST_WRITE_EXTERNAL_STORAGE = 100;
private static final int REQUEST_READ_EXTERNAL_STORAGE = 200;
private static final int REQUEST_READ_PHONE_STATE = 300;
private static final int REQUEST_CALL_PHONE = 400;
private static final int REQUEST_READ_CONTACTS = 500;
private static final int REQUEST_ACCESS_COARSE_LOCATION = 600;
private static final int REQUEST_ACCESS_FINE_LOCATION = 700;
private static final int REQUEST_RECORD_AUDIO = 800;
private static final int REQUEST_CAMERA = 900;
private Button btn1,btn2,btn3,btn4,btn5,btn6,btn7,btn8,btn9;
int mypermission;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btn1 = findViewById(R.id.button1);
btn2 = findViewById(R.id.button2);
btn3 = findViewById(R.id.button3);
btn4 = findViewById(R.id.button4);
btn5 = findViewById(R.id.button5);
btn6 = findViewById(R.id.button6);
btn7 = findViewById(R.id.button7);
btn8 = findViewById(R.id.button8);
btn9 = findViewById(R.id.button9);
mypermission = ActivityCompat.checkSelfPermission(MainActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE);
if (mypermission == PackageManager.PERMISSION_GRANTED){
btn1.setText("有:WRITE_EXTERNAL_STORAGE");
};
mypermission = ActivityCompat.checkSelfPermission(MainActivity.this, Manifest.permission.READ_EXTERNAL_STORAGE);
if (mypermission == PackageManager.PERMISSION_GRANTED){
btn2.setText("有:READ_EXTERNAL_STORAGE");
};
mypermission = ActivityCompat.checkSelfPermission(MainActivity.this, Manifest.permission.READ_PHONE_STATE);
if (mypermission == PackageManager.PERMISSION_GRANTED){
btn3.setText("有:READ_PHONE_STATE");
};
mypermission = ActivityCompat.checkSelfPermission(MainActivity.this, Manifest.permission.CALL_PHONE);
if (mypermission == PackageManager.PERMISSION_GRANTED){
btn4.setText("有:CALL_PHONE");
};
mypermission = ActivityCompat.checkSelfPermission(MainActivity.this,Manifest.permission.READ_CONTACTS);
if (mypermission == PackageManager.PERMISSION_GRANTED){
btn5.setText("有:READ_CONTACTS");
};
mypermission = ActivityCompat.checkSelfPermission(MainActivity.this,Manifest.permission.ACCESS_COARSE_LOCATION);
if (mypermission == PackageManager.PERMISSION_GRANTED){
btn6.setText("有:ACCESS_COARSE_LOCATION");
};
mypermission = ActivityCompat.checkSelfPermission(MainActivity.this,Manifest.permission.ACCESS_FINE_LOCATION);
if (mypermission == PackageManager.PERMISSION_GRANTED){
btn7.setText("有:ACCESS_FINE_LOCATION");
};
mypermission = ActivityCompat.checkSelfPermission(MainActivity.this,Manifest.permission.RECORD_AUDIO);
if (mypermission == PackageManager.PERMISSION_GRANTED){
btn8.setText("有:RECORD_AUDIO");
};
mypermission = ActivityCompat.checkSelfPermission(MainActivity.this,Manifest.permission.CAMERA);
if (mypermission == PackageManager.PERMISSION_GRANTED){
btn9.setText("有:CAMERA");
};
}
public void BtnonClick(View v){
int mypermission;
if (v==btn1) {
mypermission = ActivityCompat.checkSelfPermission(MainActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE);
if (mypermission != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
REQUEST_WRITE_EXTERNAL_STORAGE);
}else{
btn1.setText("有:WRITE_EXTERNAL_STORAGE");
Toast.makeText(getApplicationContext(),"已有权限",Toast.LENGTH_SHORT).show();
}
}else
if(v== btn2){
mypermission = ActivityCompat.checkSelfPermission(MainActivity.this, Manifest.permission.READ_EXTERNAL_STORAGE);
if (mypermission != PackageManager.PERMISSION_GRANTED ) {
ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, REQUEST_READ_EXTERNAL_STORAGE);
}else {
btn2.setText("有:READ_EXTERNAL_STORAGE");
Toast.makeText(getApplicationContext(),"已有权限",Toast.LENGTH_SHORT).show();
}
}else
if(v== btn3){
mypermission = ActivityCompat.checkSelfPermission(MainActivity.this, Manifest.permission.READ_PHONE_STATE);
if (mypermission != PackageManager.PERMISSION_GRANTED ) {
ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.READ_PHONE_STATE}, REQUEST_READ_PHONE_STATE);
}else {
btn3.setText("有:READ_PHONE_STATE");
Toast.makeText(getApplicationContext(),"已有权限",Toast.LENGTH_SHORT).show();
}
}else
if(v== btn4){
mypermission = ActivityCompat.checkSelfPermission(MainActivity.this, Manifest.permission.CALL_PHONE);
if (mypermission != PackageManager.PERMISSION_GRANTED ) {
ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.CALL_PHONE}, REQUEST_CALL_PHONE);
}else {
btn4.setText("有:CALL_PHONE");
Toast.makeText(getApplicationContext(),"已有权限",Toast.LENGTH_SHORT).show();
}
}else
if(v== btn5){
mypermission = ActivityCompat.checkSelfPermission(MainActivity.this, Manifest.permission.READ_CONTACTS);
if (mypermission != PackageManager.PERMISSION_GRANTED ) {
ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.READ_CONTACTS}, REQUEST_READ_CONTACTS);
}else {
btn5.setText("有:READ_CONTACTS");
Toast.makeText(getApplicationContext(),"已有权限",Toast.LENGTH_SHORT).show();
}
}else
if(v== btn6){
mypermission = ActivityCompat.checkSelfPermission(MainActivity.this, Manifest.permission.ACCESS_COARSE_LOCATION);
if (mypermission != PackageManager.PERMISSION_GRANTED ) {
ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.ACCESS_COARSE_LOCATION}, REQUEST_ACCESS_COARSE_LOCATION);
}else {
btn6.setText("有:ACCESS_COARSE_LOCATION");
Toast.makeText(getApplicationContext(),"已有权限",Toast.LENGTH_SHORT).show();
}
}else
if(v== btn7){
mypermission = ActivityCompat.checkSelfPermission(MainActivity.this, Manifest.permission.ACCESS_FINE_LOCATION);
if (mypermission != PackageManager.PERMISSION_GRANTED ) {
ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, REQUEST_ACCESS_FINE_LOCATION);
}else {
btn7.setText("有:ACCESS_FINE_LOCATION");
Toast.makeText(getApplicationContext(),"已有权限",Toast.LENGTH_SHORT).show();
}
}else
if(v== btn8){
mypermission = ActivityCompat.checkSelfPermission(MainActivity.this, Manifest.permission.RECORD_AUDIO);
if (mypermission != PackageManager.PERMISSION_GRANTED ) {
ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.RECORD_AUDIO}, REQUEST_RECORD_AUDIO);
}else {
btn8.setText("有:RECORD_AUDIO");
Toast.makeText(getApplicationContext(),"已有权限",Toast.LENGTH_SHORT).show();
}
}else
if(v== btn9){
mypermission = ActivityCompat.checkSelfPermission(MainActivity.this, Manifest.permission.CAMERA);
if (mypermission != PackageManager.PERMISSION_GRANTED ) {
ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.CAMERA}, REQUEST_CAMERA);
}else {
btn9.setText("有:CAMERA");
Toast.makeText(getApplicationContext(),"已有权限",Toast.LENGTH_SHORT).show();
}
}
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if ((grantResults.length>0)&&(grantResults[0]==PackageManager.PERMISSION_GRANTED)){
switch (requestCode) {
case REQUEST_WRITE_EXTERNAL_STORAGE:
btn1.setText("有:WRITE_EXTERNAL_STORAGE");
break;
case REQUEST_READ_EXTERNAL_STORAGE:
btn2.setText("有:READ_EXTERNAL_STORAGE");
break;
case REQUEST_READ_PHONE_STATE:
btn3.setText("有:READ_PHONE_STATE");
break;
case REQUEST_CALL_PHONE:
btn4.setText("有:CALL_PHONE");
break;
case REQUEST_READ_CONTACTS:
btn5.setText("有:READ_CONTACTS");
break;
case REQUEST_ACCESS_COARSE_LOCATION:
btn6.setText("有:ACCESS_COARSE_LOCATION");
break;
case REQUEST_ACCESS_FINE_LOCATION:
btn7.setText("有:ACCESS_FINE_LOCATION");
break;
case REQUEST_RECORD_AUDIO:
btn8.setText("有:RECORD_AUDIO");
break;
case REQUEST_CAMERA:
btn9.setText("有:CAMERA");
break;
}
Toast.makeText(getApplicationContext(),"权限申请成功",Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(getApplicationContext(),"权限申请失败",Toast.LENGTH_SHORT).show();
}
}
}
在这里插入代码片
上一篇: UbuntuPhone应用开发社区上线,核心应用设计征集中
下一篇: 4-5 类变量和实例变量
推荐阅读
-
Android获取定位权限,获取设备所在的经纬度
-
Android中Activity之间跳转和参数传递的实例
-
Android动态模糊效果的快速实现方法
-
ImageView点击可变暗的实例代码(android代码技巧)
-
实例详解Android Selector和Shape的用法
-
7种形式的Android Dialog使用实例
-
Android中获取资源 id 及资源 id 的动态获取
-
Android listview ExpandableListView实现多选,单选,全选,edittext实现批量输入的实例代码
-
实例详解Android自定义ProgressDialog进度条对话框的实现
-
java使用动态代理来实现AOP(日志记录)的实例代码