欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

把CordovaWebView嵌入到自己的应用(Embedding WebViews)

程序员文章站 2022-05-14 22:35:47
...
以下以Android为例。

(1)下载最新版的Cordova-Android
https://github.com/apache/cordova-android/releases
2015/02/04 最新版:cordova-android-3.7.1.zip

(2)解压到本地C盘后,通过命令行进入C:\cordova-android-3.7.1\framework

(3)执行命令“D:/android-sdk/tools/android update project -p .”
会生成以下两个文件:
引用
local.properties
proguard-project.txt


(4)执行命令 “ant jar”
会build这个Cordova的library工程,生成cordova-3.7.1.jar
引用
C:\cordova-android-3.7.1\framework\cordova-3.7.1.jar


(5)新建一个Android工程“CordovaViewSample”,把cordova-3.7.1.jar拷贝到libs文件夹下。

(6)在res/layout/activity_main.xml文件中加入以下代码
<org.apache.cordova.CordovaWebView
    android:id="@+id/tutorialView"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />


(7)修改Activity,可以参考CordovaActivity.java
/framework/src/org/apache/cordova/CordovaActivity.java

public class MainActivity extends Activity implements CordovaInterface {
	
	private final ExecutorService threadPool = Executors.newCachedThreadPool();

    // The webview for our app
	protected CordovaWebView appView;
	
    // Plugin to call when activity result is received
    protected int activityResultRequestCode;
    protected CordovaPlugin activityResultCallback;
    
    protected CordovaPreferences prefs = new CordovaPreferences();
    protected Whitelist internalWhitelist = new Whitelist();
    protected Whitelist externalWhitelist = new Whitelist();
    protected ArrayList<PluginEntry> pluginEntries;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		appView = (CordovaWebView) findViewById(R.id.tutorialView);
        
        internalWhitelist.addWhiteListEntry("*", false);
        externalWhitelist.addWhiteListEntry("tel:*", false);
        externalWhitelist.addWhiteListEntry("sms:*", false);
        prefs.set("loglevel", "DEBUG");
        
        appView.init(this, makeWebViewClient(appView), makeChromeClient(appView),
        		pluginEntries, internalWhitelist, externalWhitelist, prefs);
        
        appView.loadUrlIntoView("http://m.jd.com/");
	}
	
    protected CordovaWebViewClient makeWebViewClient(CordovaWebView webView) {
        return webView.makeWebViewClient(this);
    }
    
    protected CordovaChromeClient makeChromeClient(CordovaWebView webView) {
        return webView.makeWebChromeClient(this);
    }
    
	@Override
	public Activity getActivity() {
		return this;
	}

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

	@Override
	public Object onMessage(String id, Object data) {
		if ("exit".equals(id)) {
			super.finish();
        }
		return null;
	}

	@Override
	public void setActivityResultCallback(CordovaPlugin plugin) {
        if (activityResultCallback != null) {
            activityResultCallback.onActivityResult(activityResultRequestCode, Activity.RESULT_CANCELED, null);
        }
        this.activityResultCallback = plugin;
	}

	@Override
	public void startActivityForResult(CordovaPlugin command, Intent intent, int requestCode) {
		setActivityResultCallback(command);
		try {
			startActivityForResult(intent, requestCode);
		} catch (RuntimeException e) {
			activityResultCallback = null;
			throw e;
		}
	}
	
}


(8)修改AndroidManifest.xml
<uses-permission android:name="android.permission.INTERNET" />


(9)编译安装到手机,如下:
把CordovaWebView嵌入到自己的应用(Embedding WebViews)
            
    
    博客分类: Cordova  

如果加载assets下的HTML的话,还需要:
  • 1)把HTML/Images/JS等文件拷贝到/assets/www下
  • 2)把/framework/res/xml/config.xml拷贝到/res/xml下

http://cordova.apache.org/docs/en/4.0.0/guide_platforms_android_webview.md.html#Android%20WebViews
  • 把CordovaWebView嵌入到自己的应用(Embedding WebViews)
            
    
    博客分类: Cordova  
  • 大小: 211.4 KB