Wednesday, May 30, 2012

Convert bytes array to string

String str = new String(byte[] byteArray);

Full explanation to build apk and install it on android device using command line

1. make sure JDK is installed instead of JRE, and JAVA_HOME variable is pointing to JDK installation path

2. make sure you have ant and android sdks installed, and there is android.bat inside android sdks

3. go to the android project directory

4. under this directory, use command: /path/to/android.bat/android.bat update project -p /path/to/project folder/       =======> this will create build.xml for building apk file

5. use this command: /path/to/ant directory/bin/ant debug      ========> this will generate debug version apk

6. now you have an apk file ready for installation, use this command first: adb devices to test if a device is connected

7. after making sure you have a valid device connected, use this command: adb -d install path/to/your/app.apk     ========> this will install the apk to your android device

8. after installing the app, we want to open the app, use this command: adb shell am start -n com.zhoubo/.AppName  note: com.zhoubo represents the app source   ==========> this will open the app named AppName from source com.zhoubo

Tuesday, May 29, 2012

How Java Uses Threads


Java applications and applets are naturally threaded. The runtime environment starts execution of the program with the main() method in one thread. Garbage collection takes place in another thread. Screen updating occurs in a third thread. There may be other threads running as well, mostly related to the behavior of the virtual machine. All of this happens invisibly to the programmer. Some of the time you're only concerned with what happens in the primary thread which includes the main() method of a program. If this is the case you may not need to worry about threading at all.

Sometimes, however, you need to add your own threads to an applet or application. The simplest reason for adding a separate thread is to perform a long calculation. For instance if you're trying to find the ten millionth prime number, you probably don't want to make users twiddle their thumbs while you search. Or you may be waiting for a resource that isn't available yet, a large graphic to download from the Internet, for example. Once again you shouldn't make the user wait while your program waits. Any operation that is going to take a noticeable period of time should be placed in its own thread.

The other reason to use threading is to more evenly divide the computer's power among different tasks. If you want to draw random rectangles on the display, you would still like the applet to respond to user input. If all the CPU time is spent drawing rectangles, there's nothing left over for the user. On a preemptively multitasking operating system like Solaris or Windows NT, the user may at least be able to kill the application. On a cooperatively multitasking operating system like MacOS 9 or Windows 9x, the user may have to reboot their machine. This is a bad thing. With threads you can set the priority of different processes, so that user input receives a high priority and drawing pretty pictures receives a low priority. Then the user can stop the applet without flipping the power switch on their machine.

From: http://www.cafeaulait.org/course/week11/04.html

Friday, May 25, 2012

Use ant to build android project

if the directory is different from where ant is in, use ./path to ant/ant [debug]



$ cd android-project-directory
$ android update project --path  This creates build.xml, default.properties, and local.properties for your project
Then, follow this link to use ant command to generate apk file



./android.bat update project -p SpeechRecV6/
/cygdrive/d/Android/apache-ant-1.8.4/bin/ant debug




Thursday, May 24, 2012

Use command line to install apk


adb -d install path/to/your/app.apk
in my case, I copied the apk file to the same folder as adb exists, and use: ./adb -d install -r SpeechRecV4.apk

tomorrow I will try to use command line to do the build and compile work

After entering: ./adb shell ==============> I entered shell environment, and I tested am start -a android.intent.action.MAIN -n com.android.settings/.Settings  and this worked and opened the setting page
Then I tried to open my own SpeechRecV4 app using am start -n com.zhoubo/.SpeechRecV4Activity this also worked

Tuesday, May 22, 2012

Java network byte order issue

Java uses a DataOutputStream which has a method writeShort() (and writeIntwriteLong, etc.) which automatically write in network byte order ( the high-order byte first, followed by the next highest-order byte and so on, with the low order byte sent last).


// Input and output streams for TCP socket
protected DataInputStream in;protected DataOutputStream out;

protected Socket connect (int port) throws IOException
{
// Connect method
output.appendText ("\nConnecting to " + server + " Port " + port + "\n");
Socket socket = 
new Socket (server, port);
OutputStream rawOut = socket.getOutputStream ();
InputStream rawIn = socket.getInputStream ();
BufferedOutputStream buffOut = new BufferedOutputStream (rawOut);
out = 
new DataOutputStream (buffOut);
in = 
new DataInputStream (rawIn);return socket;
// END connect

Sunday, May 20, 2012

价格管制不会解决短缺问题

这个论点是从《经济解释》里面看来的。说的是在被管制下的价格如果低于市价,需求方的边际价值高于价格,但是供应有限,就要付出金钱价格之外的代价来补充。这些补充的代价可能是排队,论资排辈,武力解决,政治手法,人际关系等等。只要知道了补充的代价,加上原本的金钱代价就会等于边际价值。

所以《经济解释》霸气的断言,“短缺”只是一些三流经济学家的思想短缺。