0

In my application, I run a service at 12 each night using an Alarm, everything works as intended.

I have three Fragment tabs, each tab has a listview, on click of a list item a detailed view is presented.

If a user openes my app at say 11:59 (Before the service updates the underlying data from the database) he will get the data in the list view, but as the clock strikes 12, my processing starts and Database gets locked (I have implemented an exclusive lock on db while the service is running to prevent multiple threads from interfering) as the user clicks on the list row, the detailed view has blank fields and rightly so as the DB is locked and currently updating.

How do I avoid this situation? I tried to show a loader dialog by checking if my service is running from the Fragment tab I can display this dialog properly but I am finding it hard to terminate it.

private boolean isMyServiceRunning() {
        ActivityManager manager = (ActivityManager) getActivity().getSystemService(getActivity().ACTIVITY_SERVICE);
        for (RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
            if ("com.xxx.xxx.MyService".equals(service.service.getClassName())) {
                return true;
            }
        }
        return false;
    }

then:

if(isMyServiceRunning()){
Show dialog logic here
} 

But I wonder how to terminate it from a background process. ANy ideas what I can do?

The second method of doing this would be to enable Write ahead logging, would be great if someone helps me in finding some good tuts about implementing WAL in android.

User3
  • 2,465
  • 8
  • 41
  • 84
  • In your service, check if the Activity is open before you start processing data. – ThaMe90 Jan 23 '14 at 13:43
  • But that is not helpful in my case, I have to update the data at 12 because my old data is stale. So before presenting the Activity, I have to calculate the data. – User3 Jan 23 '14 at 13:45
  • 1
    How about using Timer. Run timer in a fragment and checks whether service is running or not in every 5-10 seconds and when you find that service is not running just close the dialog. – baldguy Jan 23 '14 at 13:48
  • Thinking of something on similar terms, else I have to implement WAL – User3 Jan 23 '14 at 13:52

0 Answers0