lizongbo at 618119.com 工作,生活,Android,前端,Linode,Ubuntu,nginx,java,apache,tomcat,Resin,mina,Hessian,XMPP,RPC

2012年01月31日

Android4.0系统浏览器支持window.performance API了

Filed under: Android,JavaScript,Web,前端开发 — 标签:, , — lizongbo @ 22:25

Android4.0系统浏览器支持window.performance API了

window.performance API是HTML5规范的一部分,
window.performance规范的官方文档在:
https://dvcs.w3.org/hg/webperf/raw-file/tip/specs/NavigationTiming/Overview.html
目前还是草稿阶段,中文介绍可以参考:
http://www.cnblogs.com/_franky/archive/2011/11/07/2238980.html
webkit的测试用例和属性列表可以参考:
http://svn.webkit.org/repository/webkit/trunk/LayoutTests/fast//dom/Window/window-properties-performance.html
http://svn.webkit.org/repository/webkit/trunk/LayoutTests/fast//dom/Window/window-properties-performance-expected.txt

Android4.0系统自带的浏览器实现了这一规范,并且还扩展实现window.performance.memory。

通过JavaScript实际测试结果如下:
window.performance.memory=[object MemoryInfo]
window.performance.timing=[object PerformanceTiming]
window.performance.navigation=[object PerformanceNavigation]
window.performance.timing.connectStart=1328016452403
window.performance.timing.responseStart=1328016452403
window.performance.timing.domLoading=1328016453857
window.performance.timing.connectEnd=1328016452403
window.performance.timing.domInteractive=0
window.performance.timing.fetchStart=1328016452403
window.performance.timing.secureConnectionStart=0
window.performance.timing.domainLookupStart=1328016452403
window.performance.timing.responseEnd=0
window.performance.timing.requestStart=1328016452403
window.performance.timing.loadEventEnd=0
window.performance.timing.domComplete=0
window.performance.timing.redirectStart=0
window.performance.timing.unloadEventEnd=1328016453646
window.performance.timing.domContentLoadedEventStart=0
window.performance.timing.domContentLoadedEventEnd=0
window.performance.timing.domainLookupEnd=1328016452403
window.performance.timing.navigationStart=1328016452402
window.performance.timing.unloadEventStart=1328016453646
window.performance.timing.loadEventStart=0
window.performance.timing.redirectEnd=0
window.performance.navigation.redirectCount=0
window.performance.navigation.type=1
window.performance.navigation.TYPE_NAVIGATE=0
window.performance.navigation.TYPE_RELOAD=1
window.performance.navigation.TYPE_BACK_FORWARD=2
window.performance.navigation.TYPE_RESERVED=255
window.performance.memory.totalJSHeapSize=0
window.performance.memory.usedJSHeapSize=0
window.performance.memory.jsHeapSizeLimit=0

PS:Android从2.1版本还扩展实现了navigator.connection:
通过JavaScript测试结果如下:
navigator.connection.type=4
navigator.connection.UNKNOWN=0
navigator.connection.ETHERNET=1
navigator.connection.WIFI=2
navigator.connection.CELL_2G=3
navigator.connection.CELL_3G=4

 

2010年12月20日

在Android的webview中定制js的alert,confirm和prompt对话框的方法

Filed under: Android,Java,前端开发 — 标签:, , , , , , — lizongbo @ 20:33

1.首先继承android.webkit.WebChromeClient实现MyWebChromeClient。

2.在MyWebChromeClient.java中覆盖onJsAlert,onJsConfirm,onJsPrompt三个方法。

3.在初始化Webview时设置调用webview.setWebChromeClient(new MyWebChromeClient());

4.在Webview载入的html中使用window.alert,window.confirm,window.prompt方法,系统弹出的将是自定义实现的对应对话框。

MyWebChromeClient.java的代码如下:

[code]
package com.lizongbo.mwebdev.android;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnCancelListener;
import android.content.DialogInterface.OnClickListener;
import android.content.DialogInterface.OnKeyListener;
import android.graphics.Bitmap;
import android.os.Message;
import android.util.Log;
import android.view.KeyEvent;
import android.webkit.JsPromptResult;
import android.webkit.JsResult;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.widget.EditText;

public class MyWebChromeClient extends WebChromeClient {

@Override
public void onCloseWindow(WebView window) {
super.onCloseWindow(window);
}

@Override
public boolean onCreateWindow(WebView view, boolean dialog,
boolean userGesture, Message resultMsg) {
return super.onCreateWindow(view, dialog, userGesture, resultMsg);
}

/**
* 覆盖默认的window.alert展示界面,避免title里显示为“:来自file:////”
*/
@Override
public boolean onJsAlert(WebView view, String url, String message,
JsResult result) {
final AlertDialog.Builder builder = new AlertDialog.Builder(view
.getContext());
builder.setTitle(“lizongbo的Android webview测试alert对话框”).setMessage(message)
.setPositiveButton(“确定”, null);
//不需要绑定按键事件
//屏蔽keycode等于84之类的按键
builder.setOnKeyListener(new OnKeyListener() {
@Override
public boolean onKey(DialogInterface dialog, int keyCode,
KeyEvent event) {
Log.v(“onJsAlert”, “keyCode==” + keyCode + “event=” + event);
return true;
}
});
//禁止响应按back键的事件
builder.setCancelable(false);
AlertDialog dialog = builder.create();
dialog.show();
result.confirm();//因为没有绑定事件,需要强行confirm,否则页面会变黑显示不了内容。
return true;
// return super.onJsAlert(view, url, message, result);
}

@Override
public boolean onJsBeforeUnload(WebView view, String url, String message,
JsResult result) {
return super.onJsBeforeUnload(view, url, message, result);
}

/**
* 覆盖默认的window.confirm展示界面,避免title里显示为“:来自file:////”
*/
@Override
public boolean onJsConfirm(WebView view, String url, String message,
final JsResult result) {
final AlertDialog.Builder builder = new AlertDialog.Builder(view
.getContext());
builder.setTitle(“lizongbo的Android webview测试confirm对话框”).setMessage(message)
.setPositiveButton(“确定”, new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
result.confirm();
}
}).setNeutralButton(“取消”, new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
result.cancel();
}
});
builder.setOnCancelListener(new OnCancelListener() {
@Override
public void onCancel(DialogInterface dialog) {
result.cancel();
}
});

//屏蔽keycode等于84之类的按键,避免按键后导致对话框消息而页面无法再弹出对话框的问题
builder.setOnKeyListener(new OnKeyListener() {
@Override
public boolean onKey(DialogInterface dialog, int keyCode,
KeyEvent event) {
Log.v(“onJsConfirm”, “keyCode==” + keyCode + “event=” + event);
return true;
}
});
//禁止响应按back键的事件
//builder.setCancelable(false);
AlertDialog dialog = builder.create();
dialog.show();
return true;
// return super.onJsConfirm(view, url, message, result);
}

/**
* 覆盖默认的window.prompt展示界面,避免title里显示为“:来自file:////”
* window.prompt(‘请输入您的域名地址’, ‘618119.com’);
*/
@Override
public boolean onJsPrompt(WebView view, String url, String message,
String defaultValue, final JsPromptResult result) {
final AlertDialog.Builder builder = new AlertDialog.Builder(view
.getContext());
builder.setTitle(“lizongbo的Android webview测试prompt对话框”).setMessage(message);
final EditText et = new EditText(view.getContext());
et.setSingleLine();
et.setText(defaultValue);
builder.setView(et);
builder.setPositiveButton(“确定”, new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
result.confirm(et.getText().toString());
}
}).setNeutralButton(“取消”, new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
result.cancel();
}
});

//屏蔽keycode等于84之类的按键,避免按键后导致对话框消息而页面无法再弹出对话框的问题
builder.setOnKeyListener(new OnKeyListener() {
@Override
public boolean onKey(DialogInterface dialog, int keyCode,
KeyEvent event) {
Log.v(“onJsPrompt”, “keyCode==” + keyCode + “event=” + event);
return true;
}
});

//禁止响应按back键的事件
//builder.setCancelable(false);
AlertDialog dialog = builder.create();
dialog.show();
return true;
// return super.onJsPrompt(view, url, message, defaultValue, result);
}

@Override
public void onProgressChanged(WebView view, int newProgress) {
super.onProgressChanged(view, newProgress);
}

@Override
public void onReceivedIcon(WebView view, Bitmap icon) {
super.onReceivedIcon(view, icon);
}

@Override
public void onReceivedTitle(WebView view, String title) {
super.onReceivedTitle(view, title);
}

@Override
public void onRequestFocus(WebView view) {
super.onRequestFocus(view);
}

}
[/code]

2010年04月14日

将google托管的ajax js文件同步到本地服务器

Filed under: Java — 标签:, , — lizongbo @ 21:14
google提供了供全世界访问的公共javascript库托管,
参考:http://code.google.com/intl/zh-CN/apis/ajaxlibs/
只要使用 <script src=”http://www.google.com/jsapi”></script> 引用google的js,
然后使用 google.load() 方法,就可以指定载入对应库的对应版本。 例如:google.load(“jquery”, “1”);
就可以载入当前最新的jquery库。
虽然google承诺无限期地托管各版本的js文件,但是由于大部分人知道的原因,在国内使用这种方式共享js资源存在一定风险。
因此,我参考google对这些js的部署结构,将js下载回来放到了本地服务器。
目前托管的js库有以下组件:
jQuery
jQuery UI
Prototype
script.aculo.us
MooTools
Dojo
SWFObject
Yahoo! 用户界面库 (YUI)
Ext Core
全部都同步下来,操作步骤如下:
先在Firefox里打开:
http://www.google.com/jsapi
然后将js保存到成本地文件,然后用Aptana打开,并格式化,然后找到
google.loader.rpl,然后把括号里一段json对象复制出来,定义成  var jslibs;
然后通过js生成对应js的url。然后使用java下载到本地保存到对应目录。
代码参考:
<SCRIPT LANGUAGE=”JavaScript”>
<!–
var jslibs={
“:jquery” : {
“versions” : {
“:1.2.3” : {
“uncompressed” : “jquery.js”,
“compressed” : “jquery.min.js”
},
“:1.3.1” : {
“uncompressed” : “jquery.js”,
“compressed” : “jquery.min.js”
},
“:1.3.0” : {
“uncompressed” : “jquery.js”,
“compressed” : “jquery.min.js”
},
“:1.3.2” : {
“uncompressed” : “jquery.js”,
“compressed” : “jquery.min.js”
},
“:1.2.6” : {
“uncompressed” : “jquery.js”,
“compressed” : “jquery.min.js”
},
“:1.4.0” : {
“uncompressed” : “jquery.js”,
“compressed” : “jquery.min.js”
},
“:1.4.1” : {
“uncompressed” : “jquery.js”,
“compressed” : “jquery.min.js”
},
“:1.4.2” : {
“uncompressed” : “jquery.js”,
“compressed” : “jquery.min.js”
}
},
“aliases” : {
“:1” : “1.4.2”,
“:1.4” : “1.4.2”,
“:1.3” : “1.3.2”,
“:1.2” : “1.2.6”
}
}
}
//–>
</SCRIPT>
<TEXTAREA id=”urls” NAME=”urls” ROWS=”40″ COLS=”80″></TEXTAREA>
<SCRIPT LANGUAGE=”JavaScript”>
<!–
var urls=””;
for(var jslibName in jslibs){
var jslibList=jslibs[jslibName];
for(var jslibver in jslibList[“versions”]){
var url1=”http://ajax.googleapis.com/ajax/libs/”+jslibName.replace(“:”,””)+”/”+jslibver.replace(“:”,””)+”/”+jslibList[“versions”][jslibver][“uncompressed“];
var url2=”http://ajax.googleapis.com/ajax/libs/”+jslibName.replace(“:”,””)+”/”+jslibver.replace(“:”,””)+”/”+jslibList[“versions”][jslibver][“compressed“];
urls=urls+”\n”+url1+”\n”+url2;
}
}
document.getElementById(“urls”).value=”String[] urlsStrings={”
+urls
+”};”
+”  for (int i = 0; i < urlsStrings.length; i++) {”
+”   try {”
+”    URL url = new URL(urlsStrings[i]);”
+”    File file = new File(\”E:/js\”, url.getPath());”
+”    file.getParentFile().mkdirs();”
+”    PrintWriter pw = new PrintWriter(file);”
+”    URLConnection urlc = url.openConnection();”
+”    urlc.setConnectTimeout(5000);”
+”    urlc.connect();”
+”    BufferedReader in = new BufferedReader(new InputStreamReader(”
+”      urlc.getInputStream(), \”UTF-8\”));”
+”    String line;”
+”    while ((line = in.readLine()) != null) {”
+”     pw.write(line);”
+”     // sb.append(line).append(‘\n’);”
+”    }”
+”    pw.close();”
+”    in.close();”
+”   } catch (Exception e) {”
+”    e.printStackTrace();”
+”   }”;
//–>
</SCRIPT>
Older Posts »

Powered by WordPress