編輯:關於 Android 程式設計
需求很簡單:界面上放一個輸入框和一個「撥打電話」按鈕,用戶輸入號碼後點擊按鈕,跳轉到系統撥號介面並帶入號碼。這是一個非常典型的「Intent 隱式調用系統組件」的練習。
使用垂直方向的 LinearLayout,內部依次放置提示文字、輸入框和按鈕:
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
xmlns:tools="https://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".PhoneActivity" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="請輸入電話" />
<EditText
android:id="@+id/editText1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:inputType="phone" />
<Button
android:id="@+id/callsumbit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="撥打電話" />
</LinearLayout>
這裡有一個值得注意的屬性:android:inputType="phone"。它會讓系統自動彈出數字鍵盤,並允許輸入 *、#、+ 等撥號相關字符,比使用默認的文本輸入體驗好得多。
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_call);
Button btn = (Button) findViewById(R.id.callsumbit);
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
EditText etNumber = (EditText) findViewById(R.id.editText1);
String number = etNumber.getText().toString();
Intent intent = new Intent();
intent.setAction(Intent.ACTION_CALL);
intent.setData(Uri.parse("tel:" + number));
startActivity(intent);
}
});
}
核心就是三行:ACTION_CALL 指定動作,tel: 開頭的 Uri 指定數據,然後 startActivity。系統會根據 Intent 的 action 與 data 自動匹配能處理它的 Activity,也就是系統電話應用。
直接撥打電話屬於敏感操作,必須在 AndroidManifest.xml 中聲明權限:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
package="com.ccec.callphone"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<uses-permission android:name="android.permission.CALL_PHONE" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.ccec.callphone.CallActivity"
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>
</manifest>
實際開發中,這裡有一個必須權衡的選擇:
| Action | 行為 | 是否需要權限 |
|---|---|---|
Intent.ACTION_CALL | 直接撥號,立即打出 | 需要 CALL_PHONE(危險權限,需運行時申請) |
Intent.ACTION_DIAL | 打開撥號盤並自動填入號碼,由用戶確認後再撥 | 無需任何權限 |
兩者的實現代碼幾乎一樣,只不過把 action 換成 Intent.ACTION_DIAL 即可:
Intent intent = new Intent(Intent.ACTION_DIAL);
intent.setData(Uri.parse("tel:" + number));
startActivity(intent);
從用戶體驗與上架審核的角度,大多數業務場景更推薦使用 ACTION_DIAL。原因有三:其一,避免用戶誤觸直接撥出高額付費電話;其二,讓用戶在撥出前有一次確認機會,符合通話類應用的普遍交互慣例;其三,省掉了危險權限的運行時申請流程,代碼更簡潔,也不會因為用戶拒絕授權而導致功能不可用。
如果確實需要一鍵直撥(例如客服熱線、緊急聯絡場景),那麼在 Android 6.0(API 23)及以上版本還必須實現運行時動態申請:
if (ContextCompat.checkSelfPermission(this, Manifest.permission.CALL_PHONE)
!= PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.CALL_PHONE}, REQUEST_CALL_PHONE);
} else {
// 已授權,直接撥打
}
ActivityNotFoundException。Uri.encode(number) 對號碼做編碼,防止特殊字符引發解析異常。resolveActivity() 先判斷是否有可處理的應用,再決定是否提示。這個例子雖然簡單,但涵蓋了 Android 開發中幾個核心知識點:佈局中控件的 inputType 屬性、Intent 的 action 與 data 組合、權限聲明,以及 ACTION_CALL 與 ACTION_DIAL 的場景取捨。理解了「Intent 通過 action + data 匹配組件」這一機制,後續調用系統相機、打開網頁、發送郵件、分享文本,套路完全一致。
Android Studio 報錯 9-patch image malformed 的原因與修法
在 Android Studio 中處理 .9.png 時,常會看到這則錯誤: ERROR: 9-patch image icon_item_bottom_line.9.
Android 動畫完全解析(一):幀動畫、補間動畫與屬性動畫
移動互聯網發展到今天,App 之間的競爭早已不只看功能,界面呈現與交互手感同樣關鍵。動畫已經成為交互中不可分割的一部分。平時記錄的知識點比較零碎,這次做個統一整理,順便溫
Android 如何精準獲取設備運營商
今天接到一個新需求:要給服務器上報設備的運營商名稱。我的第一反應是——連友盟都無法精準統計運營商,我該如何判斷具體是哪家運營商呢?如下圖: 這張圖反映了友盟統計到的用戶
Volley 深度實戰:從 Request 到自訂框架封裝
Volley 是 Google 在 I/O 2013 端出的官方網路通訊庫。在它出現之前,Android 開發者多半在幾個成熟的三方庫之間做選擇——android-asy