A have a little confuse the difference between Widget Context and Application Context:
Regarding problem relate with unable to register new BroadcastReceiver via implement source code of Android Widget (Ref.1)
For readability reason, i copy my answer as below:
★Problem by Henry (Ref.1):
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 thisFor 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(). “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));(where “myReceiver” is an instance of
the receiver I want.) However, when I
try to run the code, I get the
following error:Unable to start
receiver…android.content.ReceiverCallNotAllowedException:
IntentReceiver components are not
allowed to register to receive intents
★Our analysis this issue and solution for this problem (Ref.1)
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 helpHere is result:
❶ISSUE:*
Regarding limited from Widget, when try to register
BroadcastReceiver via explicit source
code: (No effect when register
BroadcastReceiver via Manifest.xml)❷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)❸PREVIOUS SOLUTION:* Code Snippet: context.registerReceiver(this,
intentName); (1)❹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★Good news for anyone who need to
register BroadcastReceiver in Widget
🙂 we CAN register successful
BroadcastReceiver❺OUR SOLUTION:* But, We can fixed this by used application context
instead of Widget context(*) Code
Snippet:
context.getApplicationContext.registerReceiver(this,
intentName);❻REFERENCE:* http://developer.android.com/reference/android/content/Intent.html#AC…
Regarding❼TARGET ENVIRONEMNT:* SDK 2.3, both on Emulator and NexusOne 2.3, If
anyone success with this solution
please update our report❽NOTES* May be difference between Context object of widget and
application, but i still don’t known
exactly cause of this problem.Please let me known if your have
better solution or explain more
clearlyAlso i had solver this problem, but i
still don’t known exactly cause of
this problem.Please let me known if your have
better solution or explain more
clearly
★CONCLUTION:
● NG: When use Widget context to register BroadcastReceiver
context.registerReceiver(this, intentName);
-> it thought exception:
Unable to start receiver...android.content.ReceiverCallNotAllowedException: IntentReceiver components are not allowed to register to receive intents
● OK: When use Application context everything working fine:
ontext.getApplicationContext.registerReceiver(this, intentName);
★QUESTION:
Also our solution can solver problem: “unable to register new broadcast message via implement source code of Android widget”.
But i still concern two Qestion:
Question❶: The difference between Widget Context and Application Context and other Context object (Activity Context)’
Question❷: Because context object was use usually so when to use Application context and when to use other Context.
For ❷ i had found explain relate with memory leak (Ref. 2), but i think it may not enough (Ref.2)
So if your have answer please let me known, any answer appreciated.
Thanks
★Referecens:
(2) http://developer.android.com/resources/articles/avoiding-memory-leaks.html
This question is over a year old and is definitely long and complicated and the English language is difficult to understand. However, it still probably deserves some kind of answer.
If I understand, you are basically asking for the difference between the different Android
Contextobjects. The main difference is scope or lifetime.The application’s Context (that you can get with
Activity.getApplicationContext()orContext.getApplicationContext()has a lifetime of your entire application. The application Context is created when your application (or parts of it) are started. It lives (more or less) forever.An activity’s Context is created when that Activity is created and goes away when that activity is destroyed. If you are doing anything with the UI, you need to do that using the activity’s Context so that when the activity is destroyed, those things that are related to that activity are also cleaned up.
There are other Contexts like the one you get in a BroadcastReceiver or an AppWidgetProvider in
onReceive(). This Context has a very short lifetime (it is destroyed immediately after theonReceive()method returns). Therefore you can only do limited things with this Context.You want to register a Receiver to listen for the broadcast Intents you are interested in. You cannot do this with the Context you get in
onReceive()because that Context has a short lifetime and the listener has to be linked to something. In this case you can use the application’s Context to link the listener to. This works, but you need to realize that you’ve created some objects that will never go away because they are always active. At least you should make sure that you clean this up when the user deletes your widget from his home screen by removing the listener usingunregisterReceiver().There are a lot of questions/answers on StackOverflow regarding usage of
Context. For example, What's the difference between the various methods to get a Context?