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

查看:13
本文介绍了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

这是我的基本代码 任何建议使其在运行时权限下工作提前致谢

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() 方法中试试这个代码

Try this code in your onClick() method

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" />

对于片段

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

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

checkSelfPermission()

ActivityCompat.checkSelfPermission()

也改变了

ActivityCompat.requestPermissions()

requestPermissions()

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

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