10.1 問題
應用程序需要向外發(fā)送短信。
10.2 解決方案
(API Level 4)
使用SMSManager發(fā)送文字或數(shù)據短信。SMSManager是一個系統(tǒng)服務吭净,用來處理短信發(fā)送并把操作的狀態(tài)反饋給應用程序瘾腰。SMSManager提供了SmsManager.sendTextMessage()和SmsManager.sendMultipartTextMessage()方法來發(fā)送文字短信姨裸,并且提供了SmsManager.sendDataMessage()方法來發(fā)送數(shù)據短信括尸。這些方法都含有有用于傳遞發(fā)送操作狀態(tài)和將消息傳回請求目標的PendingIntent參數(shù)巷蚪。
10.3 實現(xiàn)機制
下面看一個簡單的示例,其發(fā)送一條短信并監(jiān)控其狀態(tài)(參見以下代碼清單)濒翻。
發(fā)送短信的Activity
public class SmsActivity extends Activity {
//想要監(jiān)聽的設備地址 (電話號碼、短代碼等)
private static final String RECIPIENT_ADDRESS = "<ENTER YOUR NUMBER HERE>";
/* Custom Action Strings for Result Delivery */
private static final String ACTION_SENT =
"com.examples.sms.SENT";
private static final String ACTION_DELIVERED =
"com.examples.sms.DELIVERED";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Button sendButton = new Button(this);
sendButton.setText("Hail the Mothership");
sendButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
sendSMS("Beam us up!");
}
});
setContentView(sendButton);
}
@Override
protected void onResume() {
super.onResume();
//監(jiān)控操作的狀態(tài)
registerReceiver(sent, new IntentFilter(ACTION_SENT));
registerReceiver(delivered, new IntentFilter(ACTION_DELIVERED));
}
@Override
protected void onPause() {
super.onPause();
//確保在位于后臺時接收器不會激活
unregisterReceiver(sent);
unregisterReceiver(delivered);
}
private void sendSMS(String message) {
//Construct a PendingIntent to fire on SMS sent
PendingIntent sIntent = PendingIntent.getBroadcast(
this, 0, new Intent(ACTION_SENT), 0);
//Construct a PendingIntent to fire on SMS delivery confirmation
PendingIntent dIntent = PendingIntent.getBroadcast(
this, 0, new Intent(ACTION_DELIVERED), 0);
//發(fā)送短信
SmsManager manager = SmsManager.getDefault();
manager.sendTextMessage(RECIPIENT_ADDRESS, null, message,
sIntent, dIntent);
}
/*
* BroadcastReceiver that is registered to receive events when
* an SMS message is sent; with the result code.
*/
private BroadcastReceiver sent = new BroadcastReceiver(){
@Override
public void onReceive(Context context, Intent intent) {
switch (getResultCode()) {
case Activity.RESULT_OK:
//發(fā)送成功
break;
case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
case SmsManager.RESULT_ERROR_NO_SERVICE:
case SmsManager.RESULT_ERROR_NULL_PDU:
case SmsManager.RESULT_ERROR_RADIO_OFF:
//發(fā)送失敗
break;
}
}
};
/*
* BroadcastReceiver that is registered to receive events when
* an SMS delivery confirmation is received; with the result code.
*/
private BroadcastReceiver delivered = new BroadcastReceiver(){
@Override
public void onReceive(Context context, Intent intent) {
switch (getResultCode()) {
case Activity.RESULT_OK:
//傳遞成功
break;
case Activity.RESULT_CANCELED:
//傳遞失敗
break;
}
}
};
}
要點:
發(fā)送短信需要在清單中聲明android.permission.SEND_SMS權限啦膜。
在本例中有送,當用戶單擊按鈕時就會通過SMSManager發(fā)送一條短信。由于SMSManager是一項系統(tǒng)服務僧家,必須調用靜態(tài)方法SMSManager.getDefault()獲得它的一個引用雀摘。sendTextMessage()方法的參數(shù)為目標地址(號碼)、服務中心地址以及短信內容八拱。要想讓SMSManager使用系統(tǒng)默認的服務中心地址阵赠,這個參數(shù)就應該設為null涯塔。
這里注冊了兩個BroadcastReceiver,用來接收要發(fā)出的回調Intent:一個發(fā)送操作的狀態(tài)清蚀,另一個是傳遞的狀態(tài)匕荸。這些接收器只有在操作處于待發(fā)送狀態(tài)時才會被注冊,在處理完Intent后就會立即取消注冊枷邪。