實現html5專案跳轉並傳資料到安卓,在activity之間進行傳輸
1、html5中的jsp內容設定為:
<input id="print" type="button" class="button basebutton bigbutton" value="列印" >
<input id="ycTest" onclick="ycTest()" type="button" class="button basebutton bigbutton" value="洋蔥測試列印" >
2、對應的js內容:
function ycTest(){
$("#print").hide();
$("#ycTest").hide();
html2canvas($("#printCanvas"), {
allowTaint: true,
letterRendering: true,
taintTest: false,
onrendered: function(canvas) {
canvas.id = "printCanvas";
//document.body.appendChild(canvas);
//生成base64圖片資料,以上內容可以忽略,我要傳輸的資料,把圖片轉換為base64格式的資料進行傳輸
var dataUrl = canvas.toDataURL();
/* var newImg = document.createElement("img");
newImg.src = dataUrl;
document.body.appendChild(newImg); */
//http://000.000.00.00:8080/ydbjq/op=print對應html5專案的網址
var url ="http://000.000.00.00:8080/ydbjq/op=print";
var param = {"dataUrl":dataUrl};
$.post(url,param);
//在js當中呼叫安卓的方法
window.AndroidWebView.showImages(dataUrl);
}
});
$("#print").show();
$("#ycTest").show();
}
3、對應安卓的部分原始碼:
public class MainActivity extends Activity implements OnClickListener {
public WebView mWebView;
private JSONObject newJson = new JSONObject();
public Object dataUrl;
private String imgData;
private final static int REQUEST_CODE=1;
@Override
protected void onCreate(Bundle savedInstanceState) {
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mWebView = (WebView) findViewById(R.id.main_web_view);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.requestFocus();
mWebView.setScrollBarStyle(WebView.SCROLLBARS_INSIDE_OVERLAY);
//
mWebView.setWebViewClient(new WebViewClient() {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
// TODO Auto-generated method stub
// 這裡是僅僅實現點選html5內部按鈕,跳轉到安卓activity裡面
view.getSettings().setJavaScriptEnabled(true);
view.addJavascriptInterface(this, "nativeMethod");
//判斷點選的超連結是否為列印按鈕
if(url.endsWith("print")){
startActivity(new Intent(PiccActivity.this,PrinterMainActivity.class));
return true;
}
else{
view.loadUrl(url);
return true;
}
}
//錯誤介面提示
@Override
public void onReceivedError(WebView view, int errorCode,
String description, String failingUrl) {
// super.onReceivedError(view, errorCode, description, failingUrl);
mWebView.loadUrl("file:///android_asset/neterror.html");
}
});
mWebView.setWebChromeClient(new MyWebChromeClient());
mWebView.addJavascriptInterface(new DemoJavaScriptInterface(), "device");
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.loadUrl("http://000.000.00.00:8080/ydbjq");
//對應的網站網址
//對應執行activity裡面的js方法
mWebView.addJavascriptInterface(new MyJavaScriptInterface(), "AndroidWebView");
}
//對應的js方法
class MyJavaScriptInterface{
MyJavaScriptInterface(){
}
/* js呼叫java,顯示圖片
*/
@JavascriptInterface
public void showImages(String data) {
//實現對資料的格式轉換,把base64位資料,前面冗餘的資料去掉
data = data.split(",")[1];
MainActivity.this.imgData = data;
//以下實現資料傳輸到另一個activity裡面
Intent intent =new Intent(MainActivity.this,PrinterMainActivity.class);
//用Bundle攜帶資料
Bundle bundle=new Bundle();
//傳遞name引數為tinyphp
//給intent新增額外資料,key為“前面”,key值為"後面"
bundle.putString("imgData", imgData);
intent.putExtras(bundle);
startActivity(intent);
}
}
4、另一個activity進行資料接收:
Intent intent=getIntent();//getIntent將該專案中包含的原始intent檢索出來,將檢索出來的intent賦值給一個Intent型別的變數intent
//獲取傳來的引數
//新頁面接收資料
Bundle bundle = this.getIntent().getExtras();
//接收data值
String imgData = bundle.getString("imgData");
Log.i("獲取到的imgData值為",imgData);
secondTxt = (TextView)findViewById(R.id.textid);
secondTxt.setText(imgData);
textview顯示一下接收的資料,看資料是否正常接收