One thing you might want to try is to grab the wakelock in the
receiver itself, not in the thread. The way it is now, it's
theoretically possible for the receiver to exit and the phone to go
back to sleep before your threads ever gets a chance to run.


On Sun, Mar 1, 2009 at 9:27 AM, Mariano Kamp <mariano.k...@gmail.com> wrote:
> And btw. I should have mentioned that the 30 minutes sleep is a placeholder
> for the real action. In my case that would be downloading stuff from the
> net. But to show that it doesn't depend on the workload and make it more
> abstract I just put a sleep in there.
>
> On Sun, Mar 1, 2009 at 6:20 PM, Mariano Kamp <mariano.k...@gmail.com> wrote:
>>
>> Marco,
>>
>>   thanks for taking the time to respond.
>>
>>   (a) If you look at the code ( http://pastie.org/403831) you'll see that
>> it is in fact a custom log file (see class Persistent Log). The reason
>> behind that is to be able to have a look at the problem over a longer period
>> of time as it doesn't occur all the time. The default log seems to be a ring
>> buffer holding just the log for a couple of minutes and is not of much help
>> in this respect.
>>
>>   (b) The receiver is triggered by an alarm (see the full code) and spawns
>> of a Thread immediately.
>>         From the pastie code mentioned above:
>>
>>
>>
>> Intent i = new Intent(context, MyReceiver.class);
>>
>> PendingIntent pi = PendingIntent.getBroadcast(context, 0, i, 0);
>>
>>
>> AlarmManager alarmManager = (AlarmManager)
>> getSystemService(ALARM_SERVICE);
>>
>> alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,
>> (System.currentTimeMillis() + 1000),
>>
>>                              60 * 60 * 1000, pi);
>>
>>
>>
>> Cheers,
>> Mariano
>>
>> On Sun, Mar 1, 2009 at 6:13 PM, Marco Nelissen <marc...@android.com>
>> wrote:
>>>
>>> It looks like the output you posted is from your custom log file, so
>>> the first thing I'd do is to look at the system log to make sure that
>>> the problem isn't in your custom logging code.
>>> Also, doing anything lengthy in a broadcast receiver is a Bad Idea,
>>> and keeping the device awake while you're doing it is a Really, Really
>>> Bad Idea. If you need to do something every 30 minutes, you should
>>> schedule an alarm, not sleep for 30 minutes with a wakelock held.
>>>
>>>
>>>
>>> On Sun, Mar 1, 2009 at 8:42 AM, Mariano Kamp <mariano.k...@gmail.com>
>>> wrote:
>>> > Hi,
>>> >
>>> >   I don't really know how to phrase my question as I don't know what
>>> > the
>>> > problem is. The symptom is that scheduled background activities are not
>>> > completed and I want to know why that is and how I can further
>>> > debug/solve
>>> > it.
>>> >
>>> >   This is happening with NewsRob and the background synchronization
>>> > that
>>> > loads articles from Google Reader at a scheduled interval. To debug it
>>> > I
>>> > meanwhile created a synthetic sample that shows the behavior too. A
>>> > Thread
>>> > is started once an hour, acquires a partial wake lock, logs something,
>>> > sleeps for 30 minutes and then releases the partial wake lock and logs
>>> > again.
>>> >
>>> >
>>> >   So it should look something like this:
>>> > 28 04:02: onReceive
>>> > 28 04:02: In aquire
>>> > 28 04:32: After release
>>> >
>>> >   28 being the day of the month followed by the time and message.
>>> >
>>> >   Unfortunately it doesn't look like that all the time ;-)  (Blank
>>> > Lines and
>>> > <comment> added for readability).
>>> >
>>> > 28 05:02: onReceive
>>> > 28 05:02: In aquire
>>> > 28 05:32: After release
>>> >
>>> > 28 06:02: onReceive
>>> > 28 06:02: In aquire
>>> > <After release missing>
>>> >
>>> > 28 07:02: onReceive
>>> > 28 07:02: In aquire
>>> > <After release missing>
>>> >
>>> > 28 08:02: onReceive
>>> > 28 08:02: In aquire
>>> > <After release missing>
>>> >
>>> > 28 09:02: onReceive
>>> > <In aquire missing>
>>> > <After release missing>
>>> > ....
>>> >
>>> >   So I suspect I do something wrong with the partial wake lock? But
>>> > what?
>>> > And even if that is the case what should happen then? Shouldn't it
>>> > continue
>>> > to run when a button on the device is pressed?
>>> >
>>> >   So here is the full sample code: http://pastie.org/403831
>>> >
>>> >   And here is the relevant method:
>>> >
>>> >     public void onReceive(final Context context, Intent intent) {
>>> >         PersistentLog.log("onReceive", context);
>>> >         new Thread(new Runnable(){
>>> >
>>> >             public void run() {
>>> >                 PowerManager pm = (PowerManager)
>>> > context.getSystemService(Context.POWER_SERVICE);
>>> >                 PowerManager.WakeLock wl =
>>> > pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "SomeTag");
>>> >                 wl.acquire();
>>> >                 PersistentLog.log("In aquire", context);
>>> >                 SystemClock.sleep(30 * 60 * 1000);
>>> >                 wl.release();
>>> >                 PersistentLog.log("After release", context);
>>> >
>>> >             }
>>> >         }).start();
>>> >     }
>>> >
>>> >   Any ideas?
>>> >
>>> > Cheers,
>>> > Mariano
>>> >
>>> > ps. Yes, I've seen the typo. Thanks for not bringing it up ;)
>>> >
>>> >
>>> > >
>>> >
>>>
>>>
>>
>
>
> >
>

--~--~---------~--~----~------------~-------~--~----~
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-unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to