发送短信时获得异常

时间:2012-10-22 07:23:20

标签: android sms

我想创建一个发送短信的应用程序。我正在使用以下代码。但是我得到了一些BroadCastReceiver异常。这是代码:

  public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.sendsmslayout);        
        btnSendSMS = (Button) findViewById(R.id.btnSendSMS);
        txtPhoneNo = (EditText) findViewById(R.id.txtPhoneNo);
        txtMessage = (EditText) findViewById(R.id.txtMessage);

        /*
        Intent sendIntent = new Intent(Intent.ACTION_VIEW);
        sendIntent.putExtra("sms_body", "Content of the SMS goes here..."); 
        sendIntent.setType("vnd.android-dir/mms-sms");
        startActivity(sendIntent);
        */

        btnSendSMS.setOnClickListener(new View.OnClickListener() 
        {
            public void onClick(View v) 
            {               
                String phoneNo = txtPhoneNo.getText().toString();
                String message = txtMessage.getText().toString();               
                if (phoneNo.length()>0 && message.length()>0)                
                    sendSMS(phoneNo, message);                
                else
                    Toast.makeText(getBaseContext(), 
                        "Please enter both phone number and message.", 
                        Toast.LENGTH_SHORT).show();
            }
        });        
    }

    //---sends a SMS message to another device---
    private void sendSMS(String phoneNumber, String message)
    {      
        /*
        PendingIntent pi = PendingIntent.getActivity(this, 0,
                new Intent(this, test.class), 0);                
            SmsManager sms = SmsManager.getDefault();
            sms.sendTextMessage(phoneNumber, null, message, pi, null);        
        */

        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);

        //---when the SMS has been sent---
        registerReceiver(new BroadcastReceiver(){
            @Override
            public void onReceive(Context arg0, Intent arg1) {
                switch (getResultCode())
                {
                    case Activity.RESULT_OK:
                        Toast.makeText(getBaseContext(), "SMS sent", 
                                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(phoneNumber, null, message, sentPI, deliveredPI);               
    }    

这是一个例外:

10-22 12:38:37.461: E/ActivityThread(1219): Activity org.secure.sms.SendSMS has leaked IntentReceiver org.secure.sms.SendSMS$3@44e98630 that was originally registered here. Are you missing a call to unregisterReceiver()?
10-22 12:38:37.461: E/ActivityThread(1219): android.app.IntentReceiverLeaked: Activity org.secure.sms.SendSMS has leaked IntentReceiver org.secure.sms.SendSMS$3@44e98630 that was originally registered here. Are you missing a call to unregisterReceiver()?
10-22 12:38:37.461: E/ActivityThread(1219):     at android.app.ActivityThread$PackageInfo$ReceiverDispatcher.<init>(ActivityThread.java:797)
10-22 12:38:37.461: E/ActivityThread(1219):     at android.app.ActivityThread$PackageInfo.getReceiverDispatcher(ActivityThread.java:608)
10-22 12:38:37.461: E/ActivityThread(1219):     at android.app.ApplicationContext.registerReceiverInternal(ApplicationContext.java:724)
10-22 12:38:37.461: E/ActivityThread(1219):     at android.app.ApplicationContext.registerReceiver(ApplicationContext.java:711)
10-22 12:38:37.461: E/ActivityThread(1219):     at android.app.ApplicationContext.registerReceiver(ApplicationContext.java:705)
10-22 12:38:37.461: E/ActivityThread(1219):     at android.content.ContextWrapper.registerReceiver(ContextWrapper.java:308)
10-22 12:38:37.461: E/ActivityThread(1219):     at org.secure.sms.SendSMS.sendSMS(SendSMS.java:105)
10-22 12:38:37.461: E/ActivityThread(1219):     at org.secure.sms.SendSMS.access$0(SendSMS.java:56)
10-22 12:38:37.461: E/ActivityThread(1219):     at org.secure.sms.SendSMS$1.onClick(SendSMS.java:46)
10-22 12:38:37.461: E/ActivityThread(1219):     at android.view.View.performClick(View.java:2364)
10-22 12:38:37.461: E/ActivityThread(1219):     at android.view.View.onTouchEvent(View.java:4179)
10-22 12:38:37.461: E/ActivityThread(1219):     at android.widget.TextView.onTouchEvent(TextView.java:6540)
10-22 12:38:37.461: E/ActivityThread(1219):     at android.view.View.dispatchTouchEvent(View.java:3709)
10-22 12:38:37.461: E/ActivityThread(1219):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
10-22 12:38:37.461: E/ActivityThread(1219):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
10-22 12:38:37.461: E/ActivityThread(1219):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
10-22 12:38:37.461: E/ActivityThread(1219):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
10-22 12:38:37.461: E/ActivityThread(1219):     at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1659)
10-22 12:38:37.461: E/ActivityThread(1219):     at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1107)
10-22 12:38:37.461: E/ActivityThread(1219):     at android.app.Activity.dispatchTouchEvent(Activity.java:2061)
10-22 12:38:37.461: E/ActivityThread(1219):     at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1643)
10-22 12:38:37.461: E/ActivityThread(1219):     at android.view.ViewRoot.handleMessage(ViewRoot.java:1691)
10-22 12:38:37.461: E/ActivityThread(1219):     at android.os.Handler.dispatchMessage(Handler.java:99)
10-22 12:38:37.461: E/ActivityThread(1219):     at android.os.Looper.loop(Looper.java:123)
10-22 12:38:37.461: E/ActivityThread(1219):     at android.app.ActivityThread.main(ActivityThread.java:4363)
10-22 12:38:37.461: E/ActivityThread(1219):     at java.lang.reflect.Method.invokeNative(Native Method)
10-22 12:38:37.461: E/ActivityThread(1219):     at java.lang.reflect.Method.invoke(Method.java:521)
10-22 12:38:37.461: E/ActivityThread(1219):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
10-22 12:38:37.461: E/ActivityThread(1219):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
10-22 12:38:37.461: E/ActivityThread(1219):     at dalvik.system.NativeStart.main(Native Method)

请帮帮我。谢谢。

2 个答案:

答案 0 :(得分:2)

sendSMS 方法之外定义BroadcastReceiver(作为全局对象)。然后在您的Activity的 onPause 中,调用 unregisterReceiver 取消注册侦听器。

答案 1 :(得分:1)

您可以找到答案herehere。下次请谷歌了解您的Exception