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

谁都会走 提交于 2019-11-30 16:12:31

问题


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!


回答1:


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>



回答2:


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




回答3:


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




回答4:


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




回答5:


Try this.

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

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




回答6:


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

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



回答7:


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



来源:https://stackoverflow.com/questions/16412480/how-can-i-call-a-number-from-button-press-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!