星期二, 四月 02, 2013

编写自己的Wrapper Class

   编写自己的Wrapper Class
A URL Connection to a Java ARchive (JAR) file or an entry in a JAR file.
The syntax of a JAR URL is:
jar:<url>!/{entry}

参考: java.net.JarURLConnection
http://java.sun.com/j2se/1.4.2/docs/api/java/net/JarURLConnection.html

目的:要解决在可执行Jar文件中使用包含在其他地方的Jar文件中的class.

初级方法:使用Java extension机制,通过编写启动脚本来解决问题:
  把第三方的Jar Library复制到{java home}\jre\lib\ext 目录下
或使用java ?Xbootclasspath 命令行参数:

    -Xbootclasspath:<directories and zip/jar files separated by ;>
                      set search path for bootstrap classes and resources
    -Xbootclasspath/a:<directories and zip/jar files separated by ;>
                      append to end of bootstrap class path
-Xbootclasspath/p:<directories and zip/jar files separated by ;>

大多数的Java 应用服务器都使用了制作启动脚本的方法.
例子: 脚本run.bat 可以参看Jboss Application的run.bat或run.sh脚本

中级方法: 利用Jar 扩展机制规范,通过制作安装包来解决问题.
   利用Jar包扩展(extension)机制通过Manifest Specification 来编写
参考: http://java.sun.com/j2se/1.4.2/docs/guide/jar/jar.html
利用META-INF/MANIFEST.MF文件中的Class-Path:来指定需要引用的第三包,
将需要的第三方包放到在Class-Path:指定的目录下,然后把主程序jar包和第三包
制作安装包,在安装的时候按照目录结构安装。然后SystemClassLoader(sun.misc.Launcher)
自动加载

高级方法,编写自己的Container类或bootStrap Wrapper类.
思路:实现一个Custom ClassLoader-系统类java.开头的类让SysemClassLoader来载入,
  其他的类用CustomClassLoader读取给出的Jar Library 包来实现.
最终实现 Class.forName(String name)自动使用Custom ClassLoader来寻找和加载class.

这里需要介绍一些Java ClassLoader的一些限制的知识,ClassLoader的详细原理,可以参看
其他相关介绍.
Hack Java:
强行设置系统默认的ClassLoader,有作用吗?No
Field tclass = ClassLoader.class.getDeclaredField("scl");
tclass.setAccessible(true);
tclass.set(null,Mydefault);

JVM的 SystemClassLoader是 sun.misc.Luncher的实例,这里面包含native的本地方法,
其中工作算法不得而之,JVM唯一给我们留下的扩展机制就是改变ContextClassLoader来实现
JVM的ClassLoader委托模式(delegation mode).

  详细介绍:
Custom ClassLoader代码
/**
* Don't Reinvent wheel
* 使用 URLClassLoader 来载入Jar Library
*
*/

import java.io.InputStream;
import java.io.File;
import java.net.URLClassLoader;
import java.util.*;
import java.net.URL;

public class myClassLoaderByURL extends java.lang.ClassLoader
{
static java.net.URL _url=null;
static String libpath=null;
public static  myClassLoaderByURL defaultld=null;

public myClassLoaderByURL(String liburl,ClassLoader parent) throws Exception
{
     super(parent);
  if(!checkJar(liburl))
   {
  throw new java.lang.RuntimeException("Jar File "+liburl+" not exist");        
        }
     try
     {
     _url = new  URL("jar:file:/"+liburl+"!/");
}catch(Exception e)
{
     e.printStackTrace();
     }
}
protected  Class findClass(String name) throws ClassNotFoundException
{
Class speClass=null;
try
{
     speClass = getParent().loadClass(name );
     System.out.println("myClassLoaderByURL Msg: parent CLD find Class............"+name);
} catch( ClassNotFoundException cnfe )
   {
  try
  {
String rname = name;
rname = rname.replace ('.', '/') + ".class";//重要,必须还原为目录格式
  System.out.println("myClassLoaderByURL Msg: Option jar Lib:"+_url.toExternalForm());
  System.out.println("myClassLoaderByURL Msg: find class name:"+rname);
  URLClassLoader ucld = new URLClassLoader(new URL[]{_url},myClassLoaderByURL.class.getClassLoader());
  InputStream is = ucld.getResourceAsStream(rname);
if(is==null)
{
  throw new ClassNotFoundException(" Can't Find Specified Class:"+name);
}
int size=is.available();

byte[] b = new byte[size];
is.read(b,0,size);
is.close();
     speClass = defineClass(name,b,0,b.length);

}catch(Exception ioe)
{
     ioe.printStackTrace();
     throw new ClassNotFoundException();
}
}catch(Exception ce)
{
     ce.printStackTrace();
     throw new ClassNotFoundException();
}
System.out.println("myClassLoaderByURL Msg: ok,load success");

return speClass;
}

public Class loadClass( String pClassName, boolean pResolve ) throws ClassNotFoundException {
       System.out.println("myClassLoaderByURL Msg:loadClass(), resolve: " + pResolve );
       Class lClass = findLoadedClass( pClassName );
       lClass  = findClass( pClassName );


       //ResolveClass.
       try {
           if( pResolve ) {
               System.out.println("myClassLoaderByURL Msg:resolve class: " + lClass );
               resolveClass( lClass );
           }
       } catch (Error e) {
           e.printStackTrace();
           throw e;
       }
       return lClass;
   }
public Class loadClass( String pClassName) throws ClassNotFoundException
{
     Class t =loadClass(pClassName,true);
     return t;
}

private boolean checkJar(String jarfile)
{
     System.out.println("myClassLoaderByURL Msg: checkJar:"+jarfile);
     File jarf = new File(jarfile);
     try
     {
     System.out.println(jarf.getCanonicalFile());
}catch(Exception ee)
{
     }
     return jarf.exists();
}
}

编写自己的Boot Wrapper类:

import java.io.*;
import java.util.zip.*;
import java.util.jar.*;
import java.net.URLClassLoader;
import java.net.URL;
import java.lang.reflect.Method;

public class testClassLoaderByURL extends java.lang.Thread
{
static myClassLoaderByURL Mydefault;
static String JarLibraryPath="";
static String executedClass = "";
public static void main(String[] args) throws Exception
{
if(args.length <2)
{
     System.out.println("Syntax testClassLoaderByURL /path/xx.jar Executable class name(include main method");
     System.out.println("Example: java testClassLoaderByURL c:\\temp\\test.jar org.sun.go");
     System.exit(0);
}
JarLibraryPath=args[0];
executedClass = args[1];
//here can verified parameter

System.out.println("Jar is:"+JarLibraryPath+", Class is:"+executedClass);
System.out.println("Begin Load");
Mydefault = new myClassLoaderByURL(JarLibraryPath,testClassLoader.class.getClassLoader());
myClassLoaderByURL.defaultld = Mydefault;

      Thread.currentThread().setContextClassLoader(Mydefault);
     
         Class cls = Mydefault.loadClass(executedClass);//重要.
         Method main = cls.getMethod("main", new Class[]{String[].class});
         main.invoke(cls, new Object[]{args});


}
}
    

SAP相关 第一帖

2010.4.20日。

今天装好了Net Weaver 7.1 SR1
和 笔记本上ECC 6.0 EH4 FOR MSSQL 2005 WINDOW 7

准备全力投入SAP 了


Javascript 变量范围问题.

一段简单的代码引出的问题:

function test1()
{
    for (i=0;i<3 br="" i="">      {
          print("test1:"+i);
          }
    }
   
function test2()
{
    for (i=0;i<3 br="" i="">      {
          print("test2:"+i);
          test1();
          }
    }
   
   
test2();

得到结果,出乎人的意料:
test2:0
test1:0
test1:1
test1:2

修改函数test1 如下:
function test1()
{
    for (var i=0;i<3 br="" i="">      {
          print("test1:"+i);
          }
    }
 得到结果,符合预期:
test2:0
test1:0
test1:1
test1:2
test2:1
test1:0
test1:1
test1:2
test2:2
test1:0
test1:1
test1:2


这是因为Javascript的变量范围 scope是函数级别的,并没有其他语言级别的local概念。

所以为了避免递归或者嵌套查询问题,最好在变脸名前宣布var,或者使用Ecmascript 1.7
里的let关键词来避免这种情况。

关于可靠性的数序模型

可靠性特征量间的关系
可靠性特征量中可靠度R(t),累积失效率(也叫不可靠度)F(t)、概率密度f(t)和失效率λ(t)是四个基本函数,只要知道其中一个,则所有变量均可求得.基本函数间的关系见下表。
可靠性特征量 R(t) F(t) f(t) λ(t)
R(t)(可靠度) - 1-F(t) eqb1_3_5.gif (1756 字节) eqb1_3_8.gif (1821 字节)
F(t)(累积失效率) 1-R(t) - eqb1_3_6.gif (1749 字节) eqb1_3_9.gif (1841 字节)
f(t)(概率密度) eqb1_3_1.gif (1781 字节) eqb1_3_3.gif (1750 字节) - eqb1_3_10.gif (1875 字节)
λ(t)(失效率) eqb1_3_2.gif (1810 字节) eqb1_3_4.gif (1884 字节) eqb1_3_7.gif (1846 字节) -

Android Eclipse Project JNI 指引

 最佳指引,在Eclipse里的Android 项目使用JNI依赖的资源,
需要首先安装JDK,Eclipse,cygwin环境或者mingw环境,下载Android SDK,ndk.

http://code.google.com/p/awesomeguy/wiki/JNITutorial#JNI_Sources

JNI目录必须在项目最顶级目录下建立。所有的文件都要放在这里,
包括Android.mk文件,Android.mk文件内容看上述指引内复制后修改为自己实际目录即可。
.c/.cc文件。

头文件,可以借用javah来生成,如图设置Eclipse的位置工具即可。
如此则可以运行该外部工具生成对应的.h头文件。
编写完自己的c/cc文件后需要到终端运行NDK 提供的命令行工具来编译生成库。
项目的根目录及包含jni的该层目录。
 运行ndk-build即可完成编译工作。看ndk-build编译图.






ndk-build编译图.


星期二, 三月 26, 2013

如何避免 field symbol has not yet been assigned 小技巧tips

Field Symbol类似其他语言中的指针,所有用Assign <fs1> to <fs2>;其实是一个指针,任何对<fs2>的修改,
都反应在<fs1>
所以必须用赋值操作 = 或者move, <fs2>=<fs1>. 或者move <fs1> to <fs2>来进行copy value的操作。
但是这两个语句往往就会出现 field symbol has not yet been assigned.

所以根据<fs2>的类型来做一下申明即可避免这种错误。
Data: ref1 like <fs2>的数据类型,如果是个internal table的 符号,
field-symbols <fs2> like line of so_table.
那么就需要以下声明:
data: lc_dref1 like line of so_table.
ASSIGN lc_dref1 TO <fs2>.
然后再使用 <fs2> = <fs1>. 就无问题了。

可以使用 UNASSIGN <fs2>  来释放。

 


星期二, 三月 19, 2013

星期一, 三月 18, 2013

如何在ant里获取时间字符串,然后用于其他task

比如,我想删除一个目录下匹配通配符的一些文件,然后新建一个包含当前时间文件名的文件,就要如此:
1.得到当前时间格式化字符串 TODAY_UK

<tstamp>
      <format property="TODAY_UK" pattern="yyyyMMdd_HHmmsssss"  locale="cn,CN"/>
    </tstamp>

2.删除符合某个规则所有文件。

      <delete   verbose="true" failonerror="false">
             <fileset dir="." includes="Bonus*.jar"/>
 </delete>

3.新建包含时间字符串的文件。
<target name="all">
            <jar destfile="Bonus${TODAY_UK}.jar" filesetmanifest="skip">
            <zipfileset dir="." prefix="bonus" >
            <exclude name="sqlite*.*"/>
                              <include name="*.c"/>
                              <include name="*.cpp"/>
                              <include name="*.h"/>
                              <include name="*.xml"/>
                              <include name="*.xrc"/>
                              <include name="*.rc"/>
                              <include name="*.bat"/>
                              <include name="*.sql"/>
            </zipfileset>   
   </jar>
</target>

星期三, 三月 13, 2013

Google V8 在 Window 7和mingw 环境下编译通过

首先 V8 Javascript 官方不支持mingw环境,所以编译开始就要有心理准备肯定会有一些错误,一般是在link的时候出现;
一般通过修改相关配置文件的命令行选项可以纠正之。

其次,出现 undefined reference to vtable for XXXX的link错误提示,vtable是c++用来处理虚函数编译器算法,可以参看

Virtual method table - Wikipedia, the free encyclopedia


所以出现这种提示,2种可能,
1.虚函数方法没有实现,只是在.h头部文件中声明而已。
2.实现虚函数的.cc文件没有放到link命令行内。

1.首先下载最新的V8 Javascript源代码(必须首先安装上svn工具)
svn checkout http://v8.googlecode.com/svn/trunk/ v8
注意:
http://v8.googlecode.com/svn/branches/ 这个目录下是用于Google浏览器的稳定版本,可以下载最大数字的版本编译svn checkout http://v8.googlecode.com/svn/branches/3.16/ v8_316
2.进入v8 目录 cd v8,然后准备新的Google v8 编译管理工具 gyp
 
svn co http://gyp.googlecode.com/svn/trunk build/gyp

然后运行:
python build\gyp_v8 --generator-output="out" -Dv8_target_arch=ia32

注意:如果是使用msvc可以告之gyp使用的visual studio express 版本:

python build\gyp_v8 -G msvs_version=2010 --generator-output="out" -Dv8_target_arch=ia32

最后开始编译并声称v8.dll和libv8.a文件
scons.py  arch=ia32 os=win32 toolchain=gcc library=shared I_know_I_should_build_with_GYP=yes

如果最后link出现了vtable 错误,就寻找包含的方法在那个.cc文件,编译并手动生成v8.dll和libv8.a

目前这个版本会有3个文件在mingw下编译有3个文件被遗漏了
1.code-stubs-hydrogen.cc
2.sweeper-thread.cc
3.marking-thread.cc

通过命令行:
g++ -o obj\release\marking-thread.o -c -fno-rtti -fno-exceptions -DWIN32 -Wall -Werror -W -Wno-unused-parameter -Woverloaded-virtual -Wnon-virtual-dtor -pedantic -Wno-long-long -Wno-pedantic-ms-format -m32 -O3 -fomit-frame-pointer -fdata-sections -ffunction-sections -DWIN32 -DV8_TARGET_ARCH_IA32 -DENABLE_DEBUGGER_SUPPORT -Isrc src\marking-thread.cc

分别生成这.o文件在obj\release下之后,将其加入到最后生成 v8.dll和libv8.a的命令中,即可生成动态库.

V8的shell d8目前只有依赖msvc 生成,要编译生成d8.exe用来调试javascript程序,
还必须要下载 第三方的包,
在v8 或v8_316目录下:
1.svn co http://src.chromium.org/svn/trunk/deps/third_party/cygwin@66844 third_party/cygwin
2.svn co http://src.chromium.org/svn/trunk/tools/third_party/python_26@89111 third_party/python_26

这两个包用来msvc用来生成snapshot.lib的过程中,必须的。

至此即可用v8 Engine嵌入自己的程序来使用了。

v8 支持Android.

星期六, 三月 09, 2013

v8 Javascript嵌入Javascript代码注意。

C++ 支持多行字符串书写语法multiple line string
const char * jscode=
  "ball=['1','2','3','4'];\n"
  "result=''\n"
   "function test(){ for(i=0;i<ball.length;i++){result=result+ball[i];}return result;}"
 " ts= test();"
 ;
注意如果不加入换行\n,则每行代码结束必须加入分号;,来表示一行代码结束

星期五, 三月 08, 2013

Window 7 Mingw 环境编译 Google V8 Javascript 步骤

首先寻找一个稳定的代码版本是最重要的,我一开始按照如下URL里的说明下载代码:

http://code.google.com/p/v8/wiki/Source
svn checkout http://v8.googlecode.com/svn/trunk/ v8

 这个代码根据说明是更新频率是按周来的,实际我发现一天之内就可能有多个版本。
所以不得不寻找稳定的版本,最后找到一下地址:
  http://v8.googlecode.com/svn/branches/

得到最新的稳定版本到 v8_39目录下
  svn checkout http://v8.googlecode.com/svn/branches/3.9/ v8_39

然后进入v8_39目录用以下命令成功编译.

d:\Python26\Scripts\scons.py mode=release os=win32 toolchain=gcc importenv=PATH library=shared snapshot=on I_know_I_should_build_with_GYP=yes

1.
编译的环境,首先需要安装mingw环境,我使用的tdm gcc 4.7.1 的
32位版本,没有使用mingw64版本。
下载: http://tdm-gcc.tdragon.net/download ,32bit.

http://sourceforge.net/projects/tdm-gcc/files/TDM-GCC%20Installer/tdm-gcc-4.7.1-2.exe/download

2.安装python,我使用的python for window 2.6版本并且要安装scons 模块.
  scons 下载 :http://www.scons.org/ ,我使用的2.2.0版本。
 下载展开到一个目录后,运行 python setup.py 就将scons 安装到了python 安装目录的Scirpts目录下。

这样安装环境就准备完毕了。

scons在google v8 javascript环境里已经不支持了。但GYP方式目前在mingw下工作不利,所以这也是权宜之计了。


after source code download,use follow command line .


all is ok.

星期二, 三月 05, 2013

It is time say good by mingw.

越来越多open source 开始使用C++了,到此不同平台,不同编译器
之间的互操作越来越困难了,C时代的mingw的得心应手,在Visual C++的复杂,和import library的机制下,需要花费越来越多的精力了。

不同的C++生成DLL之间的使用,终于成为一个不可能的任务了。

终于决定gcc为主,msvc用作window平台工具了。


星期一, 二月 25, 2013

delopy web app to Google appengine

download appengine jdk and run follow command:

D:\appengine-java-sdk-1.7.5\bin\appcfg.cmd update war -A szpureweb

星期日, 一月 27, 2013

如何在Eclipse 中实现Build number的功能

思路:利用Ant builder
如图
1.首先输入当前Project的ant build.xml文件,和一个保存编译计数器的properties文件。

加入新的target用name 来指定:
    
        
        
        
        


   




2.给项目加入Ant Builder ,项目属性里寻找Builder 然后加入Ant Builder,然后将其用
up按钮移动到最顶部,让其第一个被调用.


 3.要加入refresh的功能和默认的target,这个参考如上和如下两个图就知道了.







星期二, 一月 22, 2013

keytool 常见用法

查看一个已经生成的keystore 文件
keytool -list -v -keystore debug.keystore

需要密码

星期三, 一月 16, 2013

abap 执行本地SQL Native SQL

 exec sql.
 insert into test(code) valeus(
                      :pl_wa-Entity_Code,
                    );
      endexec.
      if sy-subrc ne 0.
        write:/,'fail!'.
      endif.
    commit work.

SAP ABAP如何直接运行一个本地SQL SELECT查看结果.

ECC 6.t-code db02即可做到这个。
DB02 -> Diagnosis -> SQL Command editor



星期一, 一月 14, 2013

Dynamic load properties file by Absolulated directory way.

这样可以在classpath里修改controlSetup.properties文件的值,而直接在程序里使用。
通过getResourceAsStream方式载入的文件被Cache.

public InputStream getpro()
 {
String propersetupfile="com/resources/controlSetup.properties";    
String dir=this.getServlet().getServletContext().getRealPath("/");
System.out.println("real directory:"+dir);
String pdir=dir+"WEB-INF/classes/"+propersetupfile;
System.out.println("properties file in:"+pdir);
InputStream fi=null;
try
{
fi=new FileInputStream(pdir);
}catch(Exception e)
{
e.printStackTrace();
       
     fi= this.getClass()
        .getClassLoader()
        .getResourceAsStream(propersetupfile);
}
return fi;
//     return this.getClass()
//        .getClassLoader()
//        .getResourceAsStream(propersetupfile);
 }  

Java读取某个目录下的制定properteis tips

 String fileName = "test.app"; // 注意格式 test 为包名,默认读取后缀为.properties的文件
ResourceBundle rb = ResourceBundle.getBundle(fileName, new java.util.Locale(""));

在test目录下有2个文件 app.properties和app_en.properties
如果想读取 app.properties 则只要给一个空的Locale("")即可。
同样,想读取app_en.properties需要用 Locale("en")

Java计算tips,如何从一个数字字符串中得到字符整数转换。

char character = string.charAt(index);
int x = (character – '0') * 20;

星期四, 一月 10, 2013

abap 如何获得Internal table的定义字段,用RTTS

*&---------------------------------------------------------------------*
*& Report  Z_GET_TABLE_COLUMNNAME
*& get component structure by RTTS.(Runtime Type Servcies)
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*

REPORT  Z_GET_TABLE_COLUMNNAME.

TYPESBEGIN OF typ_itab,
         sex(20TYPE c,
         tigh TYPE i,
         salary TYPE p LENGTH 12 DECIMALS 3,
         name type string,
          END OF typ_itab.
DATAi_itab TYPE TABLE OF typ_itab "if has with header line,exceptin happend
      l_tabledescr_ref TYPE REF TO cl_abap_tabledescr,
      l_descr_ref      TYPE REF TO cl_abap_structdescr,
      wa_table TYPE abap_compdescr.

l_tabledescr_ref ?= cl_abap_typedescr=>describe_by_datai_itab ).
l_descr_ref ?= l_tabledescr_ref->get_table_line_type).

LOOP AT l_descr_ref->components INTO wa_table .
  WRITE/ wa_table-name,wa_table-type_kind,
         wa_table-length,wa_table-decimals.
ENDLOOP.

cl_salv_table 获得 所有internal table 定义field name

FORM alv_fieldcat_reset2
    USING alv_table   TYPE REF TO cl_salv_table.
  DATA: lo_columns TYPE REF TO cl_salv_columns.
  DATA: lo_column TYPE REF TO cl_salv_column.
  "add field authority check
  DATA: alv_fieldcat TYPE slis_fieldcat_alv.
  DATA: cx_exc_ref    TYPE REF TO cx_root.
  DATA error_text TYPE c.
  DATA:
    lta_column_ref TYPE salv_t_column_ref
  .
  FIELD-SYMBOLS:
    <lfs_column> LIKE LINE OF lta_column_ref
  lo_columns = alv_table->get_columns( ).
      lta_column_ref = lo_columns->get( ).
      LOOP AT lta_column_ref ASSIGNING <lfs_column> .
        lo_column = <lfs_column>-r_column.
 write <lfs_column>-columnname.

      ENDLOOP. 

星期三, 一月 02, 2013

Volatile: why prevent compiler reorder code

Prior to Java-5 ,编译器竟然会调整语句的顺序来优化!!!

解释链接:

http://www.cs.umd.edu/~pugh/java/memoryModel/jsr-133-faq.html#volatile

星期三, 十一月 28, 2012

Android,不再为线程烦恼

不再为线程烦恼


当你第一次开始一个Android应用的时候,有一个名字为"main"的线程就自动被系统建立了.这个main线程也被叫做 UI 线程,也就是用户界面线程,UI线程是非常重要的,它负责将事件也包括绘制(Drawing)事件分派到恰当的widgets;它也是你和Android Widgets之间互动的线程.
例如,当你触摸屏幕上的按钮,UI线程就将触摸事件分派到该widget,然后该widget设置状态为按下状态并发送一个invalidate请求到事件队列内.
UI线程出队该请求并通知widget来重绘自己.

这种单一线程模式将造成可怜的性能问题,因此在Android应用中一般是不考虑的。当所有事情都在单一线程中发生的时候,将造成线程长时间操作,比如网络访问或数据库查询,会造成这个
线程阻塞所有的用户接口.当长时间操作进行时,没有事件可以被分派,包括绘制(Drawing)事件.
从用户的角度来看,应用明显挂住了.甚至更糟,如果UI被阻塞了超过数秒(大约5秒)用户将得到声名狼藉的 ANR-程序没有响应.application not responding" 对话框。

如果你想试试这看起来有多糟糕,你可以写一个简单的应用一个按钮的onClickListener方法调用Thread.sleep(2000).
这个按钮将保持按下的状态大约2秒钟然后才返回到正常状态。当按钮按下时,用户轻易发现应用很慢.

现在你知道在UI线程内应该避免冗长的操作了,必须正确的使用额外的线程(后台工人线程worker thread)来正确的执行这些操作.
还是用一个例子:点击然后从网络上下载一个图像并显示在ImageView里.

public void onClick(View v) {
new Thread(new Runnable() {
public void run() {
Bitmap b = loadImageFromNetwork();
mImageView.setImageBitmap(b);
}
}).start();
}

初看起来,这段代码似乎给出了一个不阻塞UI线程的一个非常好的解决方法.但很不幸,它又违背了单一线程模式:
Android UI 工具集不是线程安全(thread-safe)的,必须一定要在UI线程里来操作.
这段代码内,ImageView被工人线程操作,这会造成一些奇怪的问题,并且让跟踪和修正错误变得困难和费时。

Android 提供了几种方法从其他线程里访问UI线程.罗列以下组件及方法:

这些方法或者类都能正确的使用我们先前的例子:

public void onClick(View v) {
  new Thread(new Runnable() {
public void run() {
final Bitmap b = loadImageFromNetwork();
mImageView
.post(new Runnable() {
public void run() {
mImageView
.setImageBitmap(b);
}
});
}
}).start();
}

不完美的是,这些类或方法让代码非常难以阅读.当你需要复杂频繁更新UI的时候,就更糟糕了.为了补救这个问题,Android 1.5 提供了新的功能
类:AsyncTask简化长时间操作的任务需要和UI 通讯的问题.
AsyncTask在Android 1.0和1.1也存在不过叫做UserTask,提供了一样的API你可以copy你的应用的源带码使用.

AsyncTask的目标是替你来管理thread.我们先前的例子可以很容易重写成AsyncTask的例子:

public void onClick(View v) {
  new DownloadImageTask().execute("http://example.com/image.png");
}

private class DownloadImageTask extends AsyncTask {
protected Bitmap doInBackground(String... urls) {
return loadImageFromNetwork(urls[0]);
}

protected void onPostExecute(Bitmap result) {
mImageView
.setImageBitmap(result);
}
}


如你所见,AsyncTask必须用做它的子类.最重要的是要记住AsyncTask实例必须在UI线程内建立并只能一次执行一次
你可以浏览 AsyncTask documentation 来完全理解它是如何工作的,这里给出一个快速工作原理浏览:


除了官方文档,你也可以看到几个负载的例子的源代码比如(ShelvesActivity.java AddBookActivity.java) 和 Photostream (LoginActivity.java, PhotostreamActivity.java和 ViewPhotoActivity.java). 我高度推荐读读Shelves 源代码来看看如何永久化任务访问配置改变并如何在Activity被放弃的时候放弃属性.

无论是否你要使用AsyncTask,永远记住这种线程模型的2个规则:不要阻塞UI线程并且确定Android UI 工具箱只能在UI线程内访问。
AsyncTask 是这两个要求很容易实现。

如果你想学习更多很Cool的技术,来加入我们 Google I/O. Android 团队成员在这里会给你 series of in-depth technical sessions 并回答你的问题.

Fwd: 如何在Android 4 使用optins menu



Android 4后,Google
Google is happy to bid farewell to the Menu button, and they don’t even want you calling that funny little three-dot button the “menu.”
 Rather, Google wants us to think of the menu at an “action overflow.”
 When there isn’t enough space in the Action Bar for all the icons you might need in an app, the action overflow handles it.

编写兼容的程序从2.x-4.x ,在Eclipse ADT 里编译的时候就必须要选择高版本LEVEL API,
 但在发布的时候,AndroidManifest.xml,要删除掉         android:targetSdkVersion="16"  ,而只保留 android:minSdkVersion="10"
android 2.3.3 level api,则在Android 4.X 系统下,options menu出现在了系统导航条上的三点菜单里。
注意,options menu->action bar ->system navigation bar -> overflow menu 几个概念的演化。
如图



星期四, 十一月 22, 2012

Fwd: git Cannot pull into a repository with state: MERGING 解决

 首先使用 Merge Tool,选择编辑有冲突的文件比较。
 编辑后,使用add to index ,然后再commit,即可解决这个问题

Fwd: 改变adb shell ctrl+c终端程序,并直接退出adb shell的问题

su,root后,直接用stty intr ^X
然后用ctrl+x 回车结束程序运行比如ping之类

星期四, 十一月 15, 2012

Android 4.2 root 后adb shell ,su 发现read-only file system.如何解决

Android 4.2 ,root后通过adb shell进入发现read only file system 问题
mount 发现/system / 两个文件卷的路径,一般是第一列为物理路径,
 第二列为系统卷,重新remount为可读写,
1.mount -o remount,rw rootfs
2.mount -o remount,rw /dev/block/platform/omap/omap_hsmmc.0/by-name/system
3.现在可以复制东西到/etc/了
原因是Android要在/ 卷和/system卷之间做很多的配置文件同步的比如,/etc/hosts,就会在/system/etc 下同步的。

正则表达式,发现一行包含某个特定的字符串,但又不包含某个特定的字符串

'^(?=.*IncludeString)((?!ExcludeString).)*$'
(?= 肯定前瞻
(?! 否定后顾

正则表达式发现不包含某个特定字符串的表达

^((?!ExcludeString).)*$ 这个可以处理只包含ExcludeString的行
而以下pattern则无法处理包含ExcludeString的独立行
^(.(?!stringToExclude))*$

星期四, 十一月 08, 2012

正则表达式的例子

grep -P "\d{7}(\s+|:)(\d\d[\s+-]?){7}"  可以工作,但可以修改为

更加精简的pattern.

grep -P "\d{7}.(\d\d).{7}"


用来搜索同一个文本内的如下格式的数据:

2012083:04-09-14-15-26-33-04                               
2012084:02-10-20-26-28-29-14                               
2012103 11 14 32 33 02 09 04 14 11 33 32 364812438 452814888

Re: 正则表达一点例子.

grep -P "\d{7}(\s+|:)(\d\d[\s+-]?){7}"  可以工作,但可以修改为
更加精简的pattern.
grep -P "\d{7}.(\d\d).{7}"

用来搜索同一个文本内的如下格式的数据:

2012083:04-09-14-15-26-33-04                               
2012084:02-10-20-26-28-29-14                               
2012103 11 14 32 33 02 09 04 14 11 33 32 364812438 452814888



2012/11/8 凯幄解 <javacave@gmail.com>
grep -P "\d{7}(\s+|:)(\d\d[\s+-]?){7}"

用来搜索同一个文本内的如下格式的数据:

2012083:04-09-14-15-26-33-04                               
2012084:02-10-20-26-28-29-14                               
2012103 11 14 32 33 02 09 04 14 11 33 32 364812438 452814888

正则表达一点例子.

grep -P "\d{7}(\s+|:)(\d\d[\s+-]?){7}"

用来搜索同一个文本内的如下格式的数据:

2012083:04-09-14-15-26-33-04                               
2012084:02-10-20-26-28-29-14                               
2012103 11 14 32 33 02 09 04 14 11 33 32 364812438 452814888

星期三, 十一月 07, 2012

规则表达一些概念

正则表达式中的贪婪(greedy)、勉强(reluctant)和侵占(possessive)

贪婪(greedy)

一般默认情况下,正则的量词是贪婪的,也就是“尽可能多地匹配”,符号通常是:* + ? {num,num}
举个例子说,比如"a+"这个正则,对于"aaaaaab"这个字符串,它就会匹配到6个a,这是最常见的情况

勉强(reluctant)[也叫懒惰(lazy)]

勉强与贪婪正好是相对的,它“尽可能少地匹配”,它会用最小的努力,去完成(应付)正则的匹配要求,所以很懒。符号是贪婪量词后面加上问号:*? +? ?? {num,num}?
回到前面的那个例子,如果正则换成了“勉强”的,"a+?",对于目标字符串还是"aaaaaab",那么这个正则就只会匹配一个a,因为"+"量词的意思是“一个或一个以上”,匹配一个a就能满足要求,它就不再去尝试了(不像贪婪的"a+"那样,不遗余力将所有符合要求的都匹配了)
 
“勉强”的量词在实际的使用中也是比较常见的,例如引号配对之类的问题,例如有这么一个字符串,"A says, 'bb'.",想要用正则将单引号里面的bb匹配出来,可以用正则'.*',这种情况下默认的贪婪量词也是能适用的,但如果这个字符串变成了 "A says, 'bb', and C says, 'ddddd'.",如果还是用'.*',由于贪婪,它匹配到的并不是你想要的'bb', and C says, 'ddddd',中间的.(点元字符)代表任何字符,当然也包括单引号,所以'.*'会将两个单引号中间的所有内容都匹配,不管是不是含有单引号。显然这不是我们想要的结果。如果用“勉强”的量词,'.*?',加一个问号,那么它就会尽最小的能力去完成,只要下一个字符是单引号,.*?就停止尝试,结果它就只会匹配'bb',不会将后面的, and ....那些都纳进来。
 
侵占(possessive)

这个用得很少,侵占量词在很多语言的正则中都没有被支持,它的符号是在贪婪量词后面加上一个加号:*+ ++ ?+ {num,num}+
侵占量词有个特点,它前面的子表达式作为一个整体,不记录回溯点(关于回溯方面的问题,建议你去看一看《精通正则表达式》这本书,讲得比较详细),通常是可以利用这个特点,对正则的匹配效率进行优化。其实它是和固化分组等价的,不支持侵占量词的语言中可以用固化分组的形式来代替。固化分组的写法:(?>)
有个例子可以帮助理解侵占量词的特点,但这个例子并不实用:
对于字符串"abbbbbbc",如果用普通的(贪婪)正则"a.*c"来匹配的话,它可以匹配整个字符串,因为.*可以匹配任何数量的任何字符,它是可以匹配bbbbbbc的,但它会发现正则后面还有一个c需要匹配,否则不能匹配成功,所以.*就“退回”一个字符c,使得整个正则能够成功匹配(回溯),所以贪婪即使是“贪”,它还是会顾全大局,退回一些必要的字符。
换侵占量词就不同了,"a.*+c"是一个侵占式的,它并不能成功匹配,因为.*+太霸道,占着最后的那个c不肯放回(不回溯),所以a.*+前面就已经匹配了整个字符串abbbbbbc,a.*+正则后面的c发现没有字符可以匹配了,前面的又不肯吐出一个来,所以整个正则以匹配失败结束。(这个"a.*+c"可以用固化分组改写成:"a(?>.*)c")
 

星期二, 十一月 06, 2012

Android 2.3.3 以上通用Notification 生成 代码.使用NotificationCompat.Builder

//notification unique id 
//The IDs must be unique only within the namespace of your .apk's package nam
//这个ID只需要在你自己的app 空间里唯一即可,不是整个系统唯一
public void setupNotification(Context ctx)
    {
        Intent notificationIntent = new Intent(ctx, StartGateActivity.class);
        PendingIntent contentIntent = PendingIntent.getActivity(ctx,
                0, notificationIntent,
                0);

        NotificationManager nm = (NotificationManager) ctx
                .getSystemService(Context.NOTIFICATION_SERVICE);
        nm.cancel(100);
        Resources res = ctx.getResources();
        CharSequence text = res.getString(R.string.nf_text);
        CharSequence contentTitle = res.getString(R.string.nf_title);
        CharSequence ticket = res.getString(R.string.nf_ticket);
        NotificationCompat.Builder builder= new NotificationCompat.Builder(ctx);

        builder.setContentTitle(contentTitle)
        .setContentText(text)
        .setSmallIcon(R.drawable.ic_launcher)
        .setOngoing(true) //this flag notification record  can't be clear
        .setAutoCancel(true)
        .setWhen(System.currentTimeMillis())
        .setTicker(ticket)
        .setContentIntent(contentIntent);
        Notification nf = builder.build();       
        nm.notify(100, nf);
    }

星期一, 十一月 05, 2012

如何在app中3.0 API LEVEL 11后加入notification.

public void setupViews(Context ctx)
{
    Intent notificationIntent = new Intent(ctx, LifeDemo.class);
    PendingIntent contentIntent = PendingIntent.getActivity(ctx,
            0, notificationIntent,
            PendingIntent.FLAG_CANCEL_CURRENT);

    NotificationManager nm = (NotificationManager) ctx
            .getSystemService(Context.NOTIFICATION_SERVICE);

    Resources res = ctx.getResources();
    Notification.Builder builder = new Notification.Builder(ctx);

    builder.setContentIntent(contentIntent)
                .setSmallIcon(R.drawable.ic_launcher)
                .setLargeIcon(BitmapFactory.decodeResource(res, R.drawable.ic_launcher))
                .setTicker("Demo Show")
                .setWhen(System.currentTimeMillis())
                .setAutoCancel(true)
                .setContentTitle("Demo Show 1")
                .setContentText("Demo Show2");
    Notification n = builder.build();

    nm.notify(1, n);
}

星期四, 十一月 01, 2012

如何给手机刷CMW Mod

adb reboot bootloader
fastboot boot recovery-clockwork-touch-6.0.1.4-maguro.img
#永久刷新
fastboot flash recovery recovery-clockwork-touch-6.0.1.4-maguro.img
adb reboot

发布一个apk的准备工作.

准备发布一个Android app
1.keytool -genkey -v -keystore debug.keystore -alias wxkdebugkey -keyalg RSA-validity 14000
  跟随提示回答问题.
2.Jarsigner -verbose -keystore debug.keystore yourapk.apk wxkdebugkey
3.jarsigner -verify yourapk.apk
4.zipalign -v 4 yourapk.apk lastname.apk
5.start a avd
   tools\android.bat list avd
                Available Android Virtual Devices:
                    Name: AVD233
                    Path: d:\temp\.android\avd\AVD233.avd
                  Target: Android 2.3.3 (API level 10)
                     ABI: armeabi
                    Skin: WVGA800
                ---------
                    Name: avd411
                    Path: d:\temp\.android\avd\avd411.avd
                  Target: Android 4.1 (API level 16)
                     ABI: armeabi-v7a
                    Skin: WVGA800
                  Sdcard: 512M
                Snapshot: true   
   emulator -avd AVD233 启动
5.adb devices
adb -e install lastname.apk

Android 产生key

keytool -genkey -v -keystore debug.keystore -alias wxkdebugkey -keyalg RSA-validity 14000

JDK 7.0 Display MD5 key

JDK 1.7 默认现在显示 RSA 的签名字符串了,用选项-v可以显示出MD5的签名。

keytool -v -list -alias wxkdebugkey -keystore my-release-key.keystore

星期二, 十月 23, 2012

比如屏幕旋转90度,Configuration Changes

Configuration Changes

If the configuration of the device (as defined by the Resources.Configuration class) changes, then anything displaying a user interface will need to update to match that configuration. Because Activity is the primary mechanism for interacting with the user, it includes special support for handling configuration changes.

Unless you specify otherwise, a configuration change (such as a change in screen orientation, language, input devices, etc) will cause your current activity to be destroyed, going through the normal activity lifecycle process of onPause(), onStop(), and onDestroy() as appropriate. If the activity had been in the foreground or visible to the user, once onDestroy() is called in that instance then a new instance of the activity will be created, with whatever savedInstanceState the previous instance had generated from onSaveInstanceState(Bundle).

This is done because any application resource, including layout files, can change based on any configuration value. Thus the only safe way to handle a configuration change is to re-retrieve all resources, including layouts, drawables, and strings. Because activities must already know how to save their state and re-create themselves from that state, this is a convenient way to have an activity restart itself with a new configuration.

In some special cases, you may want to bypass restarting of your activity based on one or more types of configuration changes. This is done with the android:configChanges attribute in its manifest. For any types of configuration changes you say that you handle there, you will receive a call to your current activity's onConfigurationChanged(Configuration) method instead of being restarted. If a configuration change involves any that you do not handle, however, the activity will still be restarted and onConfigurationChanged(Configuration) will not be called.




Because of this, some developers decide to handle the configuration changes (like screen orientation changes) themselves.  This is done by adding the "configChanges" attribute to the Activity declaration in AndroidManifest.xml
?
1
2
3
4
5
6
7
<activity android:name=".SomeActivity" android:label="@string/app_name"
  android:configChanges="orientation">
 <intent-filter>
  <action android:name="android.intent.action.MAIN"/>
  <category android:name="android.intent.category.LAUNCHER"/>
 </intent-filter>
</activity>

The "configChanges" attribute tells Android Platform that, some of the config changes will be handled by the activity by themselves. Platform does not need to do any special handling for these.

Android Configuration change.

http://developer.android.com/guide/topics/resources/runtime-changes.html

In Android, a configuration change causes the current activity to go away and be recreated.
The application itself keeps on running, but it has the opportunity to change
how the activity is displayed in response to the configuration change.

use onSaveInstanceState(Bundle) to keep state or other runtime data.


root Jelly Bean 4.1.2

root JB 4.1.2

首先必须安装了Android sdk和对应手机的USB驱动。
 android sdk: http://developer.android.com/sdk/index.html
  需要一些工具
 
1.download cmw(ClockworkMod) recover 工具.

 http://www.clockworkmod.com/rommanager
查找对应的设备后面的文件。
Galaxy Nexus 就要下载:
http://download2.clockworkmod.com/recoveries/recovery-clockwork-touch-6.0.1.0-maguro.img

2. download superuser packagae
 http://androidsu.com/superuser/
 下载 Superuser-3.1.3-arm-signed.zip
 
3.连接手机USB,设置为debug模式
   用adb push  Superuser-3.1.3-arm-signed.zip /sdcard/Superuser-3.1.3-arm-
signed.zip
    传送到手机上

4.临时进入cmw recover 并安装superuser 包
 进入window 命令行状态 cmd,进入android sdk安装目录下的android-sdk-windows\
platform-tools目录.
 
  adb reboot bootloader
  fastboot boot recovery-clockwork-touch-6.0.
1.0-maguro.img
 等待3秒出现,安装CMW 安装界面,如果是touch版本,则用手势滚动屏幕,选择安装
  select install zip from sdcard.
  否则用音量上下键移动,电源键确认来选择。
 
 安装完成后,用adb reboot 启动手机即可。

星期四, 九月 20, 2012

Fwd: OSWORKFLOW on JBOSS 4.22 Config.

1.download osworkflow 2.8,2006年以后已不再维护。
   http://java.net/downloads/osworkflow/

2.将osworkflow-2.8.0-example.war展开到jboss 422/server/default/deploy/osworkflow-2.8.0-example.war 目录下。

3.在MYSQL 上建立一个osworkflow的database,并运行osworkflow-2.8.0\src\etc\deployment\jdbc
 \mysql.sql,建立其需要使用的表格.
4.配置JBOSS 数据源
   在
jboss 422/server/default/deploy/mysql-ds.xml里加入数据源:
   <!-- add for osworkflow datasorce -->
  <local-tx-datasource>
    <jndi-name>osworkflow</jndi-name>
   <connection-url>jdbc:mysql://192.1.1.100:3306/osworkflow?autoReconnect=true&amp;useUnicode=TRUE&amp;characterEncoding=utf-8</connection-url>
    <driver-class>com.mysql.jdbc.Driver</driver-class>
    <user-name>root</user-name>
    <password></password>
    <exception-sorter-class-name>org.jboss.resource.adapter.jdbc.vendor.MySQLExceptionSorter</exception-sorter-class-name>
        <min-pool-size>5</min-pool-size>
        <max-pool-size>10</max-pool-size>
        <idle-timeout-minutes>1</idle-timeout-minutes>
    <metadata>
       <type-mapping>mySQL</type-mapping>
    </metadata>
  </local-tx-datasource>


5.进入
jboss 422/server/default/deploy/osworkflow-2.8.0-example.war/WEB-INF/classes下修改配置,加入数据源说明.
  注意Linux table name 有大小写的问题。

  osuser.xml:
<opensymphony-user>
   <provider class="com.opensymphony.user.provider.jdbc.JDBCAccessProvider">
  <property name="user.table">OS_USER</property>
  <property name="group.table">OS_GROUP</property>
  <property name="membership.table">OS_MEMBERSHIP</property>
  <property name="user.name" >username</property>
  <property name="user.password">passwordhash</property>
  <property name="group.name">groupname</property>
  <property name="membership.userName" >username</property>
  <property name="membership.groupName">groupname</property>
  <property name="datasource">java:/osworkflow</property>
</provider>

<provider class="com.opensymphony.user.provider.jdbc.JDBCCredentialsProvider">
 <property name="user.table">OS_USER</property>
  <property name="group.table">OS_GROUP</property>
  <property name="membership.table">OS_MEMBERSHIP</property>
<property name="user.name" >username</property>
  <property name="user.password">passwordhash</property>
  <property name="group.name">groupname</property>
  <property name="membership.userName" >username</property>
  <property name="membership.groupName">groupname</property>
  <property name="datasource">java:/osworkflow</property>
 </provider>

    <provider class="com.opensymphony.user.provider.jdbc.JDBCProfileProvider">
 <property name="user.table">OS_USER</property>
  <property name="group.table">OS_GROUP</property>
  <property name="membership.table">OS_MEMBERSHIP</property>
<property name="user.name" >username</property>
  <property name="user.password">passwordhash</property>
  <property name="group.name">groupname</property>
  <property name="membership.userName" >username</property>
  <property name="membership.groupName">groupname</property>
  <property name="datasource">java:/osworkflow</property>
 </provider>

 <authenticator class="com.opensymphony.user.authenticator.SmartAuthenticator" />

</opensymphony-user>

osworkflow.xml:
<osworkflow>
    <!--persistence class="com.opensymphony.workflow.spi.memory.MemoryWorkflowStore"/-->
    <persistence class="com.opensymphony.workflow.spi.jdbc.MySQLWorkflowStore">
 <property key="datasource" value="java:/osworkflow"/>
<property key="entry.sequence"
                      value="SELECT max(id)+1 FROM OS_WFENTRY"/>
 <property key="entry.table" value="OS_WFENTRY"/>
 <property key="entry.id" value="ID"/>
 <property key="entry.name" value="NAME"/>
 <property key="entry.state" value="STATE"/>
 <property key="step.sequence"  value="SELECT max(ID)+1 FROM OS_STEPIDS"/>

 <property key="step.sequence.increment"    value="INSERT INTO OS_STEPIDS (ID) values (null)"/>
 <property key="step.sequence.retrieve"   value="SELECT max(ID) FROM OS_STEPIDS"/>
 <property key="entry.sequence.increment" value="INSERT INTO OS_ENTRYIDS (ID) values (null)"/>
 <property key="entry.sequence.retrieve" value="SELECT max(ID) FROM OS_ENTRYIDS"/>

 <property key="history.table" value="OS_HISTORYSTEP"/>
 <property key="current.table" value="OS_CURRENTSTEP"/>
 <property key="historyPrev.table" value="OS_HISTORYSTEP_PREV"/>
 <property key="currentPrev.table" value="OS_CURRENTSTEP_PREV"/>
 <property key="step.id" value="ID"/>
 <property key="step.entryId" value="ENTRY_ID"/>
 <property key="step.stepId" value="STEP_ID"/>
 <property key="step.actionId" value="ACTION_ID"/>
 <property key="step.owner" value="OWNER"/>
 <property key="step.caller" value="CALLER"/>
 <property key="step.startDate" value="START_DATE"/>
 <property key="step.finishDate" value="FINISH_DATE"/>
 <property key="step.dueDate" value="DUE_DATE"/>
 <property key="step.status" value="STATUS"/>
 <property key="step.previousId" value="PREVIOUS_ID"/>
</persistence>
    <factory class="com.opensymphony.workflow.loader.XMLWorkflowFactory">
        <property key="resource" value="workflows.xml" />
    </factory>
</osworkflow>

 propertyset.xml:
<propertysets>
 <propertyset name="jdbc" class="com.opensymphony.module.propertyset.database.JDBCPropertySet">
  <arg name="table.name" value="OS_PROPERTYENTRY"/>
  <arg name="col.globalKey" value="GLOBAL_KEY"/>
  <arg name="col.itemKey" value="ITEM_KEY"/>
  <arg name="col.itemType" value="ITEM_TYPE"/>
  <arg name="col.string" value="STRING_VALUE"/>
  <arg name="col.date" value="DATE_VALUE"/>
  <arg name="col.data" value="DATA_VALUE"/>
  <arg name="col.float" value="FLOAT_VALUE"/>
  <arg name="col.number" value="NUMBER_VALUE"/>
  <arg name="datasource" value="java:/osworkflow"/>
  <arg name="" value=""/>
 </propertyset>
</propertysets>


至此配置完成,启动JBOSS,从浏览器输入:
http://192.168.1.1:8080/osworkflow-2.8.0-example/

即可查看.



星期二, 九月 18, 2012

Android如何确认一个Intent是否存在呢?


 /**
     * Indicates whether the specified action can be used as an intent. This
     * method queries the package manager for installed packages that can
     * respond to an intent with the specified action. If no suitable package is
     * found, this method returns false.
     *
     * @param context The application's environment.
     * @param action The Intent action to check for availability.
     *
     * @return True if an Intent with the specified action can be sent and
     * responded to, false otherwise.
     *  Example:isIntentAvailable(getApplicationContext(),Intent.ACTION_WEB_SEARCH)
     */
    public static boolean isIntentAvailable(Context context, String action) {
    final PackageManager packageManager = context.getPackageManager();
    final Intent intent = new Intent(action);
    List<ResolveInfo> list =
    packageManager.queryIntentActivities(intent,
    PackageManager.MATCH_DEFAULT_ONLY);
    return list.size() > 0;
    }

星期四, 九月 13, 2012

Android动态改变语言并自动刷新菜单.

Locale locale=null;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        String languageToLoad  = "zh";
         locale = new Locale(languageToLoad);
        Locale.setDefault(locale);
        Configuration config = new Configuration();
        config.locale = locale;
        getBaseContext().getResources().updateConfiguration(config, null);
        //getBaseContext().getResources().getDisplayMetrics()
        Log.v("log:","local is changed!");
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        setContentView(R.layout.activity_main);
         }

星期二, 九月 11, 2012

Andorid xml is binary format in .apk package

All of the resource XML files are compiled to a binary format by aapt at build time.
只有一个例外就是 res/raw下的文件是原始文件,没有做任何处理。

Activity的getString和Activity.getResource().getString的区别是什么呢?

activity的getString下的说明如此:
 localized string from the application's package's default string table

Resource的getString说明如下:
Returns the string value associated with a particular resource ID. It will be stripped of any styled text information

但是查看源代码,发现activity的getString如下:
    public final String getString(int resId) {
        return getResources().getString(resId);
    }

其实是一回事。真不明白为什么说明差异如此之大。

Android classpath access external resource.

Resources.getSystem().getString(android.R.string.somecommonstuff)

星期一, 八月 27, 2012

如何禁止wxWidgets 2.9.x Debug信息

wxWidgets 2.9.1 debugging features are always available by default
在wx/debug.h 里设置
wxDEBUG_LEVEL=0

然后从新编译所有的库文件,再编译应用代码。

REM 清除所有的库文件
mingw32-make -f makefile.gcc  SHARED=0 UNICODE=1 BUILD=release MONOLITHIC=0  USE_STC=0 USE_PROPGRID=1 USE_GUI=1 clean

REM 重新编译所有库
mingw32-make -f makefile.gcc  SHARED=0 UNICODE=1 BUILD=release MONOLITHIC=0  USE_STC=0 USE_PROPGRID=1 USE_GUI=1

禁止 wxWidgets 2.9 调试信息弹出

wxWidgets 2.9.1 debugging features are always available by default

定义wxDEBUG_LEVEL=0 在 IMPLEMENT_APP()  代码前。



星期五, 八月 10, 2012

Dynamic load css definition file.

var CSS = {

        load: /*static*/ function (url_, /*optional*/ media_) {

                // We are preventing loading a file already loaded
                var _links = document.getElementsByTagName("link");
                if (_links.length > 0 && _links["href"] == url_) return;

                // Optional parameters check
                var _media = media_ === undefined || media_ === null ? "all" : media_;
               
                var _elstyle = document.createElement("link");
                _elstyle.setAttribute("rel", "stylesheet");
                _elstyle.setAttribute("type", "text/css");
                _elstyle.setAttribute("media", _media);
                _elstyle.setAttribute("href", url_);

                var _head = document.getElementsByTagName("head")[0];
                _head.element.appendChild(_elstyle);

        }

};