如何在Android中按下按钮拨打电话号码? [英] How can I call a number from button press in Android?

查看:207
本文介绍了如何在Android中按下按钮拨打电话号码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我非常喜欢这个,我正在努力让这个工作起来。

I'm very much a beginner at this and I'm struggling to get this to work.

按下按钮时,我只想让拨号器自动输入指定的号码打开。

When button is pressed, I simply want the dialer to open with the specified number automatically inputted.

所以我尝试过以下几点:

So far I've tried the following:

Button btn_call_us = (Button) findViewById(R.id.btn_call_us);
       btn_call_us.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                Intent callIntent = new Intent(Intent.ACTION_CALL);
                callIntent.setData(Uri.parse("tel:00000000"));
                startActivity(callIntent);

            }
        });

我也试过:

Button btn_call_us = (Button) findViewById(R.id.btn_call_us);
        btn_call_us.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                String phoneno="00000000";

                Intent i=new Intent(Intent.ACTION_CALL,Uri.parse(phoneno));
                startActivity(i);

            }
        });

我已将权限ACTION_CALL添加到清单。

I've added the permission ACTION_CALL to the manifest.

每当我点击呼叫按钮时,应用程序强制关闭。

Whenever I click the Call button the app force closes.

非常感谢任何帮助。

谢谢!

推荐答案

String number = "12345678";
    Intent intent = new Intent(Intent.ACTION_CALL);
    intent.setData(Uri.parse("tel:" +number));
    startActivity(intent);

您需要将此权限添加到清单。

You need to add this Permission to your manifest.

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

这篇关于如何在Android中按下按钮拨打电话号码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆