title: how to handle orphaned pending intent by runtime exception
body: <p>Hi i want to ask a question on problem I faced. </p>
  
  <p>It involves: </p>
  
  <p><strong>activity lifecycle, receiver triggered by pending intent(generated by alarm) &amp; runtime exception.</strong></p>
  
  <p><strong>Assuming i got :</strong></p>
  
  <ul>
  <li>registered BroadcastReceiver which starts IntentService</li>
  <li>pending intent set via alarm to call BroadcastReceiver</li>
  <li><p>for example part of Activity class code which thrown runtime exception(here only to show the idea of disrupted activity lifecycle flow &amp; runtime exception): </p>
  
  <pre><code>@Override 
   protected void onCreate(Bundle savedInstanceState) {     
       super.onCreate(savedInstanceState); 
       if(true) { throw new RuntimeException("EXC"); 
   }
  
  @Override 
  protected void onStop() {
  
       // here we stop alarm or unregister receiver 
  }
  </code></pre></li>
  </ul>
  
  <p><strong>My observations:</strong></p>
  
  <ul>
  <li>after exception is thrown normal flow of activity lifecycle is broken &amp; ART is recreating "stack"</li>
  <li>in this case alarm couldn't be canceled! is still present until device is  reboot so pending intent is generated but "start condition" for it was not meet as in normal undisrupted flow</li>
  <li>BroadcastReceiver was/stays active (registered) regardless runtime exception so it is firing IntentService which abnormally generates not intended null pointer exception</li>
  </ul>
  
  <p><strong>Question:</strong></p>
  
  <ul>
  <li>the best approach which will guarantee to unregister receiver/deactivate orphaned pending intent(produced by alarm?) after exception is thrown to avoid call to intent service &amp; produce further exception, because as i mention android activity lifecycle is disrupted so it is not passing thru onPause/onStop method which in normally way i use to unregistering receiver/stop alarm/pending intent</li>
  </ul>
  
  <p><strong>What idea(solution) comes to my mind:</strong></p>
  
  <p>implements Thread.UncaughtExceptionHandler to unregister receiver/stop alarm ???</p>
  
  <p><strong>EDIT:</strong> </p>
  
  <p>After doing some digging:</p>
  
  <ol>
  <li><p>there are 2 ways to set UncaughtExceptionHandler:</p>
  
  <ul>
  <li>first "setUncaughtExceptionHandler" which applies to a single thread </li>
  <li>the second "setDefaultUncaughtExceptionHandler" to all threads in the VM. </li>
  </ul></li>
  <li><p>the app framework already provides a global uncaught exception handler.  The exception traces with the "AndroidRuntime" tag are generated here, and then the VM is killed: </p>
  
  <ul>
  <li>see frameworks/base/core/java/com/android/internal/os/RuntimeInit.java. </li>
  </ul></li>
  <li><p>Uncaught exceptions are first </p>
  
  <ul>
  <li>handled by the per-thread handler, </li>
  <li>then the per-thread-group handler, </li>
  <li>and then by the global handler. </li>
  </ul></li>
  </ol>
  
  <p>As i got some half-dead things sitting in memmory i got two choices:</p>
  
  <ul>
  <li>to shut the VM down, </li>
  <li>or lean up after crash and stop, presumably through the use of "finally" blocks that ensure everything gets taken care of.  </li>
  </ul>
  
  <p>Seems like it would be difficult to manage if I specify global, i will override the app framework's behavior for all threads.</p>