手机
当前位置:查字典教程网 >编程开发 >安卓软件开发 >Android拨打电话功能实例详解
Android拨打电话功能实例详解
摘要:本文实例分析了Android拨打电话功能。分享给大家供大家参考,具体如下:打电话是手机的一个最基本的功能,现在android智能手机非常流行...

本文实例分析了Android拨打电话功能。分享给大家供大家参考,具体如下:

打电话是手机的一个最基本的功能,现在android智能手机非常流行,里面有多种多样的精彩的手机功能,但是android手机如何实现打电话这个基本功能呢?现以实例说明如下。首先呈上程序:

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 when the activity is first created. */ @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 stub 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, "您输入的电话号码格式错误,请重新输入!", Toast.LENGTH_LONG).show(); } } }); } //方法isPhoneNumberValid(String phoneNumber)用来判断电话号码的格式是否正确 public static boolean isPhoneNumberValid(String phoneNumber){ boolean isValid=false; /** * 用下面的字符串规定电话格式如下: * ^(? 表示可使用(作为开头 * (d{3}) 表示紧接着3个数字 * )? 表示可使用)继续 * [- ]? 表示在上述格式后可用具有选择性的“-”继续 * (d{4}) 表示紧接着4个数字 * [- ]? 表示可用具有选择性的“-”继续 * (d{4})$ 表示以4个数字结束 * 可以和下面的数字格式比较: * (123)456-78900 123-4567-8900 12345678900 (123)-456-78900*/ String expression01="^(?(d{3}))?[- ]?(d{4})[- ]"; String expression02="^(?(d{3}))?[- ]?(d{3})[- ]"; Pattern p01=Pattern.compile(expression01);//通过Pattern对象将电话格式传入 Matcher m01=p01.matcher(phoneNumber);//检查电话号码的格式是否正确 Pattern p02=Pattern.compile(expression02); Matcher m02=p02.matcher(phoneNumber); if(m01.matches()||m02.matches()){ isValid=true; } return isValid; } }

res/layout/main.xml如下:

<"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:orientation="vertical" > <TextView android:layout_width="fill_parent" android:layout_height="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如下:

<"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="10" /> <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>

通过Button来拨打电话,在onClick()方法中,自定义一个Intent,传入ACTION_CALL与Uri.parse(),传入的Uri数据中电话的前缀为“tel:”。

注意要添加拨打电话的权限android.permission.CALL_PHONE

可以使用Android.Action.Dialer方式android.intent.action.DIAL来调用虚拟键盘来拨打电话。

用来检验输入的电话号码格式是否正确还有一个比较简便的方法:在main.xml中的EditText的对象中,添加

android:phoneNumber="true"

即可限制输入的数据必须为数字符号。

希望本文所述对大家Android程序设计有所帮助。

【Android拨打电话功能实例详解】相关文章:

Android 情景模式的设置代码

Android 通用型手电筒代码

Android开发常用小功能

Android文件下载进度条的实现代码

Android 动画之AlphaAnimation应用详解

Android中打电话的数据流程分析

Android 有道词典的简单实现方法介绍

Android 自定义标题栏 显示网页加载进度的方法实例

Android利用方向传感器获得手机的相对角度实例说明

深入Android Browser配置管理的详解

精品推荐
分类导航