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);
}
});
}
}
Try this code in your
onClick()
methodNow, to call create a separate method:
Add these two methods for Runtime Permission Checks:
Also make sure to add this into the manifest:
FOR FRAGMENT
If you are trying this code in a
fragment
, change thecheckSelfPermission()
to
ActivityCompat.checkSelfPermission()
and also change
ActivityCompat.requestPermissions()
to
requestPermissions()