android请求运行时调用操作的权限 [英] android request runtime permission to call action

查看:280
本文介绍了android请求运行时调用操作的权限的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有调用操作的代码,我需要最好的方式来声明运行时权限我尝试了很多代码但我总是收到错误

i have code for call action and i need best way to declare run time permission ive tried many codes but i always get error

这里是我的基本代码任何建议for make it with run permission
提前感谢

here is my basic code any suggestion for make it work with runtime permission thanks in advance

public class MainActivity extends Activity {

private Button button;
private EditText etPhoneno;

public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    button = (Button) findViewById(R.id.buttonCall);
    etPhoneno = (EditText) findViewById(R.id.editText1);
    // add button listener
    button.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {
                String phnum = etPhoneno.getText().toString();
                Intent callIntent = new Intent(Intent.ACTION_CALL);
                callIntent.setData(Uri.parse("tel:" + phnum));
                startActivity(callIntent);
        }
    });
}

}

推荐答案

onClick()方法中试用此代码

if(isPermissionGranted()){
     call_action();
}

现在,要调用创建一个单独的方法:

Now, to call create a separate method:

public void call_action(){
    String phnum = etPhoneno.getText().toString();
    Intent callIntent = new Intent(Intent.ACTION_CALL);
    callIntent.setData(Uri.parse("tel:" + phnum));
    startActivity(callIntent);
   }

为运行时权限检查添加以下两种方法:

Add these two methods for Runtime Permission Checks:

 public  boolean isPermissionGranted() {
    if (Build.VERSION.SDK_INT >= 23) {
        if (checkSelfPermission(android.Manifest.permission.CALL_PHONE)
                == PackageManager.PERMISSION_GRANTED) {
            Log.v("TAG","Permission is granted");
            return true;
        } else {

            Log.v("TAG","Permission is revoked");
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CALL_PHONE}, 1);
            return false;
        }
    }
    else { //permission is automatically granted on sdk<23 upon installation
        Log.v("TAG","Permission is granted");
        return true;
    }
}


 @Override
public void onRequestPermissionsResult(int requestCode,
                                       String permissions[], int[] grantResults) {
    switch (requestCode) {

        case 1: {

            if (grantResults.length > 0
                    && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                Toast.makeText(getApplicationContext(), "Permission granted", Toast.LENGTH_SHORT).show();
                call_action();
            } else {
                Toast.makeText(getApplicationContext(), "Permission denied", Toast.LENGTH_SHORT).show();
            }
            return;
        }

        // other 'case' lines to check for other
        // permissions this app might request
    }
 }

还要确保将其添加到清单中:

Also make sure to add this into the manifest:

<uses-permission android:name="android.permission.CALL_PHONE" />

FOR FRAGMENT

如果您在 片段 中尝试此代码,请更改

If you are trying this code in a fragment, change the

checkSelfPermission()

ActivityCompat.checkSelfPermission()

ActivityCompat.checkSelfPermission()

并且还会更改

ActivityCompat.requestPermissions()

to

requestPermissions()

requestPermissions()

这篇关于android请求运行时调用操作的权限的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆