简介:
想要建立一个支持HTML5的Android App;
这个HTML5的程序需要使用本地存储,特别是sqllite;
用eclipse创建了一个app,这个app默认在res/layout建了两个描述界面的xml文件,一个调用另一个,下面的代码基于这种情况;
package com.example.helloweb;import android.app.Activity;import android.app.ActionBar;import android.app.Fragment;import android.content.Context;import android.os.Bundle;import android.view.LayoutInflater;import android.view.Menu;import android.view.MenuItem;import android.view.View;import android.view.ViewGroup;import android.os.Build;import android.webkit.WebChromeClient;import android.webkit.WebView;import android.webkit.WebSettings;import android.webkit.WebStorage.QuotaUpdater;public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // 为了支持HTML5使用数据库(sqlite)的本地存储 String databasePath = getApplicationContext().getDir("database", Context.MODE_PRIVATE).getPath(); if (savedInstanceState == null) { PlaceholderFragment fragment = new PlaceholderFragment(); fragment.setDatabasePath(databasePath); getFragmentManager().beginTransaction() .add(R.id.container, fragment) .commit(); } } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } /** * A placeholder fragment containing a simple view. * 被实例化,作为子UI的类 */ public static class PlaceholderFragment extends Fragment { String databasePath; public PlaceholderFragment() { } // 拿到MainActivity生成的数据库路径 public void setDatabasePath(String databasePath){ this.databasePath = databasePath; } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.fragment_main, container, false); // 通过IDE在子UI里拖进去一个webView,所以这里从rootView里查找 WebView webView = (WebView)rootView.findViewById(R.id.webView1); webView.setWebChromeClient(this.makeWebChromeClient()); WebSettings webSettings = webView.getSettings(); webSettings.setJavaScriptEnabled(true); webSettings.setDatabaseEnabled(true); webSettings.setDatabasePath(this.databasePath); // 可以加载本地和网络的资源,加载网络资源需要配置权限 //webView.loadUrl("http://www.csdn.net/"); webView.loadUrl("file:///android_asset/demo.html"); return rootView; } private WebChromeClient makeWebChromeClient(){ return new WebChromeClient(){ @Override public void onExceededDatabaseQuota( String url, String databaseIdentifier, long currentQuota, long estimatedSize, long totalUsedQuota, QuotaUpdater quotaUpdater) { quotaUpdater.updateQuota(5 * 1024 * 1024); } }; } }}
配置app访问网络的权限(在AndroidManifest.xml文件里配置)
如果要使用HTML5的localStorage还是要设置databasepath和打开localStorage
webSettings.setDatabasePath(this.databasePath);webSettings.setDomStorageEnabled(true);