Advertisement
Guest User

Untitled

a guest
Aug 21st, 2017
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.47 KB | None | 0 0
  1. public class MainActivity extends Activity {
  2.  
  3. private Button button;
  4. private EditText etPhoneno;
  5.  
  6. public void onCreate(Bundle savedInstanceState) {
  7.  
  8. super.onCreate(savedInstanceState);
  9. setContentView(R.layout.activity_main);
  10.  
  11. button = (Button) findViewById(R.id.buttonCall);
  12. etPhoneno = (EditText) findViewById(R.id.editText1);
  13. // add button listener
  14. button.setOnClickListener(new View.OnClickListener() {
  15.  
  16. @Override
  17. public void onClick(View arg0) {
  18. String phnum = etPhoneno.getText().toString();
  19. Intent callIntent = new Intent(Intent.ACTION_CALL);
  20. callIntent.setData(Uri.parse("tel:" + phnum));
  21. startActivity(callIntent);
  22. }
  23. });
  24. }
  25.  
  26. if(isPermissionGranted()){
  27. call_action();
  28. }
  29.  
  30. public void call_action(){
  31. String phnum = etPhoneno.getText().toString();
  32. Intent callIntent = new Intent(Intent.ACTION_CALL);
  33. callIntent.setData(Uri.parse("tel:" + phnum));
  34. startActivity(callIntent);
  35. }
  36.  
  37. public boolean isPermissionGranted() {
  38. if (Build.VERSION.SDK_INT >= 23) {
  39. if (checkSelfPermission(android.Manifest.permission.CALL_PHONE)
  40. == PackageManager.PERMISSION_GRANTED) {
  41. Log.v("TAG","Permission is granted");
  42. return true;
  43. } else {
  44.  
  45. Log.v("TAG","Permission is revoked");
  46. ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CALL_PHONE}, 1);
  47. return false;
  48. }
  49. }
  50. else { //permission is automatically granted on sdk<23 upon installation
  51. Log.v("TAG","Permission is granted");
  52. return true;
  53. }
  54. }
  55.  
  56.  
  57. @Override
  58. public void onRequestPermissionsResult(int requestCode,
  59. String permissions[], int[] grantResults) {
  60. switch (requestCode) {
  61.  
  62. case 1: {
  63.  
  64. if (grantResults.length > 0
  65. && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
  66. Toast.makeText(getApplicationContext(), "Permission granted", Toast.LENGTH_SHORT).show();
  67. call_action();
  68. } else {
  69. Toast.makeText(getApplicationContext(), "Permission denied", Toast.LENGTH_SHORT).show();
  70. }
  71. return;
  72. }
  73.  
  74. // other 'case' lines to check for other
  75. // permissions this app might request
  76. }
  77. }
  78.  
  79. <uses-permission android:name="android.permission.CALL_PHONE" />
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement