Android basics-call

Source: Internet
Author: User

In this example, the Intent (Intent) is used to call the system dialer to call the phone.

1. layout File

Android: hint = "@ string/phoneHint": indicates the hidden prompt information.

Android: inputType = "phone": indicates that the input type is phone number.

     
  
   android:hint="@string/phoneHint"        android:ems="10"        
   android:inputType="phone" >        
       
      
  
 

2. MainActivity

Set button listening events

Intent. setData (Uri. parse ("tel:" + phoneNumber): sets the data to be transmitted. The Uri type. The telephone number must be prefixed with "tel :"

Intent. setAction (Intent. ACTION_CALL): sets the Intent action to call.

A similar feature is available in Windows, where you enter the start http://www.baidu.com in the command line, which will be opened in the default browser.



<喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPHByZSBjbGFzcz0 = "brush: java;"> public class MainActivity extends Activity {@ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); (Button) findViewById (R. id. btnCall )). setOnClickListener (new OnClickListener () {@ Overridepublic void onClick (View v) {String phoneNumber = (EditText) findViewById (R. id. etPhone )). getText (). toString (); // intention: Intent intent = new Intent (); intent. setAction (Intent. ACTION_CALL); // url: Uniform Resource Locator // uri: Uniform Resource Identifier (WIDER) intent. setData (Uri. parse ("tel:" + phoneNumber); // enable the system dial startActivity (intent );}});}}3. An error is reported. If you do not have the permission to run the program, the following error is found in LogCat:

03-22 16:58:47. 263: E/AndroidRuntime (26347): java. lang. securityException: Permission Denial: starting Intent {act = android. intent. action. CALL dat = tel: xxx cmp = com. android. phone /. outgoingCallBroadcaster} from ProcessRecord {41eec660 26347: com. example. call/u0a77} (pid = 26347, uid = 10077) requires android. permission. CALL_PHONE

Add the android. permission. CALL_PHONE permission to AndroidManifest. xml.

    
     
         

Running effect:


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.