[英]How to receive SMS sent/delivered feedback for individual SMS after user left activity
我有一個提供短信服務的應用程序。 SMS數據存儲在應用程序數據庫中。 該應用程序支持發送到多個聯系人,因此在發送SMS時,我會動態注冊一個不同的BroadcastReceiver來偵聽是否發送了每個SMS(我使用String中的每個電話號碼來標識IntentFilter中的操作)。
收到確認后,我將SMS寫入數據庫中。
問題是,如果用戶在廣播“已發送的” Intent之前離開活動,那么BroadcastReceivers將丟失,並且我無法再捕獲“已發送的” Intent,因此不會更新數據庫。 我選擇的一種解決方法是實現onKeyDown(),以防止用戶在廣播所有Intent之前關閉活動,但是此解決方案僅適用於“返回”按鈕-無法捕獲“主頁”按鈕事件。
這是我的代碼:
public void sendSMS(String[] phoneNumbers, String message){
final String currentMessage = message;
SmsManager sms = SmsManager.getDefault();
ArrayList<String> parts = sms.divideMessage(message);
for(int i=0; i<phoneNumbers.length; i++){
for(int j=0; j<parts.size(); j++){
BroadcastReceiver sent = new BroadcastReceiver(){
public void onReceive(Context arg0, Intent arg1) {
String[] arg = arg1.getAction().split(KEY_SMS_SENT);
String phoneNo = Utils.setSimpleFormatNumber(arg[1]);
String count = arg[0];
String parts = count.split(KEY_SMS_PART_NO)[0];
String partNo = count.split(KEY_SMS_PART_NO)[1];
sentReceivers.remove(this);
unregisterReceiver(this);
switch (getResultCode())
{
case Activity.RESULT_OK:
if(parts.equals(partNo)){
Toast.makeText(getBaseContext(), context.getString(R.string.sms_sent_message),
Toast.LENGTH_SHORT).show();
dbAdapter.createSentSMS(KEY_SMS_TYPE_SENT, phoneNo, currentMessage, Utils.getTimeStamp());
}
break;
case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
if(parts.equals(partNo)){
Toast.makeText(getBaseContext(), context.getString(R.string.sms_generic_failure_message),
Toast.LENGTH_SHORT).show();
dbAdapter.createSentSMS(KEY_SMS_TYPE_FAILED, phoneNo, currentMessage, Utils.getTimeStamp());
}
break;
case SmsManager.RESULT_ERROR_NO_SERVICE:
if(parts.equals(partNo)){
Toast.makeText(getBaseContext(), context.getString(R.string.sms_no_service_message),
Toast.LENGTH_SHORT).show();
dbAdapter.createSentSMS(KEY_SMS_TYPE_FAILED, phoneNo, currentMessage, Utils.getTimeStamp());
}
break;
case SmsManager.RESULT_ERROR_NULL_PDU:
if(parts.equals(partNo)){
Toast.makeText(getBaseContext(), context.getString(R.string.sms_null_pdu_message),
Toast.LENGTH_SHORT).show();
dbAdapter.createSentSMS(KEY_SMS_TYPE_FAILED, phoneNo, currentMessage, Utils.getTimeStamp());
}
break;
case SmsManager.RESULT_ERROR_RADIO_OFF:
if(parts.equals(partNo)){
Toast.makeText(getBaseContext(), context.getString(R.string.sms_radio_off_message),
Toast.LENGTH_SHORT).show();
dbAdapter.createSentSMS(KEY_SMS_TYPE_FAILED, phoneNo, currentMessage, Utils.getTimeStamp());
}
break;
}
}
};
registerReceiver(sent, new IntentFilter(String.valueOf(parts.size()-1) + KEY_SMS_PART_NO + String.valueOf(j) + KEY_SMS_SENT + phoneNumbers[i]));
//I add the BroadcastReceiver-s to a Vector in order to keep track of them
sentReceivers.add(sent);
}
}
//I use an IntentService to do the actual sending
Intent intent = new Intent(context, SMSSendService.class);
intent.putExtra(KEY_SELECTED_PHONE_NUMBERS, phoneNumbers);
intent.putExtra(KEY_SMS_MESSAGE, message);
intent.putExtra(KEY_ACTION, KEY_REQUEST_SEND_SMS);
startService(intent);
}
這是IntentService中的代碼:
public void sendSMS(String phoneNo, String message, SmsManager sms)
{
ArrayList<String> parts = sms.divideMessage(message);
ArrayList<PendingIntent> sentPIs = new ArrayList<PendingIntent>();
for(int i=0; i<parts.size(); i++){
sentPIs.add(PendingIntent.getBroadcast(context, 0, new Intent(String.valueOf(parts.size()-1) + KEY_SMS_PART_NO + String.valueOf(i) + KEY_SMS_SENT + phoneNo), 0));
}
sms.sendMultipartTextMessage(phoneNo, null, parts, sentPIs, deliveredPIs);
}
您可以創建一個服務(不是IntentService),在其中可以實例化所有BroadcastReceivers並執行發送SMS邏輯。 這樣,您的SMS-es將在后台發送,您可以攔截已發送/已發送的廣播。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.