2012-12-23 45 views
0

我正在使用此代碼發送短信。廣播接收機崩潰了一些時間android

private void sendSMS(String phoneNumber, String message) { 

     System.out.println("SMS: " + message); 
     System.out.println("to " + phoneNumber); 

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


     SmsManager sm = SmsManager.getDefault(); 
     ArrayList<String> parts = sm.divideMessage(message); 
     int numParts = parts.size(); 

     ArrayList<PendingIntent> sentIntents = new ArrayList<PendingIntent>(); 
     ArrayList<PendingIntent> deliveryIntents = new ArrayList<PendingIntent>(); 

     for (int i = 0; i < numParts; i++) { 
      sentIntents.add(sentPI); 
      deliveryIntents.add(deliveredPI); 
     } 

     sm.sendMultipartTextMessage(phoneNumber, null, parts, sentIntents, 
       deliveryIntents); 
    } 
} 

有時候,我得到這個錯誤,而其他時候,它發揮正常全無錯誤可言:

android.app.IntentReceiverLeaked: Activity com.example.senddemo.MainActivity has leaked IntentReceiver [email protected] that was originally registered here. Are you missing a call to unregisterReceiver()? 
12-23 15:23:32.800 E/ActivityThread(27024):  at android.app.LoadedApk$ReceiverDispatcher.<init>(LoadedApk.java:756) 
12-23 15:23:32.800 E/ActivityThread(27024):  at android.app.LoadedApk.getReceiverDispatcher(LoadedApk.java:551) 
12-23 15:23:32.800 E/ActivityThread(27024):  at android.app.ContextImpl.registerReceiverInternal(ContextImpl.java:836) 
12-23 15:23:32.800 E/ActivityThread(27024):  at android.app.ContextImpl.registerReceiver(ContextImpl.java:823) 
12-23 15:23:32.800 E/ActivityThread(27024):  at android.app.ContextImpl.registerReceiver(ContextImpl.java:817) 
12-23 15:23:32.800 E/ActivityThread(27024):  at android.content.ContextWrapper.registerReceiver(ContextWrapper.java:318) 
12-23 15:23:32.800 E/ActivityThread(27024):  at com.example.senddemo.MainActivity.sendSMS(MainActivity.java:331) 
12-23 15:23:32.800 E/ActivityThread(27024):  at com.example.senddemo.MainActivity.access$0(MainActivity.java:290) 
12-23 15:23:32.800 E/ActivityThread(27024):  at com.example.senddemo.MainActivity$2.onClick(MainActivity.java:120) 
12-23 15:23:32.800 E/ActivityThread(27024):  at android.view.View.performClick(View.java:2552) 
12-23 15:23:32.800 E/ActivityThread(27024):  at android.view.View$PerformClick.run(View.java:9229) 
12-23 15:23:32.800 E/ActivityThread(27024):  at android.os.Handler.handleCallback(Handler.java:587) 
12-23 15:23:32.800 E/ActivityThread(27024):  at android.os.Handler.dispatchMessage(Handler.java:92) 
12-23 15:23:32.800 E/ActivityThread(27024):  at android.os.Looper.loop(Looper.java:138) 
12-23 15:23:32.800 E/ActivityThread(27024):  at android.app.ActivityThread.main(ActivityThread.java:3701) 
12-23 15:23:32.800 E/ActivityThread(27024):  at java.lang.reflect.Method.invokeNative(Native Method) 
12-23 15:23:32.800 E/ActivityThread(27024):  at java.lang.reflect.Method.invoke(Method.java:507) 
12-23 15:23:32.800 E/ActivityThread(27024):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:878) 
12-23 15:23:32.800 E/ActivityThread(27024):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:636) 
12-23 15:23:32.800 E/ActivityThread(27024):  at dalvik.system.NativeStart.main(Native Method) 
12-23 15:23:32.810 E/ActivityThread(27024): Activity com.example.senddemo.MainActivity has leaked IntentReceiver [email protected] that was originally registered here. Are you missing a call to unregisterReceiver()? 

我需要在我的XML文件中或在我的OnDestry功能添加任何東西?因爲我不喜歡廣播接收機。爲什麼它有時會播放,有些則不是?

回答

1

您必須保存廣播接收器的參考,並取消註冊接收器。

改變你這樣的代碼:

private BroadcastReceiver mSentReceiver; 
private BroadcastReceiver mDeleiveryReceiver; 



private void sendSMS(String phoneNumber, String message) { 

     System.out.println("SMS: " + message); 
     System.out.println("to " + phoneNumber); 

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

mSentReceiver = 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; 
       } 
      } 
     }); 

     registerReceiver(mSentReceiver, new IntentFilter(SENT)); 

     // ---when the SMS has been delivered--- 

mDeleiveryReceiver = 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; 
       } 
      } 
     }); 
     registerReceiver(mDeleiveryReceiver, new IntentFilter(DELIVERED)); 

//  SmsManager sms = SmsManager.getDefault(); 
//  sms.sendTextMessage(phoneNumber, null, message, sentPI, deliveredPI); 


     SmsManager sm = SmsManager.getDefault(); 
     ArrayList<String> parts = sm.divideMessage(message); 
     int numParts = parts.size(); 

     ArrayList<PendingIntent> sentIntents = new ArrayList<PendingIntent>(); 
     ArrayList<PendingIntent> deliveryIntents = new ArrayList<PendingIntent>(); 

     for (int i = 0; i < numParts; i++) { 
      sentIntents.add(sentPI); 
      deliveryIntents.add(deliveredPI); 
     } 

     sm.sendMultipartTextMessage(phoneNumber, null, parts, sentIntents, 
       deliveryIntents); 
    } 
} 


@onDestroy(){ 
if(mSentReceiver != null) unregisterReceiver(mSentReceiver); 
if(mDeleiveryReceiver != null) unregisterReceiver(mDeleiveryReceiver); 
}