我写了一个发送短信的程序。我的问题是,当我想为我的朋友发送例如五条消息并且他的电话被关闭时,在他打开电话之后,交付只是收到最后一条短信。 我的代码如下所示:
Intent newIntent_sent = new Intent(AppStatics.INTENT_SENT_SMS+this_messageUri);
Log.d("uri sending setting", this_messageUri.getPath());
newIntent_sent.putExtra(AppStatics.INTENT_SENT_ID,this_messageUri.getPath());
PendingIntent newsentPI = PendingIntent.getBroadcast(getBaseContext(), 0,newIntent_sent,PendingIntent.FLAG_ONE_SHOT);
registerReceiver (new BroadcastReceiver() {
@Override
public void onReceive(Context arg0, Intent arg1) {
// TODO Auto-generated method stub
String uri = arg1.getExtras().getString(AppStatics.INTENT_SENT_ID);
Log.d("Uri Sending", uri);
Send_Result sr = new Send_Result(Uri.parse(uri), getResultCode() );
Thread tr = new Thread(sr);
tr.start();
SendMessegeService.this.unregisterReceiver(this);
}
}, new IntentFilter(AppStatics.INTENT_SENT_SMS+this_messageUri));
sentPIs.add(newsentPI);
Intent newIntent_deliver = new Intent(AppStatics.INTENT_DELIVERED_SMS+this_messageUri);
Log.d("Uri deliver Setting", this_messageUri.getPath());
newIntent_deliver.putExtra(AppStatics.INTENT_DELIVERE_ID,this_messageUri.getPath());
PendingIntent deliverPI = PendingIntent.getBroadcast(
getBaseContext(), 0, newIntent_deliver,0);
registerReceiver(new BroadcastReceiver() {
@Override
public void onReceive(Context arg0, Intent arg1) {
// TODO Auto-generated method stub
String uri = arg1.getExtras().getString(AppStatics.INTENT_DELIVERE_ID);
Delivery_Result dr = new Delivery_Result(Uri.parse(uri), getResultCode());
Log.d("Uri deliver", uri);
Thread tr = new Thread(dr);
tr.start();
}
},new IntentFilter(AppStatics.INTENT_DELIVERED_SMS+this_messageUri));
deliverPIs.add(deliverPI);
这是我的线程类::
public class Send_Result implements Runnable
{
Uri messageUri =null;
int resultCode ;
String toastMessage ="";
public Send_Result(Uri MessageUri,int resultCode)
{
this.messageUri = MessageUri;
this.resultCode=resultCode;
}
@Override
public void run() {
// TODO Auto-generated method stub
ContentValues values = new ContentValues();
switch (resultCode)
{
case Activity.RESULT_OK:
values.put(DataBaseStatics.STATUS,
DataBaseStatics.STATUS_PENDING);
toastMessage = getBaseContext().getResources().getString(R.string.MessageIsSending_Title);
break;
case Activity.RESULT_CANCELED:
values.put(DataBaseStatics.STATUS,
DataBaseStatics.STATUS_FAILED);
toastMessage = getBaseContext().getResources().getString(R.string.MessageSendingFailed_Title);
break;
default:
values.put(DataBaseStatics.STATUS,
DataBaseStatics.STATUS_FAILED);
toastMessage = getBaseContext().getResources().getString(R.string.MessageSendingFailed_Title);
break;
}
getBaseContext().getContentResolver().update(
this_messageUri, values, null, null);
}
}
public class Delivery_Result implements Runnable
{
Uri messageUri =null;
int resultCode ;
String toastMessage;
public Delivery_Result(Uri MessageUri,int resultCode)
{
this.messageUri = MessageUri;
this.resultCode=resultCode;
}
@Override
public void run() {
// TODO Auto-generated method stub
ContentValues values = new ContentValues();
switch (resultCode)
{
case Activity.RESULT_OK :
values.put(DataBaseStatics.STATUS, DataBaseStatics.STATUS_COMPLETE);
toastMessage=getBaseContext().getResources().getString(R.string.MessageDeliverd_Title);
break;
default :
values.put(DataBaseStatics.STATUS, DataBaseStatics.STATUS_FAILED);
toastMessage=getBaseContext().getResources().getString(R.string.MessgeDeliverFailed_Title);
break;
}
getBaseContext().getContentResolver().update(this_messageUri, values, null, null);
}
}
这是我的发送消息方法:
public void SendMessage(String sendingNumber , int index)
{
String sNumber = sendingNumber;
PendingIntent sentPI = sentPIs.get(index);
PendingIntent deliverPI = deliverPIs.get(index);
SmsManager sms =SmsManager.getDefault();
ArrayList<String> msgparts = sms.divideMessage(message);
if (msgparts.size()<=1)
{
sms.sendTextMessage(sNumber, null, message, sentPI, deliverPI);
}
else
{
sms.sendMultipartTextMessage(sNumber, null, msgparts, sentPIs, deliverPIs);
}
}