我的应用程序通过根据记录的大小合并记录来从 sqlite 数据库和 SMS 中检索记录到其他应用程序,我无法确定哪些消息已获得传递报告而哪些没有。我在 reciveMethods 上使用 BroadcastReceiver 但不幸的是我不明白如何识别。如果你们中的任何人建议我如何处理这个问题,我会很高兴。或任何其他更好的方式
public class MessageMangerActivity extends Activity {
private String MessageBody;
public enum MessageType
{
s,r,c
}
String ReceiverNumber="5556";
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.messages);
try {
SubmitReportToServer();
} catch (Exception e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
}
}
private void SubmitReportToServer() throws Exception {
// MessageHandler messageHandler =new MessageHandler();
//get Unsent Message
ReportAdapter reportAdapter= new ReportAdapter(this);
reportAdapter.open();
/* Submit Received Item report*/
Cursor receivingResults;
receivingResults=reportAdapter.FetchUnSentReceivingRecords();
int i=0;
if(receivingResults.getCount()!=-1) {
while(receivingResults.moveToNext() ){
MessageBody=MessageType.r + MessageBody +"|"+ receivingResults.getString(0)+"|" + receivingResults.getString(1)+"|"+receivingResults.getString(2)+"|"+
receivingResults.getString(3)+"|"+receivingResults.getString(4)+"|"+receivingResults.getString(5)+"|"+receivingResults.getString(6)+"|"+
receivingResults.getString(7)+"|" + receivingResults.getString(8)+"|"+ receivingResults.getString(9)+"|"+
receivingResults.getString(10)+"|"+receivingResults.getString(11)+",";
i++;
if (i > 1 ) { break; }
}
sendSMS(ReceiverNumber,MessageBody);
receivingResults.close();
/* Submit Issued Item report*/
Cursor issueResults;
issueResults=reportAdapter.FetchUnSentIssuedRecords();
i=0;
if(issueResults.getCount()!=-1) {
while(issueResults.moveToNext() ){
MessageBody=MessageBody + ","+ issueResults.getExtras();
i++;
if (i > 3 ) { break; }
}
sendSMS(ReceiverNumber,MessageBody);
issueResults.close();
}
}
/*Submit Vaccinated Children Report */
Cursor childrenResults;
childrenResults=reportAdapter.FetchUnSentVaccinatedChildrenRecords();
i=0;
if(childrenResults.getCount()!=-1) {
while(childrenResults.moveToNext() ){
MessageBody=MessageBody + ","+ childrenResults.toString();
i++;
if (i > 3 ) { break; }
}
sendSMS(ReceiverNumber,MessageBody);
childrenResults.close();
}
}
private void sendSMS(String receiverNumber, String messageBody) {
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 arg0, Intent arg1) {
switch (getResultCode())
{
case Activity.RESULT_OK:
Toast.makeText(getBaseContext(), "SMS sent",
//update Status
Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
Toast.makeText(getBaseContext(), "Generic failure",
Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_NO_SERVICE:
Toast.makeText(getBaseContext(), "No service",
Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_NULL_PDU:
Toast.makeText(getBaseContext(), "Null PDU",
Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_RADIO_OFF:
Toast.makeText(getBaseContext(), "Radio off",
Toast.LENGTH_SHORT).show();
break;
}
}
}, new IntentFilter(SENT));
//—when the SMS has been delivered—
registerReceiver(new BroadcastReceiver(){
@Override
public void onReceive(Context arg0, Intent arg1) {
switch (getResultCode())
{
case Activity.RESULT_OK:
Toast.makeText(getBaseContext(), "SMS delivered",
Toast.LENGTH_SHORT).show();
break;
case Activity.RESULT_CANCELED:
Toast.makeText(getBaseContext(), "SMS not delivered",
Toast.LENGTH_SHORT).show();
break;
}
}
}, new IntentFilter(DELIVERED));
`` SmsManager sms = SmsManager.getDefault();
sms.sendTextMessage(receiverNumber, null, messageBody, sentPI, deliveredPI);
}
}