核心 API 概覽
| 功能 | 核心類/方法 | 關鍵權限 |
| 撥打電話 | Intent.ACTION_CALL, Uri.parse("tel:"+number) | CALL_PHONE |
| 發送短信 | SmsManager.getDefault().sendTextMessage() | SEND_SMS |
完整 MainActivity 代碼
package com.bblei.caller;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.SystemClock;
import android.telephony.gsm.SmsManager;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends Activity implements OnClickListener {
private static final String TAG = "MainActivity";
private EditText etNumber;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// 隱藏標題欄 (需在 setContentView 前)
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.main);
Button btnCall = (Button) findViewById(R.id.btn_call);
btnCall.setOnClickListener(this);
Button btnSms = (Button) findViewById(R.id.btn_sendMessage);
btnSms.setOnClickListener(this);
etNumber = (EditText) findViewById(R.id.et_number);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn_call:
Log.i(TAG, "撥打電話");
Toast.makeText(this, "撥打電話", Toast.LENGTH_SHORT).show();
call();
break;
case R.id.btn_sendMessage:
Log.i(TAG, "發送短信");
Toast.makeText(this, "發送短信", Toast.LENGTH_SHORT).show();
sendMessage();
break;
}
}
private void call() {
String number = etNumber.getText().toString().trim();
if (number.isEmpty()) {
Toast.makeText(this, "請輸入電話號碼", Toast.LENGTH_SHORT).show();
return;
}
Intent intent = new Intent(Intent.ACTION_CALL);
intent.setData(Uri.parse("tel:" + number));
try {
startActivity(intent);
} catch (SecurityException e) {
Toast.makeText(this, "缺少 CALL_PHONE 權限", Toast.LENGTH_SHORT).show();
e.printStackTrace();
}
}
private void sendMessage() {
final String number = etNumber.getText().toString().trim();
if (number.isEmpty()) {
Toast.makeText(this, "請輸入電話號碼", Toast.LENGTH_SHORT).show();
return;
}
// 使用子線程避免 ANR,演示每 0.5 秒發送一次 (實際請加入停止條件)
new Thread(new Runnable() {
@Override public void run() {
SmsManager sms = SmsManager.getDefault();
while (true) { // 演示用無限循環,生產環境請勿如此
SystemClock.sleep(500);
try {
sms.sendTextMessage(number, null, "100000000RMB", null, null);
Log.i(TAG, "短信已發送至 " + number);
} catch (Exception e) {
Log.e(TAG, "發送失敗", e);
}
}
}
}).start();
}
}
AndroidManifest.xml 權限配置
<manifest ...>
<uses-permission android:name="android.permission.CALL_PHONE" />
<uses-permission android:name="android.permission.SEND_SMS" />
<application ...>
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
佈局文件 res/layout/main.xml (示例)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:orientation="vertical" android:padding="16dp"
android:layout_width="match_parent" android:layout_height="match_parent">
<EditText
android:id="@+id/et_number"
android:hint="輸入電話號碼"
android:inputType="phone"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:id="@+id/btn_call"
android:text="撥打電話"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp" />
<Button
android:id="@+id/btn_sendMessage"
android:text="發送短信"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp" />
</LinearLayout>
關鍵注意事項
- Android 6.0+ 動態權限:
CALL_PHONE 和 SEND_SMS 屬於危險權限,必須在運行時請求用戶授權(ActivityCompat.requestPermissions),否則 startActivity 或 sendTextMessage 會拋出 SecurityException。
- Intent.ACTION_DIAL vs ACTION_CALL:
ACTION_DIAL 僅跳轉撥號界面,需用戶手動點擊撥打,不需要權限。
ACTION_CALL 直接撥打,需要 CALL_PHONE 權限。建議優先使用 ACTION_DIAL。
- 短信發送最佳實踐:
• 使用 PendingIntent 監聽發送/送達狀態。
• 長短信自動分段:sms.divideMessage(text) 返回列表,逐條發送。
• 避免在主線程發送,使用 IntentService、WorkManager 或協程。
• 尊重用戶隱私,勿濫發垃圾短信(演示代碼中的無限循環僅供學習)。
- 雙卡設備:Android 5.1+ 提供
SubscriptionManager 管理多 SIM 卡,發送短信可指定 subId。
進階擴展:短信發送狀態監聽
String SENT = "SMS_SENT";
String DELIVERED = "SMS_DELIVERED";
PendingIntent sentPI = PendingIntent.getBroadcast(this, 0, new Intent(SENT), 0);
PendingIntent deliveredPI = PendingIntent.getBroadcast(this, 0, new Intent(DELIVERED), 0);
registerReceiver(new BroadcastReceiver() {
@Override public void onReceive(Context ctx, Intent intent) {
switch (getResultCode()) {
case Activity.RESULT_OK: Toast.makeText(ctx, "發送成功", Toast.LENGTH_SHORT).show(); break;
case SmsManager.RESULT_ERROR_GENERIC_FAILURE: Toast.makeText(ctx, "發送失敗", Toast.LENGTH_SHORT).show(); break;
// ... 其他錯誤碼
}
}
}, new IntentFilter(SENT));
registerReceiver(new BroadcastReceiver() {
@Override public void onReceive(Context ctx, Intent intent) {
Toast.makeText(ctx, "對方已接收", Toast.LENGTH_SHORT).show();
}
}, new IntentFilter(DELIVERED));
sms.sendTextMessage(number, null, content, sentPI, deliveredPI);