An example of this article is an analysis of the Android dial phone function. Share to everyone for your reference, specific as follows:
The phone is one of the most basic features of mobile phones, and now Android smartphones are very popular, with a variety of wonderful phone features, but how does the Android phone achieve the basic function of calling? Examples are presented below. First, the program:
Import Java.util.regex.Matcher;
Import Java.util.regex.Pattern;
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;
Import Android.widget.Toast;
public class A01activity extends activity {private Button B;
Private EditText et; /** called the activity is a.
* * @Override public void onCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
Setcontentview (R.layout.main);
b= (Button) Findviewbyid (R.id.button);
et= (EditText) Findviewbyid (r.id.et); B.setonclicklistener (New Onclicklistener () {@Override public void OnClick (View v) {//TODO auto-generated Method St
UB String S=et.gettext (). toString ();
if (Isphonenumbervalid (s) ==true) {Intent i=new Intent ("Android.intent.action.CALL", Uri.parse ("Tel:" +s));
StartActivity (i); ET.settext ("");
} else{Et.settext (""); Toast.maketext (A01activity.this, "The phone number you entered is malformed, please re-enter!")
", Toast.length_long). Show ();
}
}
}); The//Method Isphonenumbervalid (String phonenumber) is used to determine whether the phone number is in the correct format public static Boolean Isphonenumbervalid (String phonenumb ER) {boolean isvalid=false;/** * The following string is used to specify the phone format as follows: * ^\\ (?) indicates that it can be used (as the opening * (\\d{3}) to indicate that immediately after 3 digits * \)? to continue * [-]? The stated format can be used with selective "-" continuation * (\\d{4}) to denote the following 4 digits * [-]? Indicates that the available selective "-" continuation * (\\d{4}) $ representation ends with 4 digits * can be compared to the following number format: * (123) 456-78900 123-4567-8900 12345678900 (123) -456-78900*/St Ring expression01= "^\\ ( \\D{3}) \)? [- ]? (\\d{4}) [- ]?
(\\d{4}) $ "; String expression02= "^\\ ( \\D{3}) \)? [- ]? (\\d{3}) [- ]?
(\\d{5}) $ "; Pattern P01=pattern.compile (EXPRESSION01);//To pass the phone format to the Matcher M01=p01.matcher (PhoneNumber)
Check the format of the phone number correctly pattern P02=pattern.compile (EXPRESSION02);
Matcher M02=p02.matcher (PhoneNumber); if (m01.matches () | |
M02.matches ()) {isvalid=true;
return isValid;
}}
Res/layout/main.xml is as follows:
<?xml version= "1.0" encoding= "Utf-8"?> <linearlayout xmlns:android=
"http://schemas.android.com/apk/" Res/android "
android:layout_width=" fill_parent "
android:layout_height=" fill_parent "
android:o" rientation= "vertical" >
<textview
android:layout_width= "Fill_parent"
Wrap_content "
android:text=" @string/hello/>
<button android:id=
"@+id/button"
android: Layout_width= "Fill_parent"
android:layout_height= "wrap_content"
/>
<edittext
android : id= "@+id/et"
android:layout_width= "fill_parent" android:layout_height= "wrap_content
"
/>
</LinearLayout>
Androidmanifest.xml is as follows:
<?xml version= "1.0" encoding= "Utf-8"?> <manifest xmlns:android=
"http://schemas.android.com/apk/res/" Android "
package=" com.my.a01 "
android:versioncode=" 1 "
android:versionname=" 1.0 ">
< USES-SDK android:minsdkversion= "Ten"/>
<application
android:icon= "@drawable/ic_launcher"
Android:label= "@string/app_name" >
<activity
android:name= ". A01activity "
android:label=" @string/app_name ">
<intent-filter>
<action android:name= "Android.intent.action.MAIN"/>
<category android:name= "Android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
</application>
<uses-permission android:name= " Android.permission.CALL_PHONE ">
</uses-permission>
</manifest>
Through the button to make the call, in the OnClick () method, customize a intent, incoming Action_call and Uri.parse (), the incoming URI data in the prefix of the phone is "tel:".
Note To add the right to call Android.permission.CALL_PHONE
You can use the Android.Action.Dialer method Android.intent.action.DIAL to invoke the virtual keyboard to dial the phone.
There is a simpler way to verify that the phone number format you entered is correct: in a EditText object in Main.xml, add
Android:phonenumber= "true"
You can restrict the data entered must be a numeric symbol.
For more information on Android-related content readers can view the site topics: "Android Resource Operation tips Summary", "Android Development introduction and Advanced Course", "Android Control Usage Summary", "Android SMS and telephone operating skills summary" and " Android Multimedia How-to Summary (audio, video, recording, etc.)
I hope this article will help you with the Android program.