Android撥打電話功能實(shí)例詳解
本文實(shí)例分析了Android撥打電話功能。分享給大家供大家參考,具體如下:
打電話是手機(jī)的一個(gè)最基本的功能,現(xiàn)在android智能手機(jī)非常流行,里面有多種多樣的精彩的手機(jī)功能,但是android手機(jī)如何實(shí)現(xiàn)打電話這個(gè)基本功能呢?現(xiàn)以實(shí)例說明如下。首先呈上程序:
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, "您輸入的電話號(hào)碼格式錯(cuò)誤,請(qǐng)重新輸入!", Toast.LENGTH_LONG).show();
}
}
});
}
//方法isPhoneNumberValid(String phoneNumber)用來判斷電話號(hào)碼的格式是否正確
public static boolean isPhoneNumberValid(String phoneNumber){
boolean isValid=false;
/**
* 用下面的字符串規(guī)定電話格式如下:
* ^\\(? 表示可使用(作為開頭
* (\\d{3}) 表示緊接著3個(gè)數(shù)字
* \\)? 表示可使用)繼續(xù)
* [- ]? 表示在上述格式后可用具有選擇性的“-”繼續(xù)
* (\\d{4}) 表示緊接著4個(gè)數(shù)字
* [- ]? 表示可用具有選擇性的“-”繼續(xù)
* (\\d{4})$ 表示以4個(gè)數(shù)字結(jié)束
* 可以和下面的數(shù)字格式比較:
* (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對(duì)象將電話格式傳入
Matcher m01=p01.matcher(phoneNumber);//檢查電話號(hào)碼的格式是否正確
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()方法中,自定義一個(gè)Intent,傳入ACTION_CALL與Uri.parse(),傳入的Uri數(shù)據(jù)中電話的前綴為“tel:”。
注意要添加撥打電話的權(quán)限android.permission.CALL_PHONE
可以使用Android.Action.Dialer方式android.intent.action.DIAL來調(diào)用虛擬鍵盤來撥打電話。
用來檢驗(yàn)輸入的電話號(hào)碼格式是否正確還有一個(gè)比較簡(jiǎn)便的方法:在main.xml中的EditText的對(duì)象中,添加
android:phoneNumber="true"
即可限制輸入的數(shù)據(jù)必須為數(shù)字符號(hào)。
更多關(guān)于Android相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Android資源操作技巧匯總》《Android開發(fā)入門與進(jìn)階教程》、《Android控件用法總結(jié)》、《Android短信與電話操作技巧匯總》及《Android多媒體操作技巧匯總(音頻,視頻,錄音等)》
希望本文所述對(duì)大家Android程序設(shè)計(jì)有所幫助。
相關(guān)文章
Android Retrofit 2.0框架上傳圖片解決方案
這篇文章主要介紹了Android Retrofit 2.0框架上傳一張與多張圖片解決方案,感興趣的小伙伴們可以參考一下2016-03-03
Android實(shí)現(xiàn)登錄注冊(cè)頁面(下)
這篇文章主要介紹了Android實(shí)現(xiàn)登錄注冊(cè)頁面的第二篇,實(shí)現(xiàn)驗(yàn)證登錄和記住密碼功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-04-04
Android優(yōu)化方案之Fragment的懶加載實(shí)現(xiàn)代碼
本篇文章主要介紹了Android優(yōu)化方案之Fragment的懶加載實(shí)現(xiàn)代碼,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-03-03
使用Fragment+ViewPager實(shí)現(xiàn)底部導(dǎo)航欄
這篇文章主要為大家詳細(xì)介紹了使用Fragment+ViewPager實(shí)現(xiàn)底部導(dǎo)航欄,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-06-06
Android開發(fā)升級(jí)AGP7.0后的一些適配方法技巧
這篇文章主要為大家介紹了升級(jí)AGP7.0后的一些適配方法技巧,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-06-06
Android 調(diào)用系統(tǒng)應(yīng)用的方法總結(jié)
這篇文章主要介紹了Android 調(diào)用系統(tǒng)應(yīng)用的方法總結(jié)的相關(guān)資料,這里提供調(diào)用錄像,錄音,拍照等功能,需要的朋友可以參考下2017-08-08
android通過led實(shí)現(xiàn)手電筒功能
這篇文章主要為大家詳細(xì)介紹了android通過led實(shí)現(xiàn)手電筒功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-09-09

