自定义Webview调用时间间隔后

时间:2018-02-02 10:51:39

标签: android webview android-webview

我已经创建了自定义webview来加载网址。它完全加载,但我想在后台每30秒后重新加载它。

每当我尝试从ASYNC TASK调用它时,它会给我错误。请帮我解决这个问题。提前致谢。获取以下错误:

  

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

4 个答案:

答案 0 :(得分:0)

除了UIThread(您的主线程)之外,您无法从任何其他线程更改UI相关元素。

您可以使用带有post.delayed()的处理程序而不是异步任务,也可以使用runonuithread()。

答案 1 :(得分:0)

您无法访问视图的成员,例如&#34; MPointAdvertise&#34;来自&#34; doInBackground&#34;方法。 尝试在&#34; onPreExecute&#34;中进行呼叫。或者&#34; onPostExecute&#34;

答案 2 :(得分:0)

试试这个:

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

答案 3 :(得分:0)

虽然您的AsyncTask代码很好,但问题是使用Timer。

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

因此,Timer创建一个与UI Thread分离的新函数来执行asynctask。你的输出:&#34;在除UI线程之外的另一个线程上调用View方法。&#34;它是不可能的.View方法只能在它自己的线程中运行。

您可以使用处理程序来执行您的任务。处理程序能够更新UI。

试试这个:`

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