星期三, 十一月 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);

        }

};

星期三, 七月 25, 2012

如何用SVG显示汉字。只需要注意HTML需要用UTF-8格式保存即可.

<html> 
<body> 
<p><canvas id="canvas" style="border:2px solid black;" width="200" height="200"></canvas> 
<script> 
var canvas = document.getElementById("canvas"); 
var ctx = canvas.getContext("2d"); 
var data = "<svg xmlns='http://www.w3.org/2000/svg' width='200' height='200'>" + 
             "<foreignObject width='100%' height='100%'>" + 
               "<div xmlns='http://www.w3.org/1999/xhtml' style='font-size:40px;color:red'>" + 
                 "<em>I</em> like <span style='color:white; text-shadow:0 0 2px blue;'>网络的上面</span>" + 
               "</div>" + 
             "</foreignObject>" + 
           "</svg>"; 
var svg = new (self.BlobBuilder || self.MozBlobBuilder || self.WebKitBlobBuilder); 
var DOMURL = self.URL || self.webkitURL || self; 
var img = new Image(); 
svg.append(data); 
var url = DOMURL.createObjectURL(svg.getBlob("image/svg+xml;charset=utf-8")); 
img.onload = function() { 
    ctx.drawImage(img, 0, 0); 
    DOMURL.revokeObjectURL(url); 
}; 
img.src = url; 
</script> 
</body> 
</html>

星期一, 七月 16, 2012

tcpdump debug http traffic and dns query.

tcpdump -vvv -s 0 -l port 53

windump -i \Device\NPF_{CDF6DEBB-8C8E-49EF-91D5-CF6A69F1FE81} -n port 80 or port 443 or port 53

adb shell tcpdump -i  wlan0 -n port 80 or port 443 or port 53

星期五, 六月 08, 2012

Android memory usage

2.名词解释
App:Application
VSS - Virtual Set Size 虚拟耗用内存(包含共享库占用的内存)
RSS - Resident Set Size 实际使用物理内存(包含共享库占用的内存)
PSS - Proportional Set Size 实际使用的物理内存(比例分配共享库占用的内存)
USS - Unique Set Size 进程独自占用的物理内存(不包含共享库占用的内存)

tcpdump syntax

http://danielmiessler.com/study/tcpdump/

星期二, 四月 03, 2012

POI 3.7如何实现cell的填充颜色。

类似Excel 右上角填充颜色的效果。
必须在setCellValue之后,再调用setCellStyle的语句。
顺序不能错误,否则效果不如意。
        style = wb.createCellStyle();
        style.setFillForegroundColor(HSSFColor.YELLOW.index);
        style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
        cell = row.createCell((short) 2);
        cell.setCellValue("X");
        cell.setCellStyle(style);

如何在JSP里实现Streaming content,流失内容,让内容逐渐显示

<%@page buffer="none"%>
2.在out.println一些之后使用out.flush().

这样在浏览器内就显示出来效果了,逐渐显示,而不是全部一下显示了。


星期二, 三月 06, 2012

如何制作Android显示百分比数字的状态条。

main.xml

<?xml version="1.0" encoding="utf-8"?>
<!--  -->
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="30dip"
android:padding="0dip"
>
<ProgressBar android:id="@+id/progress"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="fill_parent"
android:indeterminateOnly="false"
android:layout_height="30dip"  />
<TextView
android:id="@+id/perc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:textColor="#ff0000"
android:textStyle="bold"
/>
</RelativeLayout>

代码:
package wxk.com;

import android.app.Activity;
import android.os.Bundle;
import android.os.Message;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
public class ThreadDemoActivity extends Activity {
    ProgressBar bar;
    TextView percent;
    Handler handler=new Handler() {
    @Override
    public void handleMessage(Message msg) {
    bar.incrementProgressBy(5);
    percent.setText(String.format("%03d%%", bar.getProgress()));
    }
    };
    boolean isRunning=false;
    @Override
    public void onCreate(Bundle icicle) {
    super.onCreate(icicle);
    setContentView(R.layout.main);
    bar=(ProgressBar)findViewById(R.id.progress);
    percent=(TextView)findViewById(R.id.perc);
    }
    public void onStart() {
    super.onStart();
    bar.setProgress(0);

    Thread background=new Thread(new Runnable() {
    public void run() {
    try {
    for (int i=0;i<20 && isRunning;i++) {
    Thread.sleep(1000);
    handler.sendMessage(handler.obtainMessage());
    }
    }
    catch (Throwable t) {
    // just end the background thread
    }
    }
    });
    isRunning=true;
    background.start();
    }
    public void onStop() {
    super.onStop();
    isRunning=false;
    }
}

星期五, 二月 24, 2012

Keyboard Shortcuts for Bash ( Command Shell for Ubuntu, Debian, Suse, Redhat, Linux, etc)

Keyboard Shortcuts for Bash ( Command Shell for Ubuntu, Debian, Suse, Redhat, Linux, etc)

The default shell on most Linux operating systems is called Bash. There are a couple of important hotkeys that you should get familiar with if you plan to spend a lot of time at the command line. These shortcuts will save you a ton of time if you learn them. 

 



Ctrl + A Go to the beginning of the line you are currently typing on
Ctrl + E Go to the end of the line you are currently typing on
Ctrl + L               Clears the Screen, similar to the clear command
Ctrl + U Clears the line before the cursor position. If you are at the end of the line, clears the entire line.
Ctrl + H Same as backspace
Ctrl + R Let's you search through previously used commands
Ctrl + C Kill whatever you are running
Ctrl + D Exit the current shell
Ctrl + Z Puts whatever you are running into a suspended background process. fg restores it.
Ctrl + W Delete the word before the cursor
Ctrl + K Clear the line after the cursor
Ctrl + T Swap the last two characters before the cursor
Esc + T Swap the last two words before the cursor
Alt + F Move cursor forward one word on the current line
Alt + B Move cursor backward one word on the current line
Tab Auto-complete files and folder names