any way other than AsyncTask?

When i start an activity, i will perform this action

webservice.updateallCatNews();

This function is retrieve data from online database to local database. The finish time is not constant and depend on internet speed.

I want to know when the action is finished perform. sometime 15, but sometimes 10 or 20. I need to start activity after finished.

Any function can detect the action is finished except asynctask?

private class UpdatingNews extends AsyncTask<String, Void, String> {

    @Override
    protected String doInBackground(String... params) {
        webservice.updateallCatNews();
        return null;
    }

    @Override
    protected void onPostExecute(String result) {

    }
}

Activity class

private UpdatingNews update;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main_formnewuser);
    webservice = new Database_WebService(this);

    update = new UpdatingNews();
    if (isOnline() == true) {
        update.execute("...");

    }
}

UpdateallCatNews class

public void updateallCatNews() {
    try {
        List<List_CategoryNews> newsCat = dbhelper.getAllNewsCategories();
        for (List_CategoryNews nwCat : newsCat) {

            int CatNewsID = nwCat.getCatID();

            if (CatNewsID != 0) {
                List_CategoryNews deleteNewsCat = new List_CategoryNews(
                        CatNewsID);
                dbhelper.DeleteNewsCat(deleteNewsCat);
            }
        }
        GetNewsCategory();
    } catch (Exception ex) {
        AlertDialog.Builder b = new AlertDialog.Builder(mContext);
        b.setMessage(ex.toString());
        b.show();
    }
}

Inside this class, there is no others error because other activity can perform.

This if() seem like not running

if(update.getStatus().equals(AsyncTask.Status.RUNNING));

Why the function

webservice.updateallCatNews();

not perform?

Answers


You can use thread for checking this.Here using a handler you can send messages.By checking the messages You can do the further process.The only possible way to connect thread with the UI is using a handler.I think this piece of code may help you.

Thread animator = new Thread() {
        public void run() {
            int i = 0;
            try {
                sleep(2000);
                while (i < 4) {
                    sleep(50);
                    handler.sendMessage(handler.obtainMessage(i));
                    i++;
                }
            } catch (Exception e) {

            }
        }
    };
    animator.start();
    handler = new Handler() {

        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (msg.what == 0) {
                animatedimage.setImageResource(R.drawable.sub1);
            } else if (msg.what == 1) {
                animatedimage.setImageResource(R.drawable.sub2);
            } else if (msg.what == 2) {
                animatedimage.setImageResource(R.drawable.sub3);
            } else if (msg.what == 3) {
                animatedimage.setImageResource(R.drawable.sub4);
            }
        }

    };

Need Your Help

How to construct a WebSocket URI relative to the page URI?

websocket uri relative-path

I want to construct a WebSocket URI relative to the page URI at the browser side. Say, in my case convert HTTP URIs like

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.