How to Make a Call directly in Android

后端 未结 6 2200
囚心锁ツ
囚心锁ツ 2020-12-21 05:23

I know it was very simple to do it but I come across a very strange issue. I have to call Police in danger Situation by just tapping a button. So I have used following code

相关标签:
6条回答
  • 2020-12-21 05:40

    There are two intents to call/start calling: ACTION_CALL and ACTION_DIAL. ACTION_DIAL will only open the dialer with the number filled in, but allows the user to actually call or reject the call. ACTION_CALL will immediately call the number and requires an extra permission. So make sure you have the permission

    0 讨论(0)
  • 2020-12-21 05:47

    From the documentation of ACTION_CALL:

    Note: there will be restrictions on which applications can initiate a call; most applications should use the ACTION_DIAL.

    Note: this Intent cannot be used to call emergency numbers. Applications can dial emergency numbers using ACTION_DIAL, however.

    So it seems this behavior is on purpose.

    0 讨论(0)
  • 2020-12-21 05:57

    There could be a problem that the android system doesnt recognize 100 as a valid phone number, instead if you put the country code before it then it works fine. TO solve such issue take a look at this library libnhonenumber. You could use it something like this

    public static ArrayList<String> extractPhoneNumber(String content) {
    
        ArrayList<String> numbers = new ArrayList<String>(0);
    
        PhoneNumberUtil instance = PhoneNumberUtil.getInstance();
    
        //Change IT with your contry code
        Iterable<PhoneNumberMatch> matches = instance.findNumbers(content, "IT");
    
        Iterator<PhoneNumberMatch> iterator = matches.iterator();
    
        while (iterator.hasNext()) {
            numbers.add(instance.format(iterator.next().number(), PhoneNumberFormat.INTERNATIONAL));
        }
    
        return numbers;
    }
    
    0 讨论(0)
  • 2020-12-21 05:57

    A Long time passed. But may help someone else. If you want to call directly, you should use requestPermissions method.

    1. Add this line to your manifest file:

    <uses-permission android:name="android.permission.CALL_PHONE" />
    

    2. Define a class variable in the activity class:

    private static Intent phoneCallIntent; //If use don't need a member variable is good to use a static variable for memory performance.
    

    3. Add these lines to the onCreate method of the activity:

    final String permissionToCall = Manifest.permission.CALL_PHONE;
    //Assume that you have a phone icon.
    (findViewById(R.id.menuBarPhone)).setOnClickListener(new OnClickListener(){
        public void onClick(View view) {
            phoneCallIntent = new Intent(Intent.ACTION_CALL);
            phoneCallIntent.setData(Uri.parse(getString(R.string.callNumber))); //Uri.parse("tel:your number")
            if (ActivityCompat.checkSelfPermission(MainFrame.this, permissionToCall) != PackageManager.PERMISSION_GRANTED) {
                ActivityCompat.requestPermissions(MainFrame.this, new String[]{permissionToCall}, 1);
                return;
            }
            startActivity(phoneCallIntent);
        }
    });
    

    4. And for making a call immediately after clicking on Allow button, override onRequestPermissionsResult method:

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults){
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if(requestCode == 1){
            final int permissionsLength = permissions.length;
            for (int i = 0; i < permissionsLength; i++) {
                if(grantResults[i] == PackageManager.PERMISSION_GRANTED){
                    startActivity(phoneCallIntent);
                }
            }
        }
    

    When a user give the permission, next time there will be no dialogue box and call will be make directly.

    0 讨论(0)
  • 2020-12-21 06:00

    Best way to directly call without user intervention..

    String uri = "tel:" + num.trim();
    Intent intent = new Intent(Intent.ACTION_CALL);
    intent.setData(Uri.parse(uri));
    startActivity(intent);
    
    0 讨论(0)
  • 2020-12-21 06:06
    private void phoneCall()
    {
     String phoneCallUri = "tel:91";
     Intent phoneCallIntent = new Intent(Intent.ACTION_CALL);
     phoneCallIntent.setData(Uri.parse(phoneCallUri));
     startActivity(phoneCallIntent);
    }
    
    0 讨论(0)
提交回复
热议问题