开发者

Correct way of invoking a phone call from within the app in android

开发者 https://www.devze.com 2023-02-02 04:52 出处:网络
I have a small piece of code which is basically supposed to make a phone call when a button is pushed. I looked it up online and all the sources basically gave the same code. But for some reason this

I have a small piece of code which is basically supposed to make a phone call when a button is pushed. I looked it up online and all the sources basically gave the same code. But for some reason this code doesn't work. It makes the app crash but the LogCat doesn't display anything (meaning the log is completely blank). I should also mention that in my manifest file I did add the following permission

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

The code I have is as follows. Any help would be greatly appr开发者_运维技巧eciated!

phoneButton.setOnClickListener(new OnClickListener () {
            public void onClick(View v) {
                try {
                final Intent callIntent = new Intent(Intent.ACTION_CALL);
                callIntent.setData(Uri.parse("tel:232131232"));
                ContactUs.this.startActivity(callIntent);
                }catch (ActivityNotFoundException e){
                    Log.e("Dialing", "call Failed!", e);

                }
            }           
        });


You spelt android wrong the second time...


Sounds like you need to add the user-permission for making a phone call. I believe the permission is:

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

in your manifest file.


This is a snippet from an Activity class I am currently testing on my HTC Desire -

okButton.setOnClickListener(new OnClickListener() {

   @Override
   public void onClick(View v) {
       Intent intent = new Intent(Intent.ACTION_CALL);
       intent.setData(Uri.parse("tel:" + getPhoneNumber()));
       startActivity(intent);
   }
});

I suggest changing ContactUs.this.startActivity(callIntent); to startActivity(callIntent); and testing it again.

0

精彩评论

暂无评论...
验证码 换一张
取 消