Recent Notes
Displaying keyword search results 71 - 80
Created by freyo on May 23, 2011 14:30:18
Last update: May 23, 2011 14:31:08
There are two distinct ways to process XPath: with namespace and without namespace. The code is different depending on whether the parser is namespace aware.
Code without namespace:
import java.io.*;
import javax.xml.parsers.*;
...
code with namespace:
import java.io.*;
import java.util.Iterator;
...
XML without namespace:
<?xml version="1.0" encoding="UTF-8" standalone="n...
XML with namespace:
<?xml version="1.0" encoding="UTF-8" standalone="n...
The same XPath expression works for both XML files when the parser is not namespace aware.
When the parser is namespace aware, you have to adjust the XPath accordingly depending on whether the XML has namespace declarations: " /test-license/licensee/name/text() " works for the XML file without namespace, while " /p:test-license/p:licensee/p:name/text() " works for the XML file with namespace.
Created by freyo on May 20, 2011 09:25:20
Last update: May 23, 2011 12:11:42
The javax.xml.crypto and javax.xml.crypto.dsig packages are not available in Android (as of version 2.3). Therefore, standard Java API does not work. But you can use the Apache Santuario library to do that. Here are the steps:
Download the xml security source distribution (curently version 1.4.4).
Build with ant.
Create your own library jar (only the apache classes, no javax):
jar -cf xmlsec-1.4.4.jar -C build/classes org
Copy xmlsec-1.4.4.jar to the libs directory of your Android project.
Here's the Java code:
import java.io.*;
import javax.xml.parsers.*;
...
Created by freyo on May 17, 2011 11:13:17
Last update: May 17, 2011 11:13:17
This is an odd-ball content provider in that it doesn't provide database records, but provides a resource as a stream. It can be used to provide media files or XML resources. Start the project with:
tools/android create project --package com.android... Create assets directory and add an XML file ( assets/demo.xml ): <? xml version="1.0" encoding="UTF-8"?> <people... Edit the layout ( res/layout/main.xml ): <?xml version="1.0" encoding="utf-8"?> <LinearL... Edit src/com/android/cptest/Dummy.java : package com.android.cptest; import java.io.... Add content provider ( src/com/android/cptest/XmlResource.java ): package com.android.cptest; import java.io.... Update AndroidManifest.xml : <?xml version="1.0" encoding="utf-8"?> <manifes... Add this section to the end of build.xml : <target name="-package-resources"> <ech... Build and install: ant install Screenshot: Remove the Dummy activity ( AndroidManifest.xml ): <?xml version="1.0" encoding="utf-8"?> <manifes... Create a new project for...
Created by freyo on May 09, 2011 14:15:01
Last update: May 09, 2011 14:17:22
In short, use the PackageManager class to get the PackageInfo :
PackageInfo pkgInfo = getPackageManager().getP...
To build an APK, make these changes to the greetings app :
Change the layout to ( res/layout/main.xml ):
<?xml version="1.0" encoding="utf-8"?>
<LinearL...
Change the string values ( res/values/strings.xml ):
<?xml version="1.0" encoding="utf-8"?>
<resourc...
Update AndroidManifest.xml :
<?xml version="1.0" encoding="utf-8"?>
<manifes...
Create a new Java class src/com/android/appinfo/GetAppVersion.java :
package com.android.appinfo;
import android...
Edit build.xml , change project name to "AppInfo":
<?xml version="1.0" encoding="UTF-8"?>
<project...
Install to emulator:
ant install
Screenshot:
Created by freyo on May 05, 2011 09:33:55
Last update: May 05, 2011 09:34:46
For Android, there's no entry function like " public static void main() " in Java. The main activity is declared by intent-filter in AndroidManifest.xml :
<intent-filter>
<action android:name="andro...
The category LAUNCHER declaration puts it in the Launcher . The action MAIN means this is the activity to start when the application icon is clicked in the Launcher. In fact, the Android ActivityManager starts the main activity with the intent:
{ act=android.intent.action.MAIN cat=[android.inte...
Created by freyo on April 12, 2011 15:13:32
Last update: May 03, 2011 15:20:55
Create new project
$ tools/android create project \ > --package co... Edit res/values/string.xml , add strings for the main screen. <?xml version="1.0" encoding="utf-8"?> <resourc... Add widgets to main layout ( res/layout/main.xml ). <?xml version="1.0" encoding="utf-8"?> <LinearL... Edit src/com/android/apkinfo/GetAPKPermissions.java and change contents to: package com.android.apkinfo; import android... Install the debug package to the emulator: ant install The screen should look like: Add display permissions activity. Start by creating a new layout res/layout/display_permissions.xml : <?xml version="1.0" encoding="utf-8"?> <LinearL... Add the strings used by the new Activity ( res/values/strings.xml ): <?xml version="1.0" encoding="utf-8"?> <resourc... Add the Display Permissions Activity ( src/com/android/apkinfo/DisplayPermissions.java ): package com.android.apkinfo; import android... Change src/com/android/apkinfo/GetAPKPermissions.java to invoke the Display Permissions Activity: package com.android.apkinfo; import android... Declare Display Permissions Activity in AndroidManifest.xml : <?xml version="1.0" encoding="utf-8"?> <manifes......
Created by Dr. Xi on April 20, 2011 21:44:15
Last update: May 02, 2011 20:56:58
The String.format() method provides versatile formatting capabilities. This tutorial tries to present these capabilities in a accessible manner. The format string A format string can contain zero, one, or more format specifiers . The general form of a format specifier is:
%[argument_index$] [flags] [width] [.precision]co... where things in square brackets are optional, and conversion is a character indicating the conversion to be applied to the corresponding variable value. The only required characters in the format specifier is the percent sign % and the conversion character. A simple example: public static void simpleFormat() { System.out... The Argument index The argument index is specified by a number, terminated by the dollar sign $ . The same argument may be repeated multiple times in a format string. Unindexed...
Created by Dr. Xi on April 28, 2011 11:37:23
Last update: April 28, 2011 11:37:23
The Future interface represents the result of an asynchronous computation. Future provides methods to check if the computation is complete, to wait for its completion, and to retrieve the result of the computation. You call one of the three submit methods of ExecutorService to get a Future object:
<T> Future<T> submit(Callable<T> task)
<T> Future<T> submit(Runnable task, T result)
Future<?> submit(Runnable task)
Use the first two to retrieve usable results from the computation. The third option returns a Future that returns null upon successful completion. It is used to simply wait for the task to complete, much like Thread.join() .
import java.util.concurrent.Future;
import java...
Created by Dr. Xi on April 28, 2011 11:06:03
Last update: April 28, 2011 11:06:03
The Java Executor interface replaces the call new Thread(new RunnableTask()).start() with executor.execute(new RunnableTask()) . The concurrent package provides built-in utility classes to manage threads so that you don't need to worry about them.
import java.util.concurrent.TimeUnit;
import ja...
Created by Dr. Xi on April 28, 2011 08:58:03
Last update: April 28, 2011 08:58:03
The java.util.concurrent.atomic package is a small toolkit of classes that support lock-free thread-safe programming on single variables .
This is an example of a shared counter without synchronization or lock:
import java.util.concurrent.atomic.AtomicInteger;
...