android实现用户提交之前本地保存、读入html文件的用户输入

package com.mobileoa.msgdetail;


import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import com.mobileoa.R;
import com.mobileoa.common.DBManager;
import com.mobileoa.common.MessageInfo;
import com.mobileoa.usermanager.LoginManager;
import com.mobileoa.utils.Constants;
import com.mobileoa.utils.Constants.MobileOaString;
import com.mobileoa.utils.FileManager;


import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.webkit.JsResult;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.Toast;


public class MsgTableActivity extends Activity {
public static final String TAG = MobileOaString.LOG_TAG;
private MessageInfo currentMsg = null;
private WebView webView = null;
private DBManager dao = new DBManager();
private static final String USER_INPUT = "userInput";
private String uid;
private String msgid;
private ProgressDialog progress = null;
/** 标记url是本地url还是网络url,0表示网络url */
private static int URL_TYPE = 0;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.message_table);
Log.i(TAG, "MsgTableActivity on create!");


this.currentMsg = (MessageInfo) getIntent().getExtras()
.getSerializable(MsgDetailActivityGroup.CURRENT_MSG);
this.currentMsg.setTableLocation(this.dao.getMsg(
this.currentMsg.getMsgId()).getTableLocation());


Log.i(TAG,
"The tableLocation is :" + this.currentMsg.getTableLocation());


this.uid = String.valueOf(LoginManager.getInstance().getCurrentUser()
.getUserId());
this.msgid = String.valueOf(this.currentMsg.getMsgId());
// 页面加载前显示等待条
this.progress = new ProgressDialog(this);
this.progress.setTitle("页面加载中,请稍候。。。");
this.progress.show();


this.initWebView();


}


/** 设置webview */
private void initWebView() {
this.webView = (WebView) findViewById(R.id.msg_table);
webView.getSettings().setJavaScriptEnabled(true);
webView.requestFocusFromTouch();
webView.setWebChromeClient(new MyWebChromeClient());
webView.addJavascriptInterface(this, "save_html");
webView.setWebViewClient(new MyWebViewClient());


// 如果本地不存在已保存的html文件则加载网络html文件,如果存在则加载本地html,并从sharepreference中读入用户之前保存的数据
if (this.currentMsg.getTableLocation() == null) {
Log.i(TAG, "The tableLocation is null!");
webView.loadUrl(this.currentMsg.getTableUrl());
} else {
URL_TYPE = 1;// 标记url类型为本地url
webView.loadUrl("file://" + this.currentMsg.getTableLocation());


}


Button btnSave = (Button) findViewById(R.id.btn_save);
btnSave.setOnClickListener(new Button.OnClickListener() {


public void onClick(View v) {
MsgTableActivity.this.saveHtmlInJs();


}


});


Button btnRead = (Button) findViewById(R.id.btn_read);
btnRead.setOnClickListener(new Button.OnClickListener() {


public void onClick(View v) {
// 点击读入的时候,先读入本地已保存的html文件,再读入已存储的用户输入数据
MessageInfo info = new MessageInfo();
info = MsgTableActivity.this.dao
.getMsg(MsgTableActivity.this.currentMsg.getMsgId());
String tableLoc = info.getTableLocation();
// 读入html文件
URL_TYPE = 1;// 标记url类型为本地url
MsgTableActivity.this.webView.loadUrl("file://" + tableLoc);


}


});
}


/** 执行js保存数据功能的方法 */
public void saveHtmlInJs() {
// 一段JavaScript语句,效果是保存html文件,包括用户输入未提交的内容到本地


String saveStr = "javascript:"
+ "var inputElements=document.getElementsByTagName('*'); "
+ "for(var i=0; i<inputElements.length; i++){"
+ " if(inputElements[i].className=='mobile'){"
+ " window.save_html.saveInput(inputElements[i].id, inputElements[i].value);"
+ "}};"
+ "window.save_html.saveHtml('<head>'+document.getElementsByTagName('html')[0].innerHTML+'</head>');"
+ "var message='Table save success!';" + "alert(message);";


this.webView.loadUrl(saveStr);


}


/** 执行js设置数据功能的方法 */
public void setHtmlInJs() {
String readStr = "javascript:"
+ "var inputElements=document.getElementsByTagName('*');"
+ "for(var i=0; i<inputElements.length; i++){"
+ "if(inputElements[i].className=='mobile'){"
+ "inputElements[i].value=window.save_html.readInput(inputElements[i].id);"
+ "}}";


this.webView.loadUrl(readStr);
}


/** 读取html文件保存到本地 */
public void saveHtml(String html) {
String fileName = this.uid + "_" + this.msgid;
try {
File file = FileManager.createSDFile(
Constants.MobileOaString.DIR_TABLE, fileName);
FileOutputStream out = new FileOutputStream(file);
out.write(html.getBytes());
Log.i(Constants.MobileOaString.LOG_TAG,
"Create a html file in sdcard.the filename is:"
+ file.getPath());
// 将该条公文的html文件本地路径存入数据库
this.currentMsg.setTableLocation(file.getPath());
this.dao.updateMsg(this.currentMsg);
} catch (IOException e) {
e.printStackTrace();
}
}


/** 将用户在html里面输入的内容以sharepreference的形式保存在本地 */
public void saveInput(String key, String data) {
String preStr = this.uid + "_" + this.msgid + "_";
key = preStr + key;
Editor input = this.getSharedPreferences(USER_INPUT, 0).edit();
input.putString(key, data);
input.commit();
Log.i(Constants.MobileOaString.LOG_TAG, "Saving input,the key is:"
+ key + ";the data is:" + data);
}


/** 将保存的用户输入重新写入js文件 */
public String readInput(String key) {
String preStr = this.uid + "_" + this.msgid + "_";
key = preStr + key;
SharedPreferences output = this.getSharedPreferences(USER_INPUT, 0);
String data = output.getString(key, null);
Log.i(Constants.MobileOaString.LOG_TAG, "Reading input,the key is:"
+ key + ";the data is:" + data);


return data;
}


/** 设置JavaScript交互的类 */
final class MyWebChromeClient extends WebChromeClient {
@Override
public boolean onJsAlert(WebView view, String url, String message,
JsResult result) {
if (message.length() != 0) {
Toast.makeText(MsgTableActivity.this, message,
Toast.LENGTH_SHORT).show();
result.cancel();
return true;
}
return false;
}


@Override
public boolean onJsTimeout() {
AlertDialog.Builder builder = new AlertDialog.Builder(
MsgTableActivity.this);
builder.setTitle("From JavaScript")
.setMessage("JavaScript time out!").show();
return super.onJsTimeout();
}


}


/** 设置读取html文件的类 */
final class MyWebViewClient extends WebViewClient {
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}


public void onPageStarted(WebView view, String url, Bitmap favicon) {


super.onPageStarted(view, url, favicon);
}


public void onPageFinished(WebView view, String url) {
// 页面加载完毕,隐藏等待条
MsgTableActivity.this.progress.dismiss();
// 网页加载完毕后,判断加载的是网络url还是本地url,如果是本地,则读取用户保存的数据填入网页
if (URL_TYPE == 1) {
// 读入用户输入数据
MsgTableActivity.this.setHtmlInJs();
Toast.makeText(MsgTableActivity.this, "数据读入完毕。",
Toast.LENGTH_SHORT).show();
}
URL_TYPE = 0;
Log.i(Constants.MobileOaString.LOG_TAG, "webview onPageFinished! ");
super.onPageFinished(view, url);
}


}


@Override
protected void onPause() {
this.dao.closeDb();
super.onPause();
}


}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值