当前位置: 移动技术网 > IT编程>移动开发>Android > Android拨打电话功能实例详解

Android拨打电话功能实例详解

2019年07月24日  | 移动技术网IT编程  | 我要评论

厚朴基金,自己配药救回母亲,相声小品mp3下载

本文实例分析了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})[- ]?(\\d{4})$";
   string expression02="^\\(?(\\d{3})\\)?[- ]?(\\d{3})[- ]?(\\d{5})$";
   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如下:

<?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: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如下:

<?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="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程序设计有所帮助。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网