Java android请求运行时权限调用动作

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/42057040/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 23:54:27  来源:igfitidea点击:

android request runtime permission to call action

javaandroidpermissionsruntime

提问by Ahmed Samir

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);
        }
    });
}

}

}

采纳答案by rafsanahmad007

Try this code in your onClick()method

在您的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

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

checkSelfPermission()

checkSelfPermission()

to

ActivityCompat.checkSelfPermission()

ActivityCompat.checkSelfPermission()

and also change

并且也改变

ActivityCompat.requestPermissions()

ActivityCompat.requestPermissions()

to

requestPermissions()

requestPermissions()