cordova webview的使用

本文介绍了一个使用Cordova框架的应用程序初始化配置过程,包括Activity的创建、CordovaWebView的设置及缓存配置等关键技术点。文章详细展示了如何通过Java代码进行WebView的高级定制,如开启数据库存储API功能、设置缓存模式等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

</pre><pre name="code" class="java">
</pre><pre name="code" class="java">
//不得不说,官网的api是个大坑。
</pre><pre name="code" class="java">package com.app.phone;


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


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


import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.webkit.WebSettings;


public class index extends Activity implements CordovaInterface {


	CordovaWebView cwv;
	
	 private final ExecutorService threadPool =Executors.newCachedThreadPool();


	private CordovaPlugin activityResultCallback =null;


    protected boolean activityResultKeepRunning;  
    
    // Keep app running when pause is received. (default = true)  
    // If true, then the JavaScript and native code continue to run in the  
    // background  
    // when another application (activity) is started.  
    protected boolean keepRunning = true;
    /* Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.index);
        cwv = (CordovaWebView) findViewById(R.id.tutorialView);
        
        cwv.getSettings().setCacheMode(WebSettings.LOAD_DEFAULT);  //设置 缓存模式 
        
        //开启 database storage API 功能 
        cwv.getSettings().setDatabaseEnabled(true);  
        String cacheDirPath = getFilesDir().getAbsolutePath()+"nanguamache"; 
//      String cacheDirPath = getCacheDir().getAbsolutePath()+Constant.APP_DB_DIRNAME; 
        Log.i("xiao", "cacheDirPath="+cacheDirPath); 
        //设置数据库缓存路径 
        cwv.getSettings().setDatabasePath(cacheDirPath); 
        //设置  Application Caches 缓存目录 
        cwv.getSettings().setAppCachePath(cacheDirPath); 
        //开启 Application Caches 功能 
        cwv.getSettings().setAppCacheEnabled(true); 
        
      
        Config.init(this.getActivity());
       //url  
        cwv.loadUrl("http://write.blog.youkuaiyun.com/postedit/46045297");
    }
	public void startActivityForResult(CordovaPlugin command, Intent intent,
			int requestCode) {
		 this.activityResultCallback = command;  
	        this.activityResultKeepRunning = this.keepRunning;  
	        // If multitasking turned on, then disable it for activities that return  
	        // results  
	        if (command != null) {  
	            this.keepRunning = false;  
	        }  
	        // Start activity  
	        super.startActivityForResult(intent, requestCode);  
		
	}


	@Override
	public void setActivityResultCallback(CordovaPlugin plugin) {
		
		
		 this.activityResultCallback = plugin;  
		
	}


	@Override
	public Activity getActivity() {
		// TODO Auto-generated method stub
		return this;
	}


	@Override
	public Object onMessage(String id, Object data) {
		// TODO Auto-generated method stub
		return null;
	}


	@Override
	public ExecutorService getThreadPool() {
		// TODO Auto-generated method stub
		  return threadPool;
	}


}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值