Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

Android 中如何調起撥號功能?

編輯:關於 Android 程式設計

Android 系統本就是為手機而設計的,因此在任何 App 中,只要願意,調起撥號都相當方便。

核心做法是用 Intent 做一次跳轉,把 Action 指定為 Intent.ACTION_CALL 即可。

核心程式碼:

Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:13888888888"));

第 1 步:建立 Activity

package com.android.dev;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class DialerAndMsgActivity extends Activity {
    private Button dialerButton;
    private EditText editTextPhoneNum;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        dialerButton = (Button) findViewById(R.id.Button_Dialer);
        editTextPhoneNum = (EditText) findViewById(R.id.EditText_PhoneNum);
        dialerButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View arg0) {
                Intent intent = new Intent(Intent.ACTION_CALL,
                        Uri.parse("tel:" + editTextPhoneNum.getText()));
                DialerAndMsgActivity.this.startActivity(intent);
            }
        });
    }
}

第 2 步:編寫佈局 main.xml




    

    

    

第 3 步:加入權限宣告

第 4 步:編譯運行

android 撥打電話

android 撥打電話

補充說明:Intent.ACTION_CALL 會直接撥出電話,因此必須具備 CALL_PHONE 權限,且自 Android 6.0 起還需要在執行期動態申請。如果你只希望「跳到撥號介面並帶入號碼」,而不真的按下去撥打,可以改用 Intent.ACTION_DIAL,那樣就不需要任何權限,對使用者也更友善——這在多數場景下其實是更好的選擇。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved