0

I have an alarmmanager that starts a broadcast receiver every minute which runs a thread with max priority. In that thread I get the 60 most recent tweets with Twitter4j.getUserTimeline(page) from a twitter account I use for our fire department (i.e. call information). My issue is I have noticed a substantial lag from time to time at getUserTimeline. More often than not getUserTimeline takes 4 or less seconds, but other times it takes minutes even in a area with a great signal and when on wifi. I use the same code calling twitter4j.getUserTimeline in the main app/UI with only a 3 second delay max.

Anyone have any idea why this would happen or how it can be fixed?

Fenrir
  • 309
  • 1
  • 3
  • 10

1 Answers1

0

If you're launching a process that takes approximately 4 seconds, every second, then presumably you will have 4 or more threads running at any point in time - all doing the same thing. Is there some kind of interference occurring between these processes - ie: thread deadlock? If the Twitter4j.getUserTimeline function is not guaranteed thread safe, you might be better off launching a background service (set it to run in the foreground so that Android doesn't kill it on you) and then have it run in a loop continuously calling the getUserTimeline function. It can then trigger an activity to be launched when a significant event is detected.

Peter
  • 669
  • 5
  • 14
  • Sadly, it is only running every minute, more often than not the entire processes, including getUserTimeline, only takes 4 seconds. I moved it to a background thread because when the service took a long time it would cause an ANR issue. The thread simply collects the twitter information for notification and widget use which are not updated by the thread. – Fenrir Jul 31 '12 at 00:55
  • Ah, sorry - you did say every minute too. This might be getting a little off your original topic, but you shouldn't have been getting an ANR in your service unless you were attempting to do all of your work in the OnCreate function for example. You would use this function to kick of your thread which can then take as long as it wants as long as it's in foreground mode. – Peter Jul 31 '12 at 01:05
  • Ok, that is worth a try. Could you explain how I can set the thread to foreground mode? I will look online as well. – Fenrir Jul 31 '12 at 01:09
  • Sure, the following 4 lines of code set the service as a foreground service with an icon that will link back to your activity: Notification notification = new Notification(R.drawable.notification, "AppName", System.currentTimeMillis()); Intent notificationEvent = new Intent(this, AppName.class); notification.setLatestEventInfo(this, "AppName", null, PendingIntent.getActivity(this, 0, notificationEvent, 0)); startForeground(1, notification); – Peter Jul 31 '12 at 01:25