Android Open Source - AndroidAsyncTaskLoaderSample My Progress Count Loader






From Project

Back to project page AndroidAsyncTaskLoaderSample.

License

The source code is released under:

MIT License

If you think the Android project AndroidAsyncTaskLoaderSample listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.tatuas.android.asynctaskloadersample;
/* w ww .  j a v  a 2s.co m*/
import java.lang.ref.WeakReference;

import android.content.Context;
import android.os.SystemClock;
import android.support.v4.content.AsyncTaskLoader;

public class MyProgressCountLoader extends AsyncTaskLoader<String> {
    String arg;
    String result;
    static WeakReference<MainActivity> activity;
    static int progress = 0;

    public MyProgressCountLoader(Context context, String arg) {
        super(context);
        this.arg = arg;
        MyProgressCountLoader.activity = new WeakReference<MainActivity>((MainActivity) context);
    }

    @Override
    public String loadInBackground() {
        for (int i = 0; i < 10; i++) {
            if (!isReset() && isStarted()) {
                SystemClock.sleep(1000);
                progress = i;
                if (activity.get() != null) {
                    activity.get().runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            activity.get().bar.setProgress(progress + 1);
                        }
                    });
                }
            } else {
                break;
            }
        }
        if (!isReset() && isStarted()) {
            result = arg + "hello";
            return result;
        } else {
            return null;
        }
    }

    @Override
    public void deliverResult(String data) {
        if (isReset()) {
            return;
        }

        result = data;

        if (isStarted()) {
            super.deliverResult(data);
        }
    }

    @Override
    protected void onStartLoading() {
        if (result != null) {
            deliverResult(result);
        }

        if (takeContentChanged() || result == null) {
            forceLoad();
        }
    }
}




Java Source Code List

com.tatuas.android.asynctaskloadersample.MainActivity.java
com.tatuas.android.asynctaskloadersample.MyLoader.java
com.tatuas.android.asynctaskloadersample.MyProgressCountLoader.java