如何识别哪些短信已在Android中获得投放报告 [英] How to identify which SMS has got Delivery report in Android
问题描述
我的应用程序通过基于记录的大小合并记录,从而从sqlite数据库和SMS检索记录到其他应用程序,我难以确定哪些消息具有传递报告,而哪些消息没有.我在reciveMethods上使用BroadcastReceiver 但是不幸的是我无法识别身份.如果有人建议我该如何处理此问题,我将很乐意.或其他更好的方法
My application retrieves records from sqlite database and SMS to other application by merging the records based on the size of the record, i am intruble to identify which messages have got the delivery report and which does'not . i am using BroadcastReceiver on reciveMethods but unfortunately i can't understand how to identify . i would love if any one of you suggest me the how to handle this problem. or any other better way
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);
}
}
推荐答案
您需要将所需的数据放入PendingIntent
中,该数据将在传送完成(或失败)时广播.执行此操作时,需要确保为每个消息创建唯一的PendingIntent
(可以通过在Intent
上设置唯一的ACTION来执行此操作).这是一个例子.
You need to put the data you need into the PendingIntent
that will be broadcast when the delivery completes (or fails). When doing this you need to make sure that you create a unique PendingIntent
for each message (you can do this by setting a unique ACTION on the Intent
). Here's an example.
代替此:
PendingIntent deliveredPI = PendingIntent.getBroadcast(this, 0,
new Intent(DELIVERED), 0);
执行此操作:
long messageID = ... // This is the message ID, ie: the row ID in the SQLite database that uniquely identifies this message
Intent deliveredIntent = new Intent(DELIVERED + messageID); // Use unique action string
deliveredIntent.putExtra("messageID", messageID); // Add message ID as extra
PendingIntent deliveredPI = PendingIntent.getBroadcast(this, 0,
deliveredIntent, PendingIntent.FLAG_ONE_SHOT);
注册接收器时,请确保使用正确的IntentFilter
:
When you register the receiver, be sure to use the correct IntentFilter
:
registerReceiver(new BroadcastReceiver(){
...
}, new IntentFilter(DELIVERED + messageID));
现在,当广播交货Intent
时,您可以像这样在onReceive()
中从其中获取数据:
Now, when the delivery Intent
is broadcast, you can get the data from it in onReceive()
like this:
public void onReceive(Context arg0, Intent arg1) {
long messageID = arg1.getLongExtra("messageID", -1L);
if (messageID != -1L) {
// This is the message ID (row ID) of the message that was delivered
... do your processing here
}
这篇关于如何识别哪些短信已在Android中获得投放报告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!