簡述
在項目開發(fā)的過程中往毡,我們可能或多或少的撥打電話功能,在這里我們就來總結(jié)一下?lián)艽螂娫挷僮鬟^程的一些內(nèi)容靶溜,僅作為記錄开瞭,以便以后查找。
撥打電話
撥打電話的幾種實現(xiàn)方式
// ACTION_VIEW
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("tel:" + phoneNum));
startActivity(intent);
// ACTION_DIAL
Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + phoneNum));
startActivity(intent);
// ACTION_CALL
Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + phoneNum));
startActivity(intent);
區(qū)別:
這三種方式均可以實現(xiàn)調(diào)取撥打電話頁面的功能罩息,前兩種只是調(diào)取撥打電話的頁面嗤详,并不會立刻撥打出去,第三種會直接撥打電話瓷炮。
關(guān)鍵點:
Uri.parse("tel:" + phoneNum)// 系統(tǒng)會自動識別這個字符串為電話號碼葱色。
// 在系統(tǒng)Uri的源碼里發(fā)現(xiàn)了一段代碼:
public String toSafeString() {
String scheme = getScheme();
String ssp = getSchemeSpecificPart();
if (scheme != null) {
if (scheme.equalsIgnoreCase("tel") || scheme.equalsIgnoreCase("sip")
|| scheme.equalsIgnoreCase("sms") || scheme.equalsIgnoreCase("smsto")
|| scheme.equalsIgnoreCase("mailto")) {
StringBuilder builder = new StringBuilder(64);
builder.append(scheme);
builder.append(':');
if (ssp != null) {
for (int i=0; i<ssp.length(); i++) {
char c = ssp.charAt(i);
if (c == '-' || c == '@' || c == '.') {
builder.append(c);
} else {
builder.append('x');
}
}
}
return builder.toString();
} else if (scheme.equalsIgnoreCase("http") || scheme.equalsIgnoreCase("https")
|| scheme.equalsIgnoreCase("ftp")) {
ssp = "http://" + ((getHost() != null) ? getHost() : "")
+ ((getPort() != -1) ? (":" + getPort()) : "")
+ "/...";
}
}
// Not a sensitive scheme, but let's still be conservative about
// the data we include -- only the ssp, not the query params or
// fragment, because those can often have sensitive info.
StringBuilder builder = new StringBuilder(64);
if (scheme != null) {
builder.append(scheme);
builder.append(':');
}
if (ssp != null) {
builder.append(ssp);
}
return builder.toString();
}
這段代碼里對tel、sip娘香、sms等等進行判斷苍狰,具體如何調(diào)用的可以參考源碼。
如何直接撥打分機號碼
在我們撥打電話是烘绽,如10086淋昭,可能需要撥打分機進行相關(guān)業(yè)務(wù)的查詢,這個時候可能是打通10086诀姚,然后撥號鍵上輸入相應(yīng)的數(shù)字轉(zhuǎn)分機响牛。在Android中提供了可以直接撥打分機號碼的功能。
撥打電話的功能同上赫段,但是需要在號碼中添加“,”或者“;”呀打,兩者的區(qū)別是:
手機上撥打:10086P1W1,這里P和W分別是Auto DTMF和Wait user confirm DTMF,在Android里面沒有P和W糯笙,是用逗號和分號代替的贬丛,但功能就是P,W给涕。
也就是說"tel:" + phone + "," + fenPhone:是自動撥打分機號碼
而"tel:" + phone + ";" + fenPhone:是需要用戶確認豺憔,確認之后,自動撥打分機號碼
還有一種情況是添加兩個逗號够庙,即:“,,”
說明一下恭应,逗號表示暫停,是讓電話可暫停片刻耘眨,待語音說完分機號碼再撥號昼榛,添加的逗號越多,表示暫停的時間越長剔难。
最后就是胆屿,一定不能忘記添加權(quán)限:
<uses-permission android:name="android.permission.CALL_PHONE" />
同時在Android 6.0以上還要動態(tài)獲取權(quán)限。
最后
好記性不如爛筆頭偶宫,這里僅僅是記錄一下了解的新的內(nèi)容非迹,便于以后查找,如有錯誤請及時糾正纯趋,謝謝憎兽。