title: Register BroadcastReceiver to Widget (Difference of Context object)
body: <p>A have a little confuse the <strong>difference</strong> between <strong>Widget Context</strong> and <strong>Application Context</strong>:
  Regarding problem relate with <strong>unable</strong> to register new BroadcastReceiver via implement source code of Android Widget (Ref.1)</p>
  
  <hr>
  
  <p>For readability reason, i copy my answer as below:
  ★<strong>Problem by Henry (Ref.1):</strong></p>
  
  <blockquote>
    <p>I am making a widget that needs a
    broadcast receiver, like the one in
    com.example.android.apis.appwidget.ExampleBroadcastReceiver.
    However, the example defines
    Intent.ACTION_TIMEZONE_CHANGED in the
    manifest, but there are some that do
    not allow this</p>
    
    <p>For example, Intent.ACTION_TIME_TICK
    says "You can not receive this through
    components declared in manifests, only
    by exlicitly registering for it with
    Context.registerReceiver(). "</p>
    
    <p>So I removed the manifest declarations
    and tried replacing the
    AppWidgetProvider.onEnabled function
    that was in the example with a call
    like the following:
    context.registerReceiver(myReceiver,
    new
    IntentFilter(Intent.ACTION_TIME_TICK));</p>
    
    <p>(where "myReceiver" is an instance of
    the receiver I want.)  However, when I
    try to run the code, I get the
    following error:</p>
    
    <p>Unable to start
    receiver...android.content.ReceiverCallNotAllowedException:
    IntentReceiver components are not
    allowed to register to receive intents</p>
  </blockquote>
  
  <p><strong>★Our analysis this issue and solution for this problem (Ref.1)</strong></p>
  
  <blockquote>
    <p>This is result after investigate this
    problem, i was handler successful this
    issue. So i collect as report to share
    with android developer. Hope it help</p>
    
    <p>Here is result:</p>
    
    <p>❶ISSUE:* 
    Regarding limited from Widget, when try to register
    BroadcastReceiver via explicit source
    code: (No effect when register
    BroadcastReceiver via Manifest.xml)</p>
    
    <p>❷EXAMPLE: * 
    BroadcastReceiver: ACTION_TIME_TICK message is one
    example: As docs from Android had
    point out: "You can not receive this
    through components declared in
    manifests, only by exlicitly
    registering for it with
    Context.registerReceiver()." (Ref.1)</p>
    
    <p>❸PREVIOUS SOLUTION:* Code Snippet: context.registerReceiver(this,
    intentName); (1)</p>
    
    <p>❹ERROR when used 3★ solution* When implement follow (1), it though
    exception:
    android.content.ReceiverCallNotAllowedException:
    IntentReceiver components are not
    allowed to register to receive intents</p>
    
    <p>★Good news for anyone who need to
    register BroadcastReceiver in Widget
    :) we CAN register successful
    BroadcastReceiver</p>
    
    <p>❺OUR SOLUTION:* But, We can fixed this by used application context
    instead of Widget context(*) Code
    Snippet:
    context.getApplicationContext.registerReceiver(this,
    intentName);</p>
    
    <p>❻REFERENCE:* <a href="http://developer.android.com/reference/android/content/Intent.html#AC" rel="noreferrer">http://developer.android.com/reference/android/content/Intent.html#AC</a>...
    Regarding</p>
    
    <p>❼TARGET ENVIRONEMNT:* SDK 2.3, both on Emulator and NexusOne 2.3, If
    anyone success with this solution
    please update our report</p>
    
    <p>❽NOTES* May be difference between Context object of widget and
    application, but i still don't known
    exactly cause of this problem.</p>
    
    <p>Please let me known if your have
    better solution or explain more
    clearly </p>
    
    <p>Also i had solver this problem, but i
    still don't known exactly cause of
    this problem.</p>
    
    <p>Please let me known if your have
    better solution or explain more
    clearly</p>
  </blockquote>
  
  <hr>
  
  <p><strong>★CONCLUTION:</strong></p>
  
  <p>● <strong>NG</strong>: When use <strong>Widget context</strong> to register BroadcastReceiver </p>
  
  <pre><code>context.registerReceiver(this, intentName); 
  </code></pre>
  
  <p>-> it thought <strong>exception:</strong></p>
  
  <pre><code>Unable to start receiver...android.content.ReceiverCallNotAllowedException: IntentReceiver components are not allowed to register to receive intents
  </code></pre>
  
  <p>● <strong>OK</strong>: When use <strong>Application context</strong> everything working fine:</p>
  
  <pre><code>ontext.getApplicationContext.registerReceiver(this, intentName); 
  </code></pre>
  
  <hr>
  
  <p><strong>★QUESTION:</strong>
  Also our solution can solver problem: "unable to register new broadcast message via implement source code of Android widget".</p>
  
  <p><strong>But i still concern two Qestion:</strong></p>
  
  <p><strong>Question</strong>❶: The difference between Widget Context and Application Context and other Context object (Activity Context)'</p>
  
  <p><strong>Question</strong>❷: Because context object was use usually so when to use Application context and when to use other Context.</p>
  
  <p>For ❷ i had found explain relate with memory leak (Ref. 2), but i think it may not enough (Ref.2)</p>
  
  <p>So if your have answer please let me known, any answer appreciated.</p>
  
  <p>Thanks</p>
  
  <hr>
  
  <p><strong>★Referecens:</strong></p>
  
  <p>(1) <a href="http://groups.google.com/group/android-developers/browse_thread/thread/790da1a655f4a227/0b8d6aad1dc2d371?hl=en&amp;lnk=gst&amp;q=Broadcast+Receiver+From+Widget#0b8d6aad1dc2d371" rel="noreferrer">http://groups.google.com/group/android-developers/browse_thread/thread/790da1a655f4a227/0b8d6aad1dc2d371?hl=en&amp;lnk=gst&amp;q=Broadcast+Receiver+From+Widget#0b8d6aad1dc2d371</a></p>
  
  <p>(2) <a href="http://developer.android.com/resources/articles/avoiding-memory-leaks.html" rel="noreferrer">http://developer.android.com/resources/articles/avoiding-memory-leaks.html</a></p>