String str = “one,two,three,four,five”;
String arr[] = str.split(“,”);
System.out.println(“Array Size: ” + arr.length);
The ‘|’ character means OR in regex, so you have to split it like following:
line.split("\\|");
That’s it!
String str = “one,two,three,four,five”;
String arr[] = str.split(“,”);
System.out.println(“Array Size: ” + arr.length);
The ‘|’ character means OR in regex, so you have to split it like following:
line.split("\\|");
That’s it!
//Usage from activity:
//String verison = GlobalSettings.getVersionName(this,MyActivity.class)
public static String getVersionName(Context context, Class cls) {
try {
ComponentName comp = new ComponentName(context, cls);
PackageInfo pinfo = context.getPackageManager().getPackageInfo(comp.getPackageName(), 0);
return "Version: " + pinfo.versionName;
} catch (android.content.pm.PackageManager.NameNotFoundException e) {
return null;
}
}
To start Services automatically after the Android system starts you can register a BroadcastReceiver to the Android android.intent.action.BOOT_COMPLETED system event.
In the onReceive() method the corresponding BroadcastReceiver would then start the service.
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
public class MyReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Intent service = new Intent(context, WordService.class);
context.startService(service);
}
}
If you application is installed on the SD card, then it is not available after the android.intent.action.BOOT_COMPLETED event. Register yourself in this case for the android.intent.action.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE event.
Also note that as of Android 3.0 the user needs to have started the application at least once before your application can receive android.intent.action.BOOT_COMPLETED events.
You can also telnet to connect to the device. This allows you to simulate certain things, e.g. incoming call, change the network “stability”, set your current geocodes, etc. Use “telnet localhost 5554″ to connect to your simulated device. To exit the console session, use the command “quit” or “exit”.
For example to change the power settings of your phone, to receive an sms and to get an incoming call make the following.
# connects to device telnet localhost 5554 # set the power level power status full power status charging # make a call to the device gsm call 012041293123 # send a sms to the device sms send 12345 Will be home soon # set the geo location geo fix 48 51
private String android_id = Secure.getString(getContext().getContentResolver(),
Secure.ANDROID_ID);
Network connection fail frequently especially for mobile clients. For example if you switch from Wifi to 3G then an existing network connection will break and you need to retry the request.
The Apache HttpClient has an default DefaultHttpRequestRetryHandler object registered which will per default 3 times retry a failed connection. The problem is that switching from one network to another make take a little while and DefaultHttpRequestRetryHandler will retry immediately.
To work around this issue you can implement your own version of DefaultHttpRequestRetryHandler in which you wait a pre-defined time.
For example the implementation could look like:
AbstractHttpClient client = (AbstractHttpClient) new DefaultHttpClient();
DefaultHttpRequestRetryHandler retryHandler = new DefaultHttpRequestRetryHandler() {
@Override
public boolean retryRequest(IOException exception, int executionCount, HttpContext context) {
if (!super.retryRequest(exception, executionCount, context)) {
return false;
}
// Retry but wait a bit
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
return true;
}
};
client.setHttpRequestRetryHandler(retryHandler);
You can uninstall an android application via the shell. Switch the the data/app directory (cd /data/app) and simply delete your android application.
You can also uninstall an app via adb with the package name.
adb uninstall
You can access your Android emulator also via the console. Open a shell, switch to your “android-sdk” installation directory into the folder “tools”. Start the shell via the following command “adb shell”.
adb shell
You can also copy a file from and to your device via the following commands.
// Assume the gesture file exists on your Android device
adb pull /sdcard/gestures ~/myfile
// Now copy it back
adb push ~/myfile/gesture /sdcard/gestures2
This will connect you to your device and give you Linux command line access to the underlying file system, e.g. ls, rm, mkdir, etc. The application data is stored in the directory “/data/data/package_of_my_app”.
If you have several devices running you can issue commands to one individual device.
# Lists all devices
adb devices
#Result
List of devices attached
emulator-5554 attached
emulator-5555 attached
# Issue a command to a specific device
adb -s emulator-5554 shell
If you select a UI element you can change its properties via the properties view. Most of the properties can be changed via the right mouse menu. You can also edit properties of fields directy in XML. Typically you change properties directly in the XML file as this is much faster. But the right mouse functionality is nice if you are searching for a certain property.
For example

