android request runtime permission to call action [closed]

匿名 (未验证) 提交于 2019-12-03 02:16:02

问题:

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

}

回答1:

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

FOR FRAGMENT

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

checkSelfPermission()

to

ActivityCompat.checkSelfPermission()

and also change

ActivityCompat.requestPermissions()

to

requestPermissions()



易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!