每一個(gè)Android設(shè)備特別是手機(jī)都提供一個(gè)撥打電話(huà)功能,但仍然需要編寫(xiě)一個(gè)應(yīng)用程序,給用戶(hù)一個(gè)選擇使用硬編碼的電話(huà)號(hào)碼撥打電話(huà)。
本章列出了一個(gè)簡(jiǎn)單的步驟來(lái)創(chuàng)建一個(gè)應(yīng)用程序,它可以用來(lái)?yè)艽螂娫?huà)。使用 Android 的 Intent 通過(guò)調(diào)用Android內(nèi)置的電話(huà)通話(huà)功能。以下部分說(shuō)明 Intent 對(duì)象的撥打電話(huà)功能 。
使用 ACTION_CALL 動(dòng)作觸發(fā)Android設(shè)備內(nèi)置電話(huà)功能。以下是簡(jiǎn)單的語(yǔ)法用來(lái)創(chuàng)建一個(gè)Intent 的 ACTION_CALL 動(dòng)作
Intent phoneIntent = new Intent(Intent.ACTION_CALL);
可以使用 ACTION_DIAL 動(dòng)作,而不是 ACTION_CALL,在這種情況下,在使用選項(xiàng)來(lái)修改硬編碼的電話(huà)號(hào)碼撥打電話(huà)之前,而不是直接調(diào)用的。
這里給定電話(huà)為 13800138000 撥打一個(gè)電話(huà),需要使用setData()方法指定URI為 tel:如下:
phoneIntent.setData(Uri.parse("tel:13800138000"));
要注意的一點(diǎn)是,撥打電話(huà)不需要任何額外的數(shù)據(jù)或數(shù)據(jù)類(lèi)型指定。
下面的示例演示如何在實(shí)際使用 Android Intent 打電話(huà)給定的手機(jī)號(hào)碼。
要嘗試這個(gè)例子中,需要實(shí)際配備了最新的 Android OS 移動(dòng)設(shè)備,否則仿真器可能無(wú)法正常工作。
| 步驟 | 描述 |
|---|---|
| 1 | 使用Android Studio創(chuàng)建Android應(yīng)用程序,并將它命名為PhoneCallDemounder。創(chuàng)建這個(gè)項(xiàng)目,確保目標(biāo) SDK編譯在 Android SDK 的最新版本或使用更高級(jí)別的API |
| 2 | 修改 src/MainActivity.java 文件,并添加所需的代碼,以撥打電話(huà) |
| 3 | 修改所需的布局XML文件 res/layout/activity_main.xml 添加GUI組件。添加一個(gè)簡(jiǎn)單的按鈕來(lái)?yè)艽蛱?hào)碼:13800138000 |
| 4 | 修改 res/values/strings.xml 定義所需的常數(shù)值 |
| 5 | 修改 AndroidManifest.xml 如下所示 |
| 6 | 運(yùn)行該應(yīng)用程序啟動(dòng) Android模擬器并驗(yàn)證應(yīng)用程序所做的修改結(jié)果 |
以下是修改主活動(dòng)文件 src/com.yiibai.phonecalldemo/MainActivity.java 的內(nèi)容如下:
package com.yiibai.phonecalldemo; import android.net.Uri; import android.os.Bundle; import android.app.Activity; import android.content.Intent; import android.util.Log; import android.view.Menu; import android.view.View; import android.widget.Button; import android.widget.Toast; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button startBtn = (Button) findViewById(R.id.makeCall); startBtn.setOnClickListener(new View.OnClickListener() { public void onClick(View view) { makeCall(); } }); } protected void makeCall() { Log.i("Make call", ""); Intent phoneIntent = new Intent(Intent.ACTION_CALL); phoneIntent.setData(Uri.parse("tel:91-800-001-0101")); try { startActivity(phoneIntent); finish(); Log.i("Finished making a call...", ""); } catch (android.content.ActivityNotFoundException ex) { Toast.makeText(MainActivity.this, "Call faild, please try again later.", Toast.LENGTH_SHORT).show(); } } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } }
下面是 res/layout/activity_main.xml 文件的內(nèi)容:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <Button android:id="@+id/makeCall" android:layout_width="fill_parent"