java 如何通过 Android 中的按钮呼叫号码?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16412480/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 22:52:46  来源:igfitidea点击:

How can I call a number from button press in Android?

javaandroidphone-call

提问by Nucleotide

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);

            }
        });

I've also tried:

我也试过:

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);

            }
        });

I've added the permission ACTION_CALL to the manifest.

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

Whenever I click the Call button the app force closes.

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

Any assistance would be greatly appreciated.

任何帮助将不胜感激。

Thank you!

谢谢!

回答by Naskov

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>

回答by null pointer

i think you Must add <uses-permission android:name="android.permission.CALL_PHONE" />in Manifest.

我认为您必须<uses-permission android:name="android.permission.CALL_PHONE" />在 Manifest 中添加。

回答by Hoan Nguyen

If you want the dialer to open with the number use ACTION_DIAL

如果您希望拨号器使用该号码打开,请使用 ACTION_DIAL

Intent i=new Intent(Intent.ACTION_DIAL,Uri.parse("tel:" + phoneno));  

You do not need any permission

您不需要任何许可

回答by Jawad Zeb

Make sure you have added the

确保您已添加

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

tag at a correct level in the AndroidManifest.xml file (outside the <application ... />tag but within the <manifest ... />tag):

AndroidManifest.xml 文件中正确级别的<application ... />标记(在标记外 但在 <manifest ... />标记内):

回答by user1721904

Add the following in the manifest file and it should work fine -

在清单文件中添加以下内容,它应该可以正常工作 -

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

回答by SudoRahul

Try this.

试试这个。

startActivity(new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + phoneno)));

Also, add the permission android.permission.CALL_PHONEin your manifestfile.

此外,android.permission.CALL_PHONE在您的manifest文件中添加权限。

回答by user11013012

In your Android Phone Go to: "Setting"-> "InstalledApp"-> "find your app"-> "App Permission"-> "Here allow the "Telephone Permission" Hope it will help you

在您的 Android 手机中转到:“设置”->“已安装的应用程序”->“查找您的应用程序”->“应用程序权限”->“此处允许“电话权限”希望对您有所帮助