android studio调用按钮

Dre*_*ett 1 java android call button

我正在尝试按下单击按钮,按下时会拨打电话.这是我的java代码:

public void CampusSafClick(View view){
    Intent callIntent =new Intent(Intent.ACTION_CALL);
    callIntent.setData(Uri.parse("tel:6038994210"));
    startActivity(callIntent);
}
Run Code Online (Sandbox Code Playgroud)

我知道如何制作onclick按钮,所以这不是问题.

我在清单中有这个代码:

<uses-permission android:name="android.permision.CALL_PHONE"></uses-permission>
Run Code Online (Sandbox Code Playgroud)

我一直收到错误,不幸的是你的应用已经停止工作了.

Nik*_*vic 9

这是工作代码:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            dialContactPhone("123123123");
        }
    });
}

private void dialContactPhone(final String phoneNumber) {
    startActivity(new Intent(Intent.ACTION_DIAL, Uri.fromParts("tel", phoneNumber, null)));
}
Run Code Online (Sandbox Code Playgroud)


小智 1

ivCall.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        if (serviceListDates.get(position).getUser_mobile().length() != 0) {
            final AlertDialog.Builder alertDialog = new AlertDialog.Builder(mContext);
            alertDialog.setTitle("NKA SERVICE");
            alertDialog.setMessage("Do you want to Call ?");
            alertDialog.setIcon(R.drawable.call_icon);
            alertDialog.setPositiveButton("OK", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    ((DeliveredServiceOilActivity) mContext).callPhoneNumber
                            (serviceListDates.get(position).getUser_mobile());
                }
            });`enter code here
            alertDialog.setNegativeButton("NO", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    dialog.cancel();
                }
            });
            alertDialog.show();
        } else
            AlertUtils.SHOW_TOAST(mContext, mContext.getString(R.string.please_add_number));
    }
});
Run Code Online (Sandbox Code Playgroud)