在阅读了不同的例子之后,我几乎都回到了想要的结果
代码:
WebView myWebView = (WebView)findViewById(R.id.webview);
String strHTMLTags = "<iframe width=\"100%\" height=\"100%\" src=\"http://www.youtube.com/embed/MwdUyxyRwc8?autoplay=1\" frameborder=\"0\" allowfullscreen=\"1\" mozallowfullscreen=\"1\" webkitallowfullscreen=\"1\"></iframe>";
myWebView.setWebViewClient(new WebViewClient());
myWebView.setWebChromeClient(new WebChromeClient() {});
myWebView.getSettings().setJavaScriptEnabled(true);
WebSettings webSettings = myWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
myWebView.loadData(strHTMLTags, "text/html", "utf-8");
这将使代码在自动启动时运行良好。
如果我在“loadData”之前添加此代码:
webSettings.setUserAgentString("Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/534.36 (KHTML, like Gecko) Chrome/13.0.766.0 Safari/534.36");
webSettings.setPluginState(WebSettings.PluginState.ON);
webSettings.setAllowFileAccess(true);
此代码适用于自动启动但具有分辨率的内容不正确且视频看起来不应该如此。
究竟是什么setUserAgentString做了什么以及如何让他使用自动启动。
我尝试另一种方式:
public void onPageFinished(WebView view, String url) {
int cursorXPos = (int) (view.getX() + view.getWidth()/2);
int cursorYPos = (int) (view.getX() + view.getHeight()/2);
int cursorXPos = CENTER_OF_SCREEN_X_POS;
int cursorYPos = CENTER_OF_SCREEN_Y_POS;
mInstrumentation.sendPointerSync(MotionEvent.obtain(SystemClock.uptimeMillis(),
SystemClock.uptimeMillis(),MotionEvent.ACTION_DOWN, cursorXPos,
cursorYPos, 0));
mInstrumentation.sendPointerSync(MotionEvent.obtain(SystemClock.uptimeMillis(),
SystemClock.uptimeMillis(),MotionEvent.ACTION_UP, cursorXPos,
cursorYPos, 0));
}
});
并收到错误:
05-05 15:33:52.135: E/AndroidRuntime(1334): FATAL EXCEPTION: main
05-05 15:33:52.135: E/AndroidRuntime(1334): java.lang.RuntimeException: This method can not be called from the main application thread
05-05 15:33:52.135: E/AndroidRuntime(1334): at android.app.Instrumentation.validateNotAppThread(Instrumentation.java:1555)
05-05 15:33:52.135: E/AndroidRuntime(1334): at android.app.Instrumentation.sendPointerSync(Instrumentation.java:904)
05-05 15:33:52.135: E/AndroidRuntime(1334): at com.example.embeded.MainActivity$3.onPageFinished(MainActivity.java:69)
05-05 15:33:52.135: E/AndroidRuntime(1334): at android.webkit.CallbackProxy.handleMessage(CallbackProxy.java:317)
05-05 15:33:52.135: E/AndroidRuntime(1334): at android.os.Handler.dispatchMessage(Handler.java:99)
05-05 15:33:52.135: E/AndroidRuntime(1334): at android.os.Looper.loop(Looper.java:137)
05-05 15:33:52.135: E/AndroidRuntime(1334): at android.app.ActivityThread.main(ActivityThread.java:4517)
05-05 15:33:52.135: E/AndroidRuntime(1334): at java.lang.reflect.Method.invokeNative(Native Method)
05-05 15:33:52.135: E/AndroidRuntime(1334): at java.lang.reflect.Method.invoke(Method.java:511)
05-05 15:33:52.135: E/AndroidRuntime(1334): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:993)
05-05 15:33:52.135: E/AndroidRuntime(1334): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:760)
05-05 15:33:52.135: E/AndroidRuntime(1334): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:0)
import android.app.Instrumentation;
...
private Instrumentation mInstrumentation;
....
public void onCreate(...) {
...
mInstrumentation = new Instrumentation();
....
}
// When the page is ready.
final int cursorXPos = CENTER_OF_SCREEN_X_POS;
final int cursorYPos = CENTER_OF_SCREEN_Y_POS;
new Thread() {
public void run() {
mInstrumentation.sendPointerSync(MotionEvent.obtain(SystemClock.uptimeMillis(),
SystemClock.uptimeMillis(),MotionEvent.ACTION_DOWN, cursorXPos,
cursorYPos, 0));
mInstrumentation.sendPointerSync(MotionEvent.obtain(SystemClock.uptimeMillis(),
SystemClock.uptimeMillis(),MotionEvent.ACTION_UP, cursorXPos,
cursorYPos, 0));
}
}.start();