如何识别哪些短信已在Android中获得发送报告

如何识别哪些短信已在Android中获得发送报告,第1张

概述我的应用程序通过根据记录的大小合并记录,从sqlite数据库和SMS检索记录到其他应用程序,我无法识别哪些消息已经获得了交付报告,哪些消息没有.我在reciveMethods上使用BroadcastReceiver 但不幸的是我无法理解如何识别.我很乐意,如果你们中的任何一个人建议我如何处理这个问题.或任何其他更好的方式 public class MessageMangerActivity ext 我的应用程序通过根据记录的大小合并记录,从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,new Intent(SENT),0);        PendingIntent deliveredPI = PendingIntent.getbroadcast(this,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,"SMS delivered",Toast.LENGTH_SHORT).show();                        break;                    case Activity.RESulT_CANCELED:                        Toast.makeText(getBaseContext(),"SMS not delivered",new IntentFilter(DEliVERED));    ``    SmsManager sms = SmsManager.getDefault();        sms.sendTextMessage(receiverNumber,null,messageBody,sentPI,deliveredPI);    }}
解决方法 您需要将所需的数据放入PendingIntent中,以便在传递完成(或失败)时广播.执行此 *** 作时,您需要确保为每条消息创建唯一的PendingIntent(您可以通过在Intent上设置唯一的ACTION来完成此 *** 作).这是一个例子.

而不是这个:

PendingIntent deliveredPI = PendingIntent.getbroadcast(this,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,deliveredIntent,PendingIntent.FLAG_ONE_SHOT);

注册接收器时,请务必使用正确的IntentFilter:

registerReceiver(new broadcastReceiver(){        ...    },new IntentFilter(DEliVERED + messageID));

现在,当广播传递Intent时,你可以在onReceive()中获取它的数据,如下所示:

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中获得发送报告全部内容,希望文章能够帮你解决如何识别哪些短信已在Android中获得发送报告所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1128914.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-30
下一篇 2022-05-30

发表评论

登录后才能评论

评论列表(0条)

保存