实现html5项目跳转并传数据到安卓,在activity之间进行传输

来源:互联网 发布:如何使用jade软件 编辑:程序博客网 时间:2024/06/14 00:20

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显示一下接收的数据,看数据是否正常接收

阅读全文
0 0
原创粉丝点击