android 嵌入服务端页面二 之WebView与页面互调
WebView引入页面及回调处理
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="@color/white" android:orientation="vertical" > <!-- 头部和尾部为重用和统一风格 --> <!-- 头部 --> <include layout="@layout/jx_two_page_header"/> <WebView android:id="@+id/wvTicketOne" android:layout_width="fill_parent" android:layout_height="fill_parent" /> <!-- 尾部 --> <include android:id="@+id/footer" layout="@layout/jx_two_footer"/> </LinearLayout>
在Activity的onCreate方法中加载页面
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.ticket_one); /** * 在android 2.3以后进行了较为严格的限制 * 该类可以用来帮助开发者改进他们编写的应用。 * 并且提供了各种的策略, * 这些策略能随时检查和报告开发者开发应用中存在的问题 */ StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder() .detectDiskReads().detectDiskWrites().detectNetwork() .penaltyLog().build()); StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder() .detectLeakedSqlLiteObjects().penaltyLog().penaltyDeath() .build()); Intent fromInent = getIntent(); ticketId = fromInent.getStringExtra("ticketId"); //是否管理员 String isManage = mPreferences.getString(Constants.USER_IS_MANAGE, String.valueOf(false)); //找到WebView控件 wvTicketOne = (WebView) findViewById(R.id.wvTicketOne); //设置为可访问页面JS,在Activity和页面直接互调时使用 wvTicketOne.getSettings().setJavaScriptEnabled(true); wvTicketOne.setWebViewClient(new WebViewClient()); //在onCreate 方法中调用WebView的loadUrl方法加载页面内容 wvTicketOne.loadUrl(Constants.URL_TICKET_ONE + ticketId+"&isManage="+isManage); //添加页面可访问的JS接口 wvTicketOne.addJavascriptInterface(new JavaScriptInterface(), "android"); // Activity间跳转:转到踏勘单 TextView takandan = (TextView) findViewById(R.id.tanKanDan); takandan.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(TicketOneActivity.this,TicketTanKanDan.class); intent.putExtra("ticketId", ticketId); startActivity(intent); } }); //返回按钮:这里做了最简单的处理,直接把当前Activity给关掉, //程序自动返回到上一个Activity(前提是上一个Activity没有销毁) ImageButton btnBack = (ImageButton) findViewById(R.id.cursor_back); btnBack.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { finish(); } }); //隐藏(后退、1/1、前进、刷新)按钮 ImageButton cursor_left = (ImageButton) findViewById(R.id.cursor_left); cursor_left.setVisibility(View.INVISIBLE); ImageButton cursor_right = (ImageButton) findViewById(R.id.cursor_right); cursor_right.setVisibility(View.INVISIBLE); ImageButton cursor_refresh = (ImageButton) findViewById(R.id.cursor_refresh); cursor_refresh.setVisibility(View.INVISIBLE); TextView tv_infoTotal=(TextView)findViewById(R.id.infoTotal); tv_infoTotal.setVisibility(View.INVISIBLE); TextView tv_infoView=(TextView)findViewById(R.id.infoView); tv_infoView.setVisibility(View.INVISIBLE); TextView tv_infoNum=(TextView)findViewById(R.id.infoNum); tv_infoNum.setVisibility(View.INVISIBLE); }
在Activity中创建一个页面可访问的JavaScript接口,供页面调用
private class JavaScriptInterface { private MediaPlayer mediaPlayer; public JavaScriptInterface(){ mediaPlayer = new MediaPlayer(); } /** * 播放 * @param url 播放文件路径 */ public void playMusic(String url){ if(mediaPlayer.isPlaying()){ mediaPlayer.reset(); } try{ mediaPlayer.setDataSource(url); mediaPlayer.prepare(); mediaPlayer.start(); }catch (Exception e) { e.printStackTrace(); } } /** * 暂停 */ public void stopMusic(){ if(mediaPlayer.isPlaying()){ mediaPlayer.stop(); } } /** * GPS显示 * @param gps */ public void showGps(String gps){ Intent i = new Intent(TicketOneActivity.this, MapActivity.class); i.putExtra("gps", gps); startActivity(i); } /** * 提示信息 * @param msg */ public void showMsg(String msg){ if(CommonUtils.validateStr(msg)){ Toast.makeText(TicketOneActivity.this, msg, Toast.LENGTH_SHORT).show(); } if(CommonUtils.matchString(msg, "提交成功")){ finish(); } } }
上面的showMsg方法在页面被调用
//页面调用Activity中的JavaScriptInterface方法 function Msg(msg){ if (window.android){ window.android.showMsg(msg); }else{ alert(msg); } }
----------------------------------------------
在Activity中,也可以将处理的结果返回给页面。以下是一个在任务监听中将结果返回给页面JS的例子。
/** * 签名任务监听 * * @author lihua */ private TaskListener signTicketTaskListener = new TaskAdapter() { @Override public void onPostExecute(GenericTask task, String result) { //处理完成删除本地文件 FileHelper.delFiles(REC_DIR); JSONObject signInfo = (JSONObject) getApi().getBackResult(result, getApplicationContext()); String data = ""; String signdate = ""; try { data = signInfo.getString("result"); signdate = signInfo.getString("signdate"); } catch (JSONException e) { e.printStackTrace(); } String [] signArray = CommonUtils.splitByTag(data, "$"); String words = "",sign ="",gps="",voice="",gid = "",gdt=""; //add edit if(signArray!=null&&signArray.length>0) for (String s : signArray) { if(s!=null&&s.startsWith(Constants.START_TAG_WORD)) words = s.replace(Constants.START_TAG_WORD, ""); if(s!=null&&s.startsWith(Constants.START_TAG_SIGN)) sign = s.replace(Constants.START_TAG_SIGN, ""); if(s!=null&&s.startsWith(Constants.START_TAG_GPS)) gps = s.replace(Constants.START_TAG_GPS, ""); } String cbstr =""; //页面显示设置,调用页面名称为cbTicketSign的js函数 cbstr= "javascript:cbTicketSign('"+ words+"','"+ Constants.SERVER_FILE_CONTENT_TICKET + sign+"','"+ gps+"','"+ signdate+"'"+ ")"; System.out.println("cbstr:"+cbstr); wvTicketOne.loadUrl(cbstr); //关闭提示对话框 mDialog.dismiss(); } @Override public String getName() { return "Login"; } };
页面JS针对Activity的回调函数和JS的普通函数没有多大区别,以下是上面Activity中调用的JS函数。
//工作票签名回调 function cbTicketSign(sAttr, words, sign, gps ,voice , date){ if(sAttr=="workGroupSign"){ $('#workGroupSignLbl').html(words); $('#workGroupSignLbl').hide(); $('#workGroupSignImg').show(); $("#workGroupSignImg").attr("src","${root }"+sign); $("#workGroupSignGps").attr("src","${root}/images/ticket/gps.png"); $("#workGroupSignGps").bind("click",function(){ showGps(gps); }); } }
相关推荐
huha 2020-10-16
xfcyhades 2020-11-20
sgafdsg 2020-11-04
Michael 2020-11-03
fengyeezju 2020-10-14
ziyexiaoxiao 2020-10-14
业余架构师 2020-10-09
OuNuo0 2020-09-29
moses 2020-09-22
Angelia 2020-09-11
qinxu 2020-09-10
刘炳昭 2020-09-10
Nostalgiachild 2020-09-07
Nostalgiachild 2020-08-17
leavesC 2020-08-14
一青年 2020-08-13
AndroidAiStudy 2020-08-07
ydc0 2020-07-30
绿豆饼 2020-07-28