Finding app heap availability

VMRuntime was a great class provided in Android SDK to find and alter information about the VM used by your application.

http://developer.android.com/sdk/api_diff/5/changes/dalvik.system.VMRuntime.html

However, this has been deprecated since version 5 mentioning this was only intended for Internal usage and not for public usage. That;s sad since we cannot find useful functions elsewhere.

If you do want to find memory consumption of the system, you can use:

ActivityManager actvityManager = (ActivityManager) this.getSystemService( ACTIVITY_SERVICE );
ActivityManager.MemoryInfo mInfo = new ActivityManager.MemoryInfo ();
actvityManager.getMemoryInfo( mInfo );
Log.i(TAG, " minfo.availMem " + mInfo.availMem);
Log.i(TAG, " minfo.lowMemory " + mInfo.lowMemory);
Log.i(TAG, " minfo.threshold " + mInfo.threshold);

If you are looking for information for your app, you can use the java provided Runtime methods:

Log.i("AvailableHeapMemory", " Runtime.getRuntime().freeMemory() "
        + Runtime.getRuntime().freeMemory());
Log.i("AvailableHeapMemory", " Runtime.getRuntime().maxMemory() "
        + Runtime.getRuntime().maxMemory());
Log.i("AvailableHeapMemory", " Runtime.getRuntime().totalMemory() "
        + Runtime.getRuntime().totalMemory());

Adjusting webview contents to fit

I assumed that the webview controller would automatically fit the html contents horizontally and would allow only vertical scrolling. But that doesn’t seem to be the way. You can ofcourse set the below values to make it horizontally fit: (available from 2.1)

webview.getSettings().setLoadWithOverviewMode(true);

webview.getSettings().setUseWideViewPort(true);

 

webview.getSettings().setLayoutAlgorithm(LayoutAlgorithm.SINGLE_COLUMN); should be an easier option, but it’s not working on 2.1. There was a bug raised (link) was fixed in 2.2 only.

Device ID in GUID format

You can use Secure.Android_Id for getting unique id of the device. But if you need to pass it to as GUID like deviceId in iPhone, there doesn’t seem to be an easy way. The basic code I wrote for the conversion is something like this:

String android_id = getAndroidId(); long leastSignificantBits = 0; long mostSignificantBits = 0; UUID uuid = null;

    if (android_id != null) {
        String asciiConvertedText = "";
        int len = android_id.length();
        for (int i = 0; i < len; i++) {
            asciiConvertedText += "" + ((short) android_id.charAt(i));
        }

        len = asciiConvertedText.length();
        if (len > 16) {
            leastSignificantBits = Long.parseLong(asciiConvertedText.substring(0, 15));

            if (asciiConvertedText.length() > 31) {
                mostSignificantBits = Long.parseLong(asciiConvertedText.substring(16, 31));
            } else {
                mostSignificantBits = Long.parseLong(asciiConvertedText.substring(32,
                        asciiConvertedText.length()));
            }

            uuid = new UUID(mostSignificantBits, leastSignificantBits);
        } else if (len > 0) {

            leastSignificantBits = Long.parseLong(asciiConvertedText);
            uuid = new UUID(mostSignificantBits, leastSignificantBits);
        } else {
            uuid = UUID.randomUUID();
        }
    }

    if (uuid != null) {
        deviceUUID = uuid.toString();
    }
I posted this in stackoverflow and there was a good suggestion to use hashcodes. Keep in mind that hashcodes are guaranteed to return the same value only for only that JVM instance as per the java api docs. Android doc is not very clear about that.
 

Launch cancelled error

You might run into this issue when connecting to the Android emulator/device:

Failed to upload <<apk_name>>.apk on device <<device_name>>
java.net.ConnectException: Connection refused: connect
Launch canceled!

1. Start command prompt and try running the command adb kill-server && adb start-server. (you should cd to the androidsdk\tools directory OR should have added those to the path).

2. If that doesn’t work – kill any instances of adb.exe running, from the task manager. Then restart the adb as told above.