简体   繁体   中英

How can I call a number from button press in 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);

            }
        });

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.

Whenever I click the Call button the app force closes.

Any assistance would be greatly appreciated.

Thank you!

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>

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

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

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

You do not need any permission

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

Try this.

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

Also, add the permission android.permission.CALL_PHONE in your manifest file.

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

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

在你的Android手机中转到:“设置” - >“InstalledApp” - >“找到你的应用程序” - >“应用程序权限” - >“这里允许”电话许可“希望它会帮助你

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM