Friday, February 4, 2011

Re: [android-developers] Re: Where to put a broadcast receiver for a widget

An AppWidgetProvider is transient. It's only instantiated as needed, and
is destroyed soon after that.

That's why you can't use an API that requires a long-lasting reference
to the app widget provider (or receiver). This includes binding to a
service (but you can start one, though) or registering a receiver (but
you can get the most recent value of a sticky broadcast, as Mark showed).

-- Kostya

04.02.2011 9:13, Jeffrey пишет:
> Okay, figured it out. I guess I can't have anything from the
> AppWidgetProvider class activate a registerReceiver so I have to make
> a service to do it. I don't keep it running, just launch it to update
> everything then it dies.
>
> Thanks for your help and patience.
>
> On Feb 3, 10:56 pm, Jeffrey<jeffisagen...@gmail.com> wrote:
>> Also the only thing that changes is the Context, is that where the
>> problem is? Because if it's not that then I have absolutely no clue as
>> to what it could be.
>>
>> On Feb 3, 10:45 pm, Jeffrey<jeffisagen...@gmail.com> wrote:
>>
>>
>>
>>
>>
>>
>>
>>> Am I doing something wrong? The first time my app runs this code it
>>> runs it from the Configuration page and it works just fine, the second
>>> time it comes up with a nullpointerexception, the code is this:
>>> IntentFilter batteryLevelFilter = new
>>> IntentFilter(Intent.ACTION_BATTERY_CHANGED);
>>> rawlevel = registerReceiver(null,
>>> batteryLevelFilter).getIntExtra("level", -1);
>>> I know that registerReceiver doesn't like to run from the
>>> AppWidgetProvider, so is it something to do with the class being
>>> called by the onUpdate()? I don't know if I need to do anything
>>> special but I can't seem to get it to run this line of code to save my
>>> life.
>>> Also as a side note I tried using this
>>> int rawlevel = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
>>> and it can't resolve BatteryManager.EXTRA_LEVEL even after importing
>>> BatteryManager and setting the minSDK to 8.
>>> On Feb 3, 8:30 pm, AndroidDevTime<androiddevd...@gmail.com> wrote:
>>>> what are the power battery performance implications of maintaining a
>>>> service? Will this drain the battery more to keep a service up and
>>>> running? Especially if you bind to the service from the widget to
>>>> call service functions?
>>>> On Feb 3, 3:39 pm, Mark Murphy<mmur...@commonsware.com> wrote:
>>>>> 2011/2/3 Jeffrey<jeffisagen...@gmail.com>:
>>>>>> Thank you, one last issue that I'm having, you said to get the battery
>>>>>> level to call registerReceiver with a null broadcast receiver, but
>>>>>> when I try the following code I get a nullpointerexception:
>>>>>> int rawlevel = registerReceiver(null,
>>>>>> batteryLevelFilter).getIntExtra("level", -1);
>>>>>> I'm not sure if thats right or not. When I was running a broadcast
>>>>>> receiver it grabbed the context and the code was as such:
>>>>>> int rawlevel = intent.getIntExtra("level", -1);
>>>>>> which worked but since I no longer use the broadcast receiver to grab
>>>>>> the intent I can't seem to figure out how to get it into a manageable
>>>>>> variable.
>>>>> There are two possibilities:
>>>>> 1. Your batteryLevelFilter is incorrect
>>>>> 2. Your code is being called before an ACTION_BATTERY_CHANGED
>>>>> broadcast has been sent
>>>>> Since #2 is conceivable, depending on when you are setting up your
>>>>> alarms, be sure to handle a null response to registerReceiver().
>>>>> Also, I would use BatteryManager.EXTRA_LEVEL instead of "level".
>>>>> That's a change I need to make in one of my own examples, one of these
>>>>> days...
>>>>> --
>>>>> Mark Murphy (a Commons Guy)http://commonsware.com|http://github.com/commonsguyhttp://commonsware.com/blog|http://twitter.com/commonsguy
>>>>> Android Training...At Your Office:http://commonsware.com/training


--
Kostya Vasilyev -- WiFi Manager + pretty widget -- http://kmansoft.wordpress.com

--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscribe@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

No comments:

Post a Comment