在Android上单击按钮进行电话呼叫

时间:2017-03-21 06:06:44

标签: android android-intent

我已经在我的应用程序中实现了一个电话服务,当我们点击该按钮时它会拨打电话,但问题是它不能支持某些设备上的所有设备它运行良好而在某些设备中它不是现在工作为我提供了解决方法,使其适用于所有设备。 这是我的代码。

public class SOSCallHelp extends AppCompatActivity {
private Button call1;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_soscall_help);
    call1=(Button)findViewById(R.id.call1);

  call1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent callIntent = new Intent(Intent.ACTION_CALL);
            callIntent.setData(Uri.parse("tel:9078784565"));
            if (ActivityCompat.checkSelfPermission(SOSCallHelp.this,
                    Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED)

            {
                return;
            }
            startActivity(callIntent);
        }
    });

5 个答案:

答案 0 :(得分:0)

请确保在App Manifest中为Pre-Lollipop设备添加以下权限。

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

答案 1 :(得分:0)

申请许可

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

致电

        Intent callIntent = new Intent(Intent.ACTION_CALL);
        callIntent.setData(Uri.parse("tel:9078784565"));
        if (ActivityCompat.checkSelfPermission(SOSCallHelp.this,
                Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED)

        {
            return;
        }
        startActivity(callIntent);

答案 2 :(得分:0)

       try this:

    final int PERMISSION_REQUEST_CODE = 111;

       if (driverMobile != null) {
            if (Build.VERSION.SDK_INT >= 23) {
                // Marshmallow+
                if (!checkCallPhonePermission() || !checkReadStatePermission()) {
                    requestPermission();
                } else {
                    Intent callIntent = new Intent(Intent.ACTION_CALL);
                    callIntent.setData(Uri.parse("tel:" + 1234567899));
                    startActivity(callIntent);
                }
            } else {
                Intent callIntent = new Intent(Intent.ACTION_CALL);
                callIntent.setData(Uri.parse("tel:" + + 1234567899));
                startActivity(callIntent);
            }
        } else {

        }

//权限

  private void requestPermission() {
    ActivityCompat.requestPermissions(this, new String[]
{Manifest.permission.CALL_PHONE, Manifest.permission.READ_PHONE_STATE}, 
PERMISSION_REQUEST_CODE);
}


 private void requestPermissions() {
    ActivityCompat.requestPermissions(this, new String[]
 {Manifest.permission.CALL_PHONE, Manifest.permission.READ_PHONE_STATE}, 
 PERMISSION_REQUEST_CODES);
} 


 private boolean checkCallPhonePermission() {
    int result = ContextCompat.checkSelfPermission(this, Manifest.permission.CALL_PHONE);
    if (result == PackageManager.PERMISSION_GRANTED) {
        return true;
    } else {
        return false;
    }
}

private boolean checkReadStatePermission() {
    int result = ContextCompat.checkSelfPermission(this, Manifest.permission.READ_PHONE_STATE);
    if (result == PackageManager.PERMISSION_GRANTED) {
        return true;
    } else {
        return false;
    }
}



  @Override
 public void onRequestPermissionsResult(int requestCode, String 
 permissions[], int[] grantResults) {
    switch (requestCode)
    {
        case PERMISSION_REQUEST_CODE:
            if (grantResults.length > 0 && grantResults[0] == 
 PackageManager.PERMISSION_GRANTED) {
                Intent callIntent = new Intent(Intent.ACTION_CALL);
                callIntent.setData(Uri.parse("tel:" + 123456789
                startActivity(callIntent);
            }
            break;
  }

  }

答案 3 :(得分:0)

请改为 -

public class SOSCallHelp extends AppCompatActivity {
    private Button call1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_soscall_help);
        call1 = (Button) findViewById(R.id.call1);

        call1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent callIntent = new Intent(Intent.ACTION_CALL);
                callIntent.setData(Uri.parse("tel:9078784565"));
                if (ActivityCompat.checkSelfPermission(SOSCallHelp.this,
                        Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED)

                {
                    return;
                }
                PackageManager packageManager = getActivity().getPackageManager();
                if (callIntent.resolveActivity(packageManager) != null) {
                    startActivity(callIntent);
                } else {
                    Log.d("SOSCallHelp", "No Intent available to handle this action");
                }


            }
        });
    }
}

答案 4 :(得分:0)

只需使用:

 private static int REQUEST_CALL_PHONE = 111;

 private void requestCallPhonePermission() {
    String callPermission = android.Manifest.permission.CALL_PHONE;
    int hasPermission = ContextCompat.checkSelfPermission(SOSCallHelp.this, callPermission);
    String[] permissions = new String[]{callPermission};
    if (hasPermission != PackageManager.PERMISSION_GRANTED) {
        requestPermissions(permissions, REQUEST_CALL_PHONE);
    } else {
        Intent intent = new Intent(Intent.ACTION_CALL);
        intent.setData(Uri.parse("tel:9078784565"));
        startActivity(intent);
    }
}

@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);

    if (requestCode == REQUEST_CALL_PHONE) {
        if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {

            Intent intent = new Intent(Intent.ACTION_CALL);
            intent.setData(Uri.parse("tel:9078784565"));
            startActivity(intent);
        } else {
            Toast.makeText(SOSCallHelp.this, "CALL_PHONE DENIED", Toast.LENGTH_SHORT).show();

        }
    }

}

现在点击按钮

调用此requestCallPhonePermission()函数

在清单文件中添加<uses-permission android:name="android.permission.CALL_PHONE" />  权限。