简体   繁体   English

时间间隔后自定义Webview调用

[英]Custom Webview call After time interval

I have created my custom webview for loading the url. 我已经创建了用于加载网址的自定义Web视图。 it is loading perfectly but the issue that i want to reload it after every 30 second in the background. 它加载完美,但我想在后台每隔30秒重新加载一次的问题。

Whenever i try to call it from ASYNC TASK it will give me error. 每当我尝试从ASYNC TASK调用它时,都会给我错误。 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. java.lang.IllegalStateException:在UI线程之外的另一个线程上调用View方法。

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);
    }
}
}

You can't change UI related elements from any other thread apart from the UIThread (your main thread). 除了UIThread(您的主线程)之外,您不能从任何其他线程更改与UI相关的元素。

You could either use a handler with a post.delayed() instead of an async task or use runonuithread(). 您可以使用带有post.delayed()的处理程序来代替异步任务,也可以使用runonuithread()。

You can't access a view's member like "MPointAdvertise" from the "doInBackground" method. 您无法通过“ doInBackground”方法访问视图的成员,例如“ MPointAdvertise”。 Try to do the call in "onPreExecute" or "onPostExecute" 尝试在“ onPreExecute”或“ onPostExecute”中进行调用

Try this: 尝试这个:

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

Although your AsyncTask code is fine problem is on using Timer. 尽管您的AsyncTask代码很好,但是使用Timer还是有问题。

"...corresponding to each Timer object is a single background thread that is used to execute all the timer's tasks, sequentially. “ ...与每个Timer对象相对应的是一个后台线程,该线程用于依次执行所有计时器的任务。

So, Timer creates a new separated from UI Thread for execute the asynctask. 因此,Timer创建了一个与UI线程分离的新对象来执行asynctask。 Your output: " Calling View methods on another thread than the UI thread." 您的输出:“在UI线程之外的另一个线程上调用View方法。” it's not possible.The View methods just works within it's own thread. View方法只是在它自己的线程中工作。

You can use handler to execute your task. 您可以使用处理程序执行任务。 Handler are able to update the UI. 处理程序能够更新UI。

Try this: ` 试试这个:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM