无法注册已发送短信的接收方

时间:2012-03-21 08:04:39

标签: android broadcastreceiver

我有一个AlarmReceiver类,当警报在特定时间响铃时设置(在警报中设置)

我在AlarmReciever类中发送短信,但我无法注册短信发送的接收方。

这是代码

                  private int sendSMS(String messageToSend,String contactsToSend,Context context)
       {        
                StringTokenizer  tok=new StringTokenizer(contactsToSend);
                int n=tok.countTokens();
                String SENT = "SMS_SENT";
                String DELIVERED = "SMS_DELIVERED";
                final AlertDialog dialog = new AlertDialog.Builder(context).create();

                PendingIntent sentPI = PendingIntent.getBroadcast(context, 0,new Intent(SENT), 0);

                //---when the SMS has been sent---
                **/*registerReceiver(new BroadcastReceiver**(){
                    @Override
                    public void onReceive(Context arg0, Intent arg1) {
                        switch (getResultCode())
                        {
                                case Activity.RESULT_OK:
                                    break;

                                case SmsManager.RESULT_ERROR_GENERIC_FAILURE:

                                    break;
                                case SmsManager.RESULT_ERROR_NO_SERVICE:

                                    break;
                                case SmsManager.RESULT_ERROR_NULL_PDU:

                                    break;
                                case SmsManager.RESULT_ERROR_RADIO_OFF:

                                    break;
                        }
                        return;
                    }
                }, new IntentFilter(SENT));*/



        SmsManager sms = SmsManager.getDefault();
        String phoneNumber;
        for(int i=0;i<n;i++)
        {
                phoneNumber=tok.nextToken();
                sms.sendTextMessage(phoneNumber, null, messageToSend, null,null); 
        }
        return 1;

      }

当我从行中删除注释(以粗体显示)时,它会给出错误。 那么我如何注册接收器来检查短信是否被发送。

1 个答案:

答案 0 :(得分:0)

而不是注册接收者,您可以创建这样的服务

  public class SMSObserver extends BroadcastReceiver 
    {
        static final String ACTION ="android.provider.Telephony.SMS_SENT";

        @Override 
        public void onReceive(Context context, Intent intent) 
        { 
            if (intent.getAction().equals(ACTION)) 
            {
                     //your action code here
            }            
        }
    }

but if you want register reciever you can use in your onCreate()

 // SendSmsObserver smsObeserver = (new SendSmsObserver(new Handler()));
       // ContentResolver contentResolver = this.getContentResolver();
       // contentResolver.registerContentObserver(Uri.parse("content://sms"),true, smsObeserver);