<manifest xmlns:android="https://schemas.android.com/apk/res/android"
package="com.example.phonecall">
<!-- API 23+ 執行時權限,必須聲明 -->
<uses-permission android:name="android.permission.CALL_PHONE" />
<!-- 若僅需開啟撥號介面由用戶確認,使用 ACTION_DIAL 無需權限 -->
<uses-permission android:name="android.permission.CALL_PRIVILEGED" /> <!-- 系統級,一般應用不可用 -->
</manifest>
| Intent Action | 行為 | 需權限 | 適用場景 |
|---|---|---|---|
ACTION_DIAL | 開啟撥號器預填號碼,**用戶需手動點擊撥打鍵** | **無** | 尊重用戶意願、合規、推薦 |
ACTION_CALL | **直接撥打電話**,無用戶確認 | CALL_PHONE (危險權限) | 自動化測試、企業內部工具、緊急呼叫 |
class MainActivity : ComponentActivity() {
private val requestCallPermission = registerForActivityResult(ActivityResultContracts.RequestPermission()) { granted ->
if (granted) dialNumber(phoneNumber) else showPermissionRationale()
}
private lateinit var phoneNumber: String
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val etPhone = findViewById(R.id.etPhone)
val btnCall = findViewById
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="24dp"
android:gravity="center">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="請輸入電話號碼:"
android:textSize="18sp" />
<EditText
android:id="@+id/etPhone"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="phone"
android:hint="09xxxxxxxx" />
<Button
android:id="@+id/btnCall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:text="直接撥打 (需權限)" />
<Button
android:id="@+id/btnDial"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:text="開啟撥號器 (免權限)" />
</LinearLayout>
Uri.parse("tel:+886912345678") 支援國際格式;特殊字元 (*、#、逗號暫停) 需 Uri.encode。SubscriptionManager 可指定 SIM 槽,但需系統簽名權限,一般應用無法控制。110、119、911 等無需 SIM 卡即可撥打,ACTION_CALL 同樣適用。CALL_PHONE 需填寫權限聲明表單,否則會被拒。優先使用 ACTION_DIAL。遵循「最小權限、用戶確認、優雅降級」原則,讓撥號功能既合規又好用。