title: Android - Exception Finalize class having broadcastreceiver inside
body: <p>I have made a class and inside the class <strong>constructor</strong> I created a <strong>broadcastreceiver</strong> for SMS and in <strong>finalize</strong> method class I do <strong>unregister</strong> the reciver .</p>
  
  <p>Now : my application have 3 tabs each tab have activity when <strong>OnCreate</strong> of the activity I do create instance of my class  (Create BroadcastReceiver for the current activity) and <strong>OnStop</strong> I do finalize to the class.</p>
  
  <p>The problem is when i moving in tabs many time i get force close application with exception as the last show in this post .</p>
  
  <p>MYCLASS CODE:</p>
  
  <pre><code>package com.example.tawasol1;
  
  import android.content.BroadcastReceiver;
  import android.content.Context;
  import android.content.Intent;
  import android.content.IntentFilter;
  import android.os.Bundle;
  import android.telephony.SmsMessage;
  import android.util.Log;
  import android.widget.Toast;
  
  import com.example.tawasol1.Cls_stringManager.MessageType;
  import com.example.tawasol1.Cls_stringManager.QueryMessge;
  
  abstract class RecieverEvents{
      abstract void OnAccountNotRegistered(String Sender,String Message,String Provider);
      abstract void OnAccountNoBalance(String Sender,String Message,String Provider);
      abstract void OnAccountActivatedOk(String Sender,String Message,String Provider);
      abstract void OnAccountRegisterNoMoney(String Sender,String Message);
      abstract void OnMessage(String Sender,String Message,String FromNumber);
      abstract void OnEnvitationMessage(String Sender,String Message,String FromNumber);
  
  
  }
  
  public class Cls_CreateReceiver  {
      private Context mContext ;
      private RecieverEvents mReceiverEvnts ;
      private BroadcastReceiver CurrentReceiver ;
      private String smsSender  ;
      private String smsBody  ;
  //  private Intent CheckPage ;
  
  
  
      Cls_CreateReceiver(Context c ,int Priority,  RecieverEvents RecEvents){
          mContext =c ;
          mReceiverEvnts = RecEvents ;
  
  
          //====================== Body Of receiver 
  
          IntentFilter fp=new IntentFilter();
          fp.addAction("android.provider.Telephony.SMS_RECEIVED");
          fp.setPriority(Priority);
  
  
          CurrentReceiver = new BroadcastReceiver(){
  
              @Override
              public void onReceive(Context conx, Intent recIntent) {
  
  
  
                      //===================== Get Message Extras  
                  Bundle IntExtras = recIntent.getExtras() ;
                   if (IntExtras == null)
                         return;
                  Object[] pdus = (Object[]) IntExtras.get("pdus");
  
                  // get message from bundle intent 
                  for (int i = 0; i &lt; pdus.length; i++) {
  
                        SmsMessage SMessage = SmsMessage.createFromPdu((byte[]) pdus[i]);
                        smsSender = SMessage.getOriginatingAddress();
                        smsBody = SMessage.getMessageBody().toString();
  
                          }    
  
                      //======================End Message Extras      
  
                  Boolean isSystemMessage = false ;
                  QueryMessge retMsgQuery = QueryMessge.None ;    
  
                  cls_database smsCheck =  new cls_database(mContext) ;
                  Cls_stringManager StrManager = new Cls_stringManager();
  
  
                  isSystemMessage = smsCheck.isOurSystemMessage(smsSender);
  
                  //================= isSystemMessage     
                  if (isSystemMessage)
                  {
                  this.abortBroadcast() ; 
  
  
  
  
  
                          if (StrManager.isQueryMessage(smsBody))
                          {
  
                              Log.d("isQueryMessage:", "YES");
  
                              retMsgQuery = StrManager.GetQueryMsg(smsBody) ;
  
                              switch (retMsgQuery) {
                              case None:
                                  // Toast.makeText(c,"Error Message Query Type" , Toast.LENGTH_LONG).show();
                              break;
  
                              case NoBalance :
                                  mReceiverEvnts.OnAccountNoBalance(smsSender, smsBody, "");
                                      break ; 
                              case AccountActivated :
  
                                  mReceiverEvnts.OnAccountNotRegistered(smsSender, smsBody, "");
                                  break ;
                              case NoRegisteredAccount :
                                  mReceiverEvnts.OnAccountNotRegistered(smsSender, smsBody, "") ;
  
                                  break ;
                              default :
                                  Log.d("Receiver Other Msg:", smsBody);
                                  Log.d("Receiver Other Msg:", smsSender);
  
                                  if(smsSender.equals("TASHBIK"))
                                  {
                                      Log.d("Receiver Other Msg:Tashbik", smsBody);
                                      mReceiverEvnts.OnAccountRegisterNoMoney(smsSender, smsBody) ;
  
                                  }
  
                              } //switch Case 
  
  
  
  
                          }//StrManager.isQueryMessage
                          else
                          {
                              Log.d("isQueryMessage:", "NOT");
  
                              if (StrManager.isAccountExpireNoRegistered(smsBody)){
                                  mReceiverEvnts.OnAccountNotRegistered(smsSender, smsBody, "") ;
                              return;
                              }
  
                              if (StrManager.isNoBalaceMessage(smsBody)){
                                  mReceiverEvnts.OnAccountNoBalance(smsSender, smsBody, "") ;
                              return;
                              }
  
  
                              String ChatterMobile = "" ;
                              String ChatterMessage="" ;
  
                              MessageType MsgType = MessageType.None ;
  
                              MsgType = StrManager.GetMessageType(smsBody);
  
  
                              Log.d("Reciver&gt;HasBody:", StrManager.hasBody(smsBody).toString());
                              Log.d("Reciver&gt;HasMobile:", StrManager.hasMobile(smsBody).toString());
  
                              Log.d("Reciver&gt;Mobile:", StrManager.GetMobileFromMessage(smsBody));
                              Log.d("Reciver&gt;Msg:", StrManager.GetMessageBody(smsBody));
  
                              if(StrManager.hasMobile(smsBody))
                                  ChatterMobile = StrManager.GetMobileFromMessage(smsBody);
  
                              if (StrManager.hasBody(smsBody))
                                  ChatterMessage = StrManager.GetMessageBody(smsBody);
  
  
  
                              switch (MsgType) {
                              case Chatting:
                                  mReceiverEvnts.OnMessage(smsSender, smsBody, "CHAT");
                                  break;
                              case DirectMessage:
                                  mReceiverEvnts.OnMessage(smsSender, ChatterMessage.trim(), ChatterMobile.trim());
                                  break;
                              case Invitation :
                                  mReceiverEvnts.OnEnvitationMessage(smsSender, smsBody, ChatterMobile.trim());
                                  break ;
                              default:
  
                                  if(smsSender.equals("TASHBIK"))
                                  {
                                      Log.d("Receiver Other Msg:Tashbik", smsBody);
                                      mReceiverEvnts.OnAccountRegisterNoMoney(smsSender, smsBody) ;
  
                                  }
  
                                  break;
                              }
  
  
  
  
  
  
  
  
                          }//StrManager.isQueryMessage
  
                  }//================= isSystemMessage or Not
  
  
  
  
  
              } // End onReceive event
  
   };
  //====================End Of Receiver 
  
  
          mContext.registerReceiver(CurrentReceiver,fp);
  
  } //End Of Constructor 
  
  public void UnregisterReceiver(){
      mContext.unregisterReceiver(CurrentReceiver);
  
  }   
  
  
  @Override
  protected void finalize() throws Throwable {
  
      mContext.unregisterReceiver(CurrentReceiver);
  
      super.finalize();
  } 
  
  
  
  }
  </code></pre>
  
  <p>ERROR LOG :</p>
  
  <pre><code>03-04 14:48:57.331: E/System(22346): Uncaught exception thrown by finalizer
  03-04 14:48:57.331: E/System(22346): java.lang.IllegalArgumentException: Receiver not registered: com.example.tawasol1.Cls_CreateReceiver$1@41d64278
  03-04 14:48:57.331: E/System(22346):    at android.app.LoadedApk.forgetReceiverDispatcher(LoadedApk.java:654)
  03-04 14:48:57.331: E/System(22346):    at android.app.ContextImpl.unregisterReceiver(ContextImpl.java:1297)
  03-04 14:48:57.331: E/System(22346):    at com.example.tawasol1.Cls_CreateReceiver.finalize(Cls_CreateReceiver.java:223)
  03-04 14:48:57.331: E/System(22346):    at java.lang.Daemons$FinalizerDaemon.doFinalize(Daemons.java:186)
  03-04 14:48:57.331: E/System(22346):    at java.lang.Daemons$FinalizerDaemon.run(Daemons.java:169)
  03-04 14:48:57.331: E/System(22346):    at java.lang.Thread.run(Thread.java:856)
  </code></pre>