Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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);
- }
- });
- }
- if(isPermissionGranted()){
- call_action();
- }
- public void call_action(){
- String phnum = etPhoneno.getText().toString();
- Intent callIntent = new Intent(Intent.ACTION_CALL);
- callIntent.setData(Uri.parse("tel:" + phnum));
- startActivity(callIntent);
- }
- 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
- }
- }
- <uses-permission android:name="android.permission.CALL_PHONE" />
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement