Background service and UI updates and implementing WAL

眉间皱痕 提交于 2019-12-25 13:26:21

问题


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.

来源:https://stackoverflow.com/questions/21309808/background-service-and-ui-updates-and-implementing-wal

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!