SMSManager Android报告发送(多个发送短信)

时间:2016-08-16 04:31:12

标签: java android broadcastreceiver sms

我有这个在模拟器上运行的特定代码。 (我还没有测试过真正的设备)。目的是向所有数字发送相同的文本消息。 使用BroadcastReceiver,我已经在其中进行了一些自定义。

我想知道,如何获得每个成功(交付)短信的报告? 请考虑我的哪一个选项是正确的:

  1. 我是否需要分配PendingIntent的每次调用 registerReceiver?
  2. 或仅仅设置一次就够了吗? (请检查下面的代码)。
  3. 因为我担心的是,所有的消息都被发送了。但是我没有获得交付状态(由于广播接收者分配的错误)。

    private void sendingSMS(){
            // this method will be repeatedly called because the phoneNumbers are stored inside the ArrayList
    
            PendingIntent sentPI = null, deliveryPI = null;
    
            if(smsMan == null){
                smsMan = SmsManager.getDefault();
    
                Intent sentIntent = new Intent("SENT_SMS_ACTION");
                sentIntent.putExtra("PhoneNum", phoneNumbersSelected.get(startIndexPhoneNumbers));
    
                Intent deliveryIntent = new Intent("DELIVERED_SMS_ACTION");
                deliveryIntent.putExtra("PhoneNum", phoneNumbersSelected.get(startIndexPhoneNumbers));
    
                sentPI = PendingIntent.getBroadcast(getApplicationContext(), 0, sentIntent, 0);
                deliveryPI = PendingIntent.getBroadcast(getApplicationContext(), 0, deliveryIntent, 0);
    
                registerReceiver(new MyBReceiver(MyBReceiver.SENT), new IntentFilter("SENT_SMS_ACTION"));
                registerReceiver(new MyBReceiver(MyBReceiver.DELIVERY), new IntentFilter("DELIVERED_SMS_ACTION"));
    
            } 
    
            smsMan.sendTextMessage(phoneNumbersSelected.get(startIndexPhoneNumbers), null, composedMessage, sentPI, deliveryPI);
    
            Toast.makeText(getApplicationContext(), "Trying to send SMS to " + phoneNumbersSelected.get(startIndexPhoneNumbers), Toast.LENGTH_SHORT).show();
        }
    

    BroadCastReceiver就在这里

    package project.fgroupindonesia.smsmassal;
    
    import android.app.Activity;
    import android.content.BroadcastReceiver;
    import android.content.Context;
    import android.content.Intent;
    import android.telephony.SmsManager;
    import android.widget.Toast;
    
    public class MyBReceiver extends BroadcastReceiver{
    
        public MyBReceiver(){
            // default
            setUsage(SENT);
        }
    
        public MyBReceiver(int mode){
            setUsage(mode);
        }
    
        public static int SENT = 0, DELIVERY = 1;
        int usage;
    
        public void setUsage(int val){
            usage = val;
        }
    
        private int getUsage(){
            return usage;
        }
    
        @Override
        public void onReceive(Context arg0, Intent arg1) {
    
            if(this.getUsage() == SENT){
    
                switch(getResultCode()){
                case Activity.RESULT_OK:
                    Toast.makeText(arg0, "SMS is sent to " + arg1.getStringExtra("PhoneNum"), Toast.LENGTH_SHORT).show();
                    break;
                case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
                    Toast.makeText(arg0, "ERROR GENERIC while sending to " + arg1.getStringExtra("PhoneNum"), Toast.LENGTH_SHORT).show();
                    break;
                case SmsManager.RESULT_ERROR_RADIO_OFF:
                    Toast.makeText(arg0, "RADIO OFF while sending to " + arg1.getStringExtra("PhoneNum"), Toast.LENGTH_SHORT).show();
                    break;
                case SmsManager.RESULT_ERROR_NULL_PDU:
                    Toast.makeText(arg0, "ERROR NULL while sending to " + arg1.getStringExtra("PhoneNum"), Toast.LENGTH_SHORT).show();
                    break;
                }
    
            } else if(this.getUsage() == DELIVERY){
    
                // some code
                Toast.makeText(arg0, "SMS is delivered to " + arg1.getStringExtra("PhoneNum"), Toast.LENGTH_SHORT).show();
    
            }
    
        }
    
    }
    

0 个答案:

没有答案