Android APP 兩種用程式撥號的方式,androidapp

來源:互聯網
上載者:User

Android APP 兩種用程式撥號的方式,androidapp

想在APP中添加一個撥號功能該怎樣做呢?Android提供了兩種方式,一種是ACTION_CALL方式直接撥打,另一種是ACTION_DIAL方式開啟系統的撥號介面。

下面我們來做個小例子

首先需要在AndroidManifest.xml中添加一個使用許可權,這個容易忘哈哈。

<uses-permission android:name="android.permission.CALL_PHONE" />

然後搭一個簡單的介面測試一下,下面是布局檔案代碼

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical" >    <TextView        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:text="請輸入要撥打的號碼:" />    <EditText        android:id="@+id/etPhone"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:inputType="phone" />    <Button        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:onClick="onClickActionCall"        android:text="ACTION_CALL方式直接撥打" />    <Button        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:onClick="onClickActionDial"        android:text="ACTION_DIAL方式開啟撥號介面" /></LinearLayout>

下面是對應的Activity代碼:

package chengyujia.androidtest;import android.app.Activity;import android.content.Intent;import android.net.Uri;import android.os.Bundle;import android.view.View;import android.widget.EditText;import android.widget.Toast;public class CallActivity extends Activity {    private EditText etPhone;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_call);        etPhone = (EditText) findViewById(R.id.etPhone);    }    // ACTION_CALL方式撥打到電話(直接撥打)    public void onClickActionCall(View v) {        //這裡的Intent.ACTION_CALL實際就是一個特定的字串,        //ACTION_CALL = "android.intent.action.CALL",        //告訴系統我要直接撥號了。        call(Intent.ACTION_CALL);    }    // ACTION_DIAL方式撥打到電話(開啟撥號介面)    public void onClickActionDial(View v) {        //同理,這裡的Intent.ACTION_DIAL也是一個特定的字串        //ACTION_DIAL = "android.intent.action.DIAL"        //告訴系統我要開啟撥號介面,並把要撥的號顯示在撥號介面上,由使用者決定是否要撥打。        call(Intent.ACTION_DIAL);    }        private void call(String action){        String phone = etPhone.getText().toString();        if(phone!=null&&phone.trim().length()>0){        //這裡"tel:"+電話號碼 是固定格式,系統一看是以"tel:"開頭的,就知道後面應該是電話號碼。        Intent intent = new Intent(action, Uri.parse("tel:" + phone.trim()));        startActivity(intent);//調用上面這個intent實現撥號        }else{            Toast.makeText(this, "電話號碼不可為空", Toast.LENGTH_LONG).show();        }    }}

下面運行一下,看看效果。

介面如下:

我填寫了電話號碼10086,下面點擊第一個按鈕“ACTION_CALL方式直接撥打”,

如下:

發現並沒有直接撥出去,而是給了使用者一個提示,讓使用者選擇是否真的要撥號,這也是防止有人作惡啊。科技本應該讓生活更美好,而不是讓生活更糟糕,但不是每個人都這麼想的哦,所以不得不防啊。系統做的對,咱繼續測試,點擊“允許一次”,就開始真正撥號了,如下:

掛了電話,回到剛才的測試介面,點擊第二個按鈕“ACTION_DIAL方式開啟撥號介面”,下面是點擊後的:

 

這就是系統的撥號介面,同時把要撥的號碼也給使用者寫好了,要不要撥就由使用者決定嘍。

實際開發中用哪種方式,這個要看具體情況了。好了,關於Android APP 用程式實現撥號功能就寫這些吧。

工作不是生活的全部,最後放一個搞笑的段子,樂呵樂呵

菩提老祖將悟空喚至身前:“你已學會長生不老術和七十二變,今日為師欲傳授你新的法術。” 悟空道:“是何法術?”菩提老祖道:“看到這天上的雲彩了嗎?這邊有七朵雲彩,那邊有五朵雲彩,一共有幾朵?” 悟空答:“十二朵。” 菩提老祖道:“嗯,我要教你的就是雲端運算。”

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.