android之webview
来源:互联网 发布:美工助理 编辑:程序博客网 时间:2024/06/01 17:48
最近做一个项目需要在线播放优酷,土豆 等视频,需要用到WebView.
期间遇到很多大大小小 问题:
1 无法全屏,一点击全屏就卡死白屏;
2 退出后 声音还没停止,以及程序最小化问题;
目前这两个问题算是解决了。
对于问题2 网上基本就这一个方法:
试了之后确实 问题解决了。
对于问题1,网上找了很久, 终于看到可以的;
附上源代码:
NetVideoActivity.java
package canton.tower.travel.VoiceAndVideo; import java.lang.reflect.InvocationTargetException;import java.lang.reflect.Method;import canton.tower.travel.R;import android.os.Bundle;import android.app.Activity;import android.content.Intent;import android.util.Log;import android.view.View;import android.webkit.WebChromeClient;import android.webkit.WebSettings.PluginState;import android.webkit.WebSettings;import android.webkit.WebView;import android.webkit.WebViewClient;import android.widget.FrameLayout;import android.widget.Toast;public class NetVideoActivity extends Activity {private FrameLayout mFullscreenContainer;private FrameLayout mContentView;private View mCustomView = null;private WebView mWebView;@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.netvideolay);Intent intent = getIntent();Bundle bundle = intent.getExtras();String str_add = "";if(bundle==null) str_add = "http://www.tudou.com/v/TM6mAN5rZNk/&resourceId=0_04_05_99/v.swf"; //else str_add = bundle.getString("address");initViews();initWebView();Toast.makeText(this, "加载中,若无响应,请返回重试...", Toast.LENGTH_LONG).show();mWebView.loadUrl(str_add);}private void initViews() {mFullscreenContainer = (FrameLayout) findViewById(R.id.fullscreen_view);mContentView = (FrameLayout) findViewById(R.id.main_view);mWebView = (WebView) findViewById(R.id.webview);}private void initWebView() {WebSettings settings = mWebView.getSettings();settings.setJavaScriptEnabled(true);settings.setJavaScriptCanOpenWindowsAutomatically(true);settings.setPluginState(PluginState.ON);settings.setUseWideViewPort(false);settings.setAllowFileAccess(true);settings.setLoadWithOverviewMode(true);mWebView.setFitsSystemWindows(true);mWebView.setWebChromeClient(new MyWebChromeClient());mWebView.setWebViewClient(new MyWebViewClient());}class MyWebChromeClient extends WebChromeClient {private CustomViewCallback mCustomViewCallback;private int mOriginalOrientation = 1;@Overridepublic void onShowCustomView(View view, CustomViewCallback callback) {// TODO Auto-generated method stubonShowCustomView(view, mOriginalOrientation, callback);super.onShowCustomView(view, callback);}public void onShowCustomView(View view, int requestedOrientation,WebChromeClient.CustomViewCallback callback) {if (mCustomView != null) {callback.onCustomViewHidden();return;}if (getPhoneAndroidSDK() >= 14) {mFullscreenContainer.addView(view);mCustomView = view;mCustomViewCallback = callback;mOriginalOrientation = getRequestedOrientation();mContentView.setVisibility(View.INVISIBLE);mFullscreenContainer.setVisibility(View.VISIBLE);mFullscreenContainer.bringToFront();setRequestedOrientation(mOriginalOrientation);}}public void onHideCustomView() {mContentView.setVisibility(View.VISIBLE);if (mCustomView == null) {return;}mCustomView.setVisibility(View.GONE);mFullscreenContainer.removeView(mCustomView);mCustomView = null;mFullscreenContainer.setVisibility(View.GONE);try {mCustomViewCallback.onCustomViewHidden();} catch (Exception e) {}// Show the content view.setRequestedOrientation(mOriginalOrientation);}}class MyWebViewClient extends WebViewClient {@Overridepublic boolean shouldOverrideUrlLoading(WebView view, String url) {// TODO Auto-generated method stubview.loadUrl(url);return super.shouldOverrideUrlLoading(view, url);}}public static int getPhoneAndroidSDK() {// TODO Auto-generated method stubint version = 0;try {version = Integer.valueOf(android.os.Build.VERSION.SDK);} catch (NumberFormatException e) {e.printStackTrace();}return version;}private void callHiddenWebViewMethod(String name){ if( mWebView != null ){ try { Method method = WebView.class.getMethod(name); method.invoke(mWebView); } catch (NoSuchMethodException e) { // Log.e("No such method: " + name, e); } catch (IllegalAccessException e) { // Log.e("Illegal Access: " + name, e); } catch (InvocationTargetException e) { //Log.error("Invocation Target Exception: " + name, e); } }}@Overrideprotected void onResume() {// TODO Auto-generated method stubsuper.onResume();}@Overrideprotected void onPause() {super.onPause();//mWebView.pauseTimers();Log.e("oo", "oo");if (isFinishing()){mWebView.loadUrl("about:blank");//setContentView(new FrameLayout(this));} this.callHiddenWebViewMethod("onPause");}@Overridepublic void onBackPressed() {mWebView.loadUrl("about:blank");mWebView.clearCache(true);mWebView.clearHistory(); System.gc(); finish(); }}
netvideolayout.xml
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent"> <FrameLayout android:id="@+id/fullscreen_view" android:layout_width="fill_parent" android:layout_height="fill_parent" android:visibility="gone" /> <FrameLayout android:id="@+id/main_view" android:layout_width="fill_parent" android:layout_height="fill_parent"> <WebView android:id="@+id/webview" android:layout_width="fill_parent" android:layout_height="fill_parent" android:scrollbars="none" /> </FrameLayout > </FrameLayout>
最后在manifest.xml application 标签里 添加 android:hardwareAccelerated="true"
仍存在一些问题:
1 全屏之后只能根据播放器的图标提示退出全屏, 不能按返回键退出;
2 还有一个比较蛋疼的问题, 在网络不稳定的情况下, 播放经常卡住, 一旦卡住,退出Activity 后再次进去 仍然白屏,要等待一段时间 才反应过来;
若哪位童鞋解决了问题,还麻烦告知,谢谢!
- Android 之 WebView 组件
- Android之Hello WebView
- android之【 WebView组件 】
- android之webview使用
- android之webview
- android 之webview
- android之webview使用
- android之webview
- android之WebView详解
- android 之WebView
- android之WebView
- android之WebView应用
- Android控件之WebView
- Android之WebView详解
- Android基础之WebView
- Android之WebView
- Android之webView入门
- Android控件之WebView
- 自从有了OJ水题嗅探神器,麻麻再也不担心我刷不到题了。。
- paip.php-gtk 桌面程序 helloworld总结
- 九、cocos2dx之Actions
- arduino通过可变电阻演示模拟信号
- chm文件无法显示网页问题
- android之webview
- cronExpression配置说明
- Android基础-Service(服务)
- php实现seo伪原创,同义词替换
- leetcode_question_26 Remove Duplicates from Sorted Array
- 细谈C语言中的strcpy,strncpy,memcpy,memmove,memset函数
- thinkphp中读取文件需要使用绝对路径
- Ubuntu12.04下eclipse提示框黑色背景色的修改方法
- linux常用命令