Fragment里使用 PhoneGap 的 CordovaWebView

首先说明一下为什么要使用 CordovaWebView 而不直接使用 WebView 呢?由于 Android4.4 版本之后对 WebView 做了很大的改变,具体参考

这篇文章:理解WebKit和Chromium: Android 4.4 上的Chromium WebView,这样的改变会出现这样的问题,也是我遇到的问题:webview4.4以上版本使用loadurl加载过长js文件失效问题

网上也有相关的解决方法,但是感觉很麻烦,而且解决不了我的问题,最后想了想,我们在使用 PhoneGap 的时候有一个继承 WebView 的 CordovaWebView 组件

已经对于这样的问题做了很好的处理,所以为何不直接用咧,所以就有了这篇文章的: Fragment里使用 PhoneGap 的 CordovaWebView

实现一个类继承 PhoneGap 的接口:CordovaInterface 

<span style="font-size:14px;">import android.app.Activity;
import android.content.ContextWrapper;
import android.content.Intent;

import org.apache.cordova.CordovaInterface;
import org.apache.cordova.CordovaPlugin;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * Created by long on 2016/5/30.
 */
public class CordovaContext extends ContextWrapper implements CordovaInterface {
    Activity activity;
    protected final ExecutorService threadPool = Executors.newCachedThreadPool();

    public CordovaContext(Activity activity) {
        super(activity.getBaseContext());
        this.activity = activity;
    }

    @Override
    public void startActivityForResult(CordovaPlugin cordovaPlugin, Intent intent, int i) {
    }

    @Override
    public void setActivityResultCallback(CordovaPlugin cordovaPlugin) {
    }

    @Override
    public Activity getActivity() {
        return activity;
    }

    @Override
    public Object onMessage(String s, Object o) {
        return null;
    }

    @Override
    public ExecutorService getThreadPool() {
        return threadPool;
    }
}


</span>


<span style="font-size:14px;">import android.app.Fragment;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;

import org.apache.cordova.Config;
import org.apache.cordova.CordovaWebView;

public class HomeFragment extends Fragment {
    private View view = null;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

            LayoutInflater localInflater = inflater.cloneInContext(new CordovaContext(this.getActivity()));
            view = localInflater.inflate(R.layout.fragment_cordova, container, false);

            CordovaWebView webView = (CordovaWebView) view.findViewById(R.id.id_cordovaWebView);
            Config.init(getActivity());
            webView.loadUrl("file:///android_asset/www/index_home.html");

        }

        return view;
    }

}


</span>

相关文章分享:

http://www.lai18.com/content/1978961.html

https://github.com/Adobe-Marketing-Cloud-Apps/app-sample-android-phonegap/wiki/Embed-Webview-in-Android-Fragment

posted on   EvanLong  阅读(138)  评论(0编辑  收藏  举报

编辑推荐:
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
阅读排行:
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示