Start Android Emulator From Terminal

tip

Sometimes running the emulator from Android Studio doesn’t work and hangs with black screen. It can be started from terminal to avoid the problem.

By default the emulator is installed in ~/Library/Android/sdk/tools. To list all available Android Virtual Devices (AVD), use the following command:

1
2
3
4
5
6
./emulator -list-avds

Nexus_5X_API_28_x86
Nexus_S_API_15
Pixel_2_XL_API_28
Pixel_API_27

Run the following command to start the emulator from terminal:

1
./emulator -avd Nexus_S_API_15 -gpu host

Check out the docs for more emulator commands and flags.

Android 7+ Trace Files Location on Nexus 5X

The location where android trace files are stored on Nexus 5X with Android 7.0 is

1
/sdcard/Android/data/com.domain.myapp/files/filename.trace

After using the os.Debug class to generate trace files for an app

1
2
Debug.startMethodTracing("mytracefile");
Debug.stopMethodTracing();

the file can be pulled from the stored location like

1
adb pull /sdcard/Android/data/com.domain.myapp/files/mytracefile.trace

Useful Android ADB Commands

Show all events happening in the phone. The command is very useful for debugging transitions between activities, background, Home button, launcher icon start, notification icon start, etc.

1
adb logcat -b events

Show current tasks and activities running on your connected device:

1
adb shell dumpsys activity

Android ListView Optimizations

These tips are gathered from articles and Google IO videos. The items are not ordered by importance.

1. Reuse convertView

Always reuse convertView. This can improve the performance of ListView scrolling up to 50-60% percent, especially when ListView items are complex.

2. Use the Holder pattern if possible.

3. Remove realtime computation of the scrollbar thumb size

1
android:smoothScrollbar="false"

Android Timing Logger

The TimingLogger is useful to measure precisely the execution time of code blocks (or even just one line of code).

The Android docs show how it can be used. Below is a simple example:

1
2
3
4
5
6
7
8
9
TimingLogger timings = new TimingLogger("MYTAG", "MethodA");
// do some work A ...
timings.addSplit("work A");
// do some work B ...
timings.addSplit("work B);
// do some work C ...
timings.addSplit("work C");

timings.dumpToLog(); 
warning

The TimingLogger will not output anything unless it is manually enabled.

The following command enables the TimingLogger output for MYTAG

1
adb shell setprop log.tag.MYTAG VERBOSE

After the TimingLogger is enabled and the application has started generating logs, we can view them with the adb command:

1
adb logcat -v time MYTAG:W *:S

MySQL Stored Procedures

Stored Procedure Example 1

  • check for valid input parameter
  • declare handler for db error (1062 => duplicate key)
  • insert data
  • return status
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
DROP PROCEDURE IF EXISTS test_proc;

DELIMITER $$
CREATE PROCEDURE test_proc (
        IN p_test_id INT,
        IN p_test_string VARCHAR(255)
    )
BEGIN
  main: BEGIN

    # declare handler when duplicate key db error is thrown
    DECLARE EXIT HANDLER FOR 1062 BEGIN
        SELECT 1 AS status_code, 'Duplicate entry for input key values' AS status_message;
    END;

    # check if input parameter is NULL
    IF p_test_id IS NULL OR p_test_id < 1 THEN
        SELECT 2 AS status_code, 'Missing or invalid p_test_id parameter' AS status_message;
        LEAVE main;
    END IF;

    INSERT INTO test_table
    SET test_id = p_test_id,
        test_string = p_test_string;    

    SELECT 0 AS status_code, 'OK' AS status_message;
  END main;
END; 
$$
DELIMITER ;

# CALL test_proc(10, 'test string parameter');

MySQL stored procedure OUT parameter

I had to use a MySQL stored procedure written by another team, so here I don’t question the design of the procedure in any way.

My task is to call the procedure and get the OUT parameters after the execution has completed. The call is made with PHP PDO.

Android Game Loop: J2ME style

This post shows an empty basic game loop in Android, similar to how we used to make game loops on the J2ME platform.

The View implements Runnable and starts a render thread itself. An interesting thing is that we can’t invalidate the View from its own thread, as invalidate must be called from the main UI thread only. Instead we use postInvalidate which sends a message to the UI thread’s message queue. The consequences are that drawing is not performed immediately, but only when the UI thread handles the message.

SimpleXML Parse Tags with Different Namespaces

It’s a little bit tricky to parse and use XML data when tags have different namespaces. We would have to use the namespace string when accessing the objects like below with $dc_namespaces:

XML with two different namespaces

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
<!-- xmldata.xml -->
<rootnode xmlns="http://mydomain.com/2010">
    <node>
        <title>This is the first Node</title>
        <meta>
            <dc:language>en</dc:language>
        </meta>
    </node>
    <node>
        <title>This is the second Node</title>
        <meta>
            <dc:language>en</dc:language>
        </meta>
    </node>
</rootnode>

PHP code to access the parsed object:

1
2
3
4
5
6
7
8
9
<?php
$dc_namespace = 'http://purl.org/dc/elements/1.1/';

$data = simplexml_load_file('xmldata.xml');
foreach($data as $node)
{
    $title = $node->title;
    $lang  = $node->meta->children($dc_namespace)->language;
}