2012-10-22 85 views
0

我想打一個應用程序發送sms.I我使用下面的代碼爲purpose.But我得到一些廣播接收器exception.This是代碼:獲取異常而發送短信

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 [email protected] 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 [email protected] 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) 

請幫助我。提前感謝。

+0

你錯過了對'unregisterReceiver()'的調用嗎? – MarchingHome

+0

可能重複[this](http://stackoverflow.com/questions/9781769/activity-has-leaked-intentreceiver)問題 – 13hsoj

+0

看到這篇文章http://stackoverflow.com/questions/9078390/intentrecieverleakedexception-are-you -missing-A-打電話到unregisterreceiver –

回答

2

定義BroadcastReceiver以外sendSMS方法(作爲全局對象)。然後在您的活動的onPause中,調用unregisterReceiver註銷偵聽器。