0

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)

助けてください。よろしくお願いします。

4

2 に答える 2

2

sendSMSBroadcastReceiverメソッドの外側で(グローバル オブジェクトとして)定義します。次に、 Activity のonPauseで、 unregisterReceiverを呼び出してリスナーの登録を解除します。

于 2012-10-22T07:28:27.047 に答える
1

答えはここここにあります。次回は、Exception取得している を Google で検索してください。

于 2012-10-22T07:28:02.290 に答える