0

我想制作一个用于发送短信的应用程序。我为此目的使用以下代码。但我收到了一些 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)

请帮助我。在此先感谢。

4

2 回答 2

2

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

于 2012-10-22T07:28:27.047 回答
1

你可以在这里这里找到你的答案。下一次,请谷歌为Exception你得到。

于 2012-10-22T07:28:02.290 回答