如何使用WebAppInterface(Javascript界面)操作Android视图?
当我的Android WebView运行时,我使用WebAppInterface从Javascript获取数据。在我从Javascript中获取一些数据之后,如何操作Android视图如何使用WebAppInterface(Javascript界面)操作Android视图?,javascript,android,Javascript,Android,当我的Android WebView运行时,我使用WebAppInterface从Javascript获取数据。在我从Javascript中获取一些数据之后,如何操作Android视图 public class WebAppInterface { Context mContext; /** Instantiate the interface and set the context */ WebAppInterface(Context c) { mCont
public class WebAppInterface {
Context mContext;
/** Instantiate the interface and set the context */
WebAppInterface(Context c) {
mContext = c;
}
//This function can only be called by javascript
@android.webkit.JavascriptInterface
public void sendDataToJava(float currTime, String url) {
videoUrl = url;
videoTime = Math.round(currTime);
//use references to views to change stuff
videoView.seekTo(videoTime); //ERROR HERE
//ERROR: Only the original thread that created a view hierarchy can touch its views
}
}
您需要保留对视图包含活动的引用,并使用runnable调用activity.runOnUiThread方法调用与视图相关的代码 它看起来像这样:
public class WebAppInterface {
Context mContext;
Activity mActivity;
/** Instantiate the interface and set the context */
WebAppInterface(Context c, Activity a) {
mContext = c;
mActivity = a;
}
//This function can only be called by javascript
@android.webkit.JavascriptInterface
public void sendDataToJava(float currTime, String url) {
videoUrl = url;
videoTime = Math.round(currTime);
mActivity.runOnUiThread(new Runnable() {
@Override
public void run() {
videoView.seekTo(videoTime);
}
});
}
}