Android-Basic Knowledge: simple call function _ intent_study

Source: Internet
Author: User

Call the phone to learn how to use intent.

Because it is a small program, you can use the UI interface> Activity> business layer to write code from top to bottom.

To use system functions, add <uses-permission android: name = "android. permisson. CALL_PHONE"/> to AndroidMainfest. xml.

1. Interface Design res/layout/main. xml

    

Main. xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content" android:text="@string/textMobileString"/>
<EditText android:layout_width="fill_parent"
android:layout_height="wrap_content" android:hint="@string/edtMobileNumString"
android:id="@+id/edtMobileNum"/>

<Button android:layout_width="wrap_content"
android:layout_height="wrap_content" android:id="@+id/btnCall"
android:text="@string/btnCallString"/>
</LinearLayout>

 

2. Add res/values/strings. xml to the string variable

    

Strings. xml

<? Xml version = "1.0" encoding = "UTF-8"?>
<Resources>
<String name = "hello"> Hello World, PhoneCallActivity! </String>
<String name = "app_name"> PhoneCall </string>
<String name = "textMobileString"> enter the phone number you want to call: </string>
<String name = "edtMobileNumString"> enter the number... </string>
<String name = "btnCallString"> call </string>
</Resources>

 

3. Compile the Activity

   

PhoneCallActivity. java

Package com. PhoneCall. melody;

Import android. app. Activity;
Import android. content. Intent;
Import android.net. Uri;
Import android. OS. Bundle;
Import android. view. View;
Import android. view. View. OnClickListener;
Import android. widget. Button;
Import android. widget. EditText;

Public class PhoneCallActivity extends Activity implements OnClickListener {
Private EditText editText = null; // number editing Control
Private Button btnCall = null; // call the Button
Private String mobileNum = null; // number variable

@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
This. setContentView (R. layout. main );

/**
* Get controls
*/
EditText = (EditText) this. findViewById (R. id. edtMobileNum );
BtnCall = (Button) this. findViewById (R. id. btnCall );

MobileNum = editText. getText (). toString (); // get the number
BtnCall. setOnClickListener (this); // sets the listener event
}

@ Override
Public void onClick (View v ){
Switch (v. getId ()){
/**
* Call
*/
Case R. id. btnCall:
/**
* Intent. ACTION_CALL -- notifies the System of the intent (Intent) action.
* Uri. parse ("tel:" + mobileNum) Uri is a unified resource identifier, and the phone number is followed by the tel notification system.
*/
Intent intent = new Intent (Intent. ACTION_CALL, Uri. parse ("tel :"
+ MobileNum ));
PhoneCallActivity. this. startActivity (intent );
Break;
Default:
Break;
}
}

}
Related Article

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.