Akash
Akash

Reputation: 971

Custom Webview call After time interval

I have created my custom webview for loading the url. it is loading perfectly but the issue that i want to reload it after every 30 second in the background.

Whenever i try to call it from ASYNC TASK it will give me error. Please help me to solve this. Thanks in advance.Getting the below error:

java.lang.IllegalStateException: Calling View methods on another thread than the UI thread.

public class MPointAdvertiseView extends WebView {
private static final int DEFAULT_ADVERTISE_WIDTH = 320;
private static final int DEFAULT_ADVERTISE_HEIGHT = 50;
private static final int DEFAULT_BANNER_TYPE = 0;

private int mAdvertiseWidth = DEFAULT_ADVERTISE_WIDTH;
private int mAdvertiseHeight = DEFAULT_ADVERTISE_HEIGHT;
private String mAdvertiseID;

private int mAdvertiseType = DEFAULT_BANNER_TYPE;


public MPointAdvertiseView(Context context) {
    super(context);
}

public MPointAdvertiseView(Context context, AttributeSet attrs) {
    this(context, attrs, 0);
}

public MPointAdvertiseView(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);

    TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.MPointAdvertiseView, defStyleAttr, 0);

    mAdvertiseWidth = a.getInteger(R.styleable.MPointAdvertiseView_banner_width, DEFAULT_ADVERTISE_WIDTH);
    mAdvertiseHeight = a.getInteger(R.styleable.MPointAdvertiseView_banner_height, DEFAULT_ADVERTISE_HEIGHT);
    mAdvertiseType = a.getInteger(R.styleable.MPointAdvertiseView_banner_type, DEFAULT_BANNER_TYPE);
    mAdvertiseID = a.getString(R.styleable.MPointAdvertiseView_advertise_id);
    a.recycle();
    initializeView(context, mAdvertiseID);
}

public MPointAdvertiseView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
    super(context, attrs, defStyleAttr, defStyleRes);
}

public void initializeView(final Context mContext, final String advertiseID) {
    getSettings().setJavaScriptEnabled(true);

    setWebViewClient(new WebViewClient());

    Timer timer = new Timer();
    timer.schedule(new TimerTask() {
        @Override
        public void run() {
            loadAdvertise advertise = new loadAdvertise(mContext);
            advertise.execute(advertiseID);
        }
    }, 30000);
}

public void load(String url) {
    loadUrl(url);
}

public class loadAdvertise extends AsyncTask<String, Void, String> {
    private Context mContext;

    public loadAdvertise(Context mContext) {
        this.mContext = mContext;
    }

    @Override
    protected String doInBackground(String... params) {
        String adsURL = MPointAdvertise.getInstance().initializeAdverise(mContext, params[0], mAdvertiseWidth, mAdvertiseHeight, mAdvertiseType);
        return adsURL;
    }

    @Override
    protected void onPostExecute(String adsURL) {
        super.onPostExecute(adsURL);
        loadUrl(adsURL);
    }
}
}

Upvotes: 1

Views: 369

Answers (4)

Ulysses Caetano Braga
Ulysses Caetano Braga

Reputation: 138

Although your AsyncTask code is fine problem is on using Timer.

"...corresponding to each Timer object is a single background thread that is used to execute all the timer's tasks, sequentially.

So, Timer creates a new separated from UI Thread for execute the asynctask. Your output: " Calling View methods on another thread than the UI thread." it's not possible.The View methods just works within it's own thread.

You can use handler to execute your task. Handler are able to update the UI.

Try this: `

    Handler handler = new Handler();
    handler.postDelayed( new Runnable() {
        @Override
        public void run() {
            //Task's code
        }
    }, timeInMills );`

Upvotes: 0

Softcloud
Softcloud

Reputation: 141

Try this:

timer.schedule(new TimerTask(){
    @Override
    public void run(){
        post(new Runnable() {
            @Override 
            public void run() {
                loadUrl(adsURL);
            } 
        });
}, 30_000L);

Upvotes: 1

You can't access a view's member like "MPointAdvertise" from the "doInBackground" method. Try to do the call in "onPreExecute" or "onPostExecute"

Upvotes: 0

Sotiris S. Magionas
Sotiris S. Magionas

Reputation: 799

You can't change UI related elements from any other thread apart from the UIThread (your main thread).

You could either use a handler with a post.delayed() instead of an async task or use runonuithread().

Upvotes: 0

Related Questions