Recent Notes
Displaying keyword search results 81 - 90
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;
...
Created by Dr. Xi on April 27, 2011 15:58:31
Last update: April 27, 2011 20:05:22
From JavaDoc : java.util.concurrent.locks.ReentrantLock is a reentrant mutual exclusion Lock with the same basic behavior and semantics as the implicit monitor lock accessed using synchronized methods and statements, but with extended capabilities. The extended capabilities are: tryLock() : acquire the lock if possible, otherwise immediately return false. tryLock(long timeout, TimeUnit unit) : acquire the lock within the timeout, return false after timeout. lockInterruptibly() : try to acquire the lock and enter wait state, but can be interrupted by other threads and exit waiting. The following shows how java.util.concurrent.locks.ReentrantLock can be used. Start two threads to manipulate the same counter, one incrementing, the other decrementing. Since the counter is incremented and decremented the same number of times, in the end, it should be 0. Without locking,...
Created by jinx on April 26, 2011 11:58:33
Last update: April 26, 2011 12:00:40
The PHP function array_unique removes duplicate values from arrays. It takes an optional parameter $sort_flags , which can be one of:
SORT_REGULAR : compare items normally (don't change types)
SORT_NUMERIC : compare items numerically
SORT_STRING : compare items as strings
SORT_LOCALE_STRING : compare items as strings, based on the current locale.
Example:
<?php
$a = array(
'1' => 'Apple',
...
Output:
array_unique results:
array(3) {
[1]=>
...
Created by freyo on April 21, 2011 14:27:17
Last update: April 21, 2011 14:28:00
The class android.content.pm.PackageManager offers two methods to compare signatures between two packages: checkSignatures (int uid1, int uid2) and checkSignatures (String pkg1, String pkg2) . The first takes the UIDs of the two packages, the second takes the names of the packages. If all signatures match, the return valus is PackageManager.SIGNATURE_MATCH , otherwise PackageManager.SIGNATURE_NO_MATCH , or PackageManager.SIGNATURE_UNKNOWN_PACKAGE .
Sample code:
int sigMatch = getPackageManager().checkSignatures...
Created by freyo on April 18, 2011 15:08:21
Last update: April 18, 2011 15:12:20
Generate android project
$ ~/android-sdk-linux_86/tools/android create proj...
Create XML file res/xml/books.xml :
<?xml version="1.0"?>
<catalog>
<book id=...
Edit layout ( res/layout/main.xml ):
<?xml version="1.0" encoding="utf-8"?>
<Lin...
Edit code ( src/com/android/xmlres/XMLResource.java ):
package com.android.xmlres;
import java.io....
Change activity label from app_name to booklist ( AndroidManifest.xml ):
<?xml version="1.0" encoding="utf-8"?>
<manifes...
Add value for string resource ( res/values/string.xml ):
<?xml version="1.0" encoding="utf-8"?>
<resourc...
Deploy and test:
ant install
Created by freyo on April 18, 2011 14:14:29
Last update: April 18, 2011 14:15:48
For XML resources, Android returns an XmlPullParser reference. I tried the RSSReader example from xmlpull.org , it failed both on the Android emulator and with xpp3-1.1.4c .
This is an example for XmlPullParser usage. Since the XmlPullParser is event based, a callback mechanism works better.
Java code:
import java.io.*;
import java.net.*;
import ...
The XML file used:
<?xml version="1.0" encoding="UTF-8"?>
<catalog...
Created by Dr. Xi on April 18, 2011 12:10:37
Last update: April 18, 2011 12:14:24
Python defines a long list of special methods to customize class behavior. This is a short list for the Basic Customizations . Method Description object.__new__(cls[, ...]) Called to create a new instance of class cls. __new__() is a static method (special-cased so you need not declare it as such) that takes the class of which an instance was requested as its first argument. __new__() is intended mainly to allow subclasses of immutable types (like int, str, or tuple) to customize instance creation. It is also commonly overridden in custom metaclasses in order to customize class creation. object.__init__(self[,...]) Called when the instance is created. The arguments are those passed to the class constructor expression. If a base class has an __init__() method, the derived class’s __init__()...
Created by jinx on April 16, 2011 13:08:50
Last update: April 16, 2011 13:09:13
There are two functions:
int strcasecmp (string $str1 , string $str2) : binary safe case-insensitive string comparison.
int strnatcasecmp (string $str1 , string $str2) : case insensitive string comparisons using a "natural order" algorithm (a comparison algorithm that orders alphanumeric strings in the way a human being would).
In both cases the function returns:
< 0 if str1 is less than str2
= 0 if str1 is equal to str2
> 0 if str1 is greater than str2
Created by freyo on April 15, 2011 09:08:28
Last update: April 15, 2011 09:08:28
Follow three steps to read a file from Android internal storage:
Call openFileInput() and pass it the name of the file to read. This returns a java.io.FileInputStream .
Read bytes from the file with read() .
Then close the stream with close() .
Sample code:
FileInputStream in = openFileInput("test.txt");
...
Created by freyo on April 15, 2011 09:00:54
Last update: April 15, 2011 09:00:54
Sample code for writing to a file in the internal storage. There are three steps:
Open the file with Context.openFileOutput , which returns java.io.FileOutputStream .
Write to the file.
Close the file.
import java.io.*;
import android.content.Contex...
The second parameter to openFileOutput is the operating mode. Available values are:
Context.MODE_PRIVATE
Context.MODE_APPEND
Context.MODE_WORLD_READABLE
Context.MODE_WORLD_WRITEABLE
The file is saved in /data/data/<package_name>/files .