Tips and Tricks for Android Users
Posts tagged Development
On Device Packet Capture
Aug 5th
When I need to capture network traffic from my device I normally capture traffic at a router to see what’s going on. I had seen some mentions of running tcpdump on device and pulling off the pcap file to a desktop to inspect, but Androshark was what people mentioned the most. And it didn’t seem to be actively developed any more. I ran across Shark for Root and Sharkreader recently however. It’s an app for packet capture and a simple packet capture viewer directly on the device. Works out pretty well. Requires root access, and it seems to be working quite well on my Nexus One with CM6. Screenshots below.
Start/stop capture, writes to the sdcard by default:

View packet dump stream:

View contents of an individual packet:

Assember/Disassembler for Dalvik
Jul 30th
I ran across a mention of an assember/disassember pair for Dalvik in an XDA Forum posting. Just had a chance to give at least the assembler a try and make sure I could get things working before I posted. Yep, working on the emulator at least. There are a few examples you can start with, instructions for assembling and running are in the comments. And some pages in the wiki that look like they’ll provide some great starting points.
Text to Speech Using Scripting
Jul 25th
I’ve been fooling around with Scripting Layer for Android to generate some speech notifications. Two issues I ran into, figured I would share if anyone else does. The first was that the examples still use the droid.speak() call, and it should be droid.ttsSpeak() instead. Quick fix. The second however I only saw on my CM6 device, which was that no speech was coming out ever after I updated the ttsSpeak() call. logcat turned up the issue quickly enough though. I just needed to go into settings from the home screen and download the data necessary to generate speech. Once you’re in the “Voice Input and Output” area the process is obvious. It’s just knowing that you need to go into settings to get speech working that’s a bit tricky.
Commodore 64 Emulation
Jul 24th
I was happy to see there’s a Commodore 64 emulator in the Marketplace for Android, the Frodo emulator. I played around with a bit, but what I really wanted to do was hack around a bit with some old school basic. This is one of the computers I grew up with, and I get nostalgic for the old days every once in a while. However, I was having a really hard time figuring out how to get the version of Frodo under Android to create a new disk for me to save stuff to. If you can’t save the stuff you’re playing with what’s the use? I could save out the whole state, but I figured there had to be a better way.
So I downloaded a version of VICE for my desktop system. That has a much richer set of controls, one of which is creating and attaching a fresh image (it’s a ‘file’ menu item). I figured if I was lucky the image created with VICE would work to at least get me started on Frodo on my device. And lucky me, yes it does! Actually, now I can push the d64 images back and forth, use the emulator on my desktop to do a little poking around and push the file over to my device to take with me. Very nice! Just remember, save with replace is ‘SAVE “@0:FILENAME”,8′. Otherwise an attempt to overwrite a file will just silently fail. Ahhh, nostalgic.
Android Scripting Environment is now SL4A
Jul 22nd
I downloaded and installed the latest Android scripting package, now called Scripting Layer for Android, or SL4A. I had some issues with it under CyanogenMod5 (probably my own, but I never debugged). Today I updated to a CM6 release and it seems to be working a whole lot better. There are links to a whole bunch of examples on the Tutorials page.
The application interface itself is pretty simple. When it first loads up you won’t have any interpreters besides shell. If you go into the View menu, select interpreters, and then select Add from the menu under there you can add other interpreters. The interpreters generally come with example scripts, which will show up in the main list view once they’re loaded:

There’s a preferences screen, which covers mostly visual options:

If you long press on a script you have an option to edit it:

The editor that comes up is just a simple but effective textbox, so at least you can edit scripts in place on the device:

And then you can run a script, in this place displaying a toast message over the keyboard when I run the hello world program:

Gettie Awards
May 29th
If you’ve had a hacking waiting around for the right time to be released, this might be it. The Gettie Awards is offering $15,000 to the best Android app they find. There are categories for all the major mobile platforms, and an overall prize of $25,000! If you haven’t been coding, dust off your compiler and get cracking.
Android Scripting Environment
Jun 9th
Google has recently released a scripting environment for Android that allows you to write Python, Lua, or BeanShell scripts that interface with Android functionality. From the project page:
- Handle intents
- Start activities
- Make phone calls
- Send text messages
- Scan bar codes
- Poll location and sensor data
- Use text-to-speech
Getting WebView To Load New Pages
Jun 3rd
There are a bunch of examples of using WebView to display HTML content loaded from the web in an Android app, including the sample from Google for the WebView class. However the ones I found generally don’t load new pages on their own. I can load up Google, but if you click on anything in the app it launches a browser and takes the user out of the app. The answer to the problem is in the additional points on that Hello, WebView page – you need to set a new WebViewClient to handle the URL load requests. Their example is very helpful, but I prefer to do the override with an anonymous inner class:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
WebView webview = new WebView(this);
webview.getSettings().setJavaScriptEnabled(true);
webview.setWebViewClient(new WebViewClient() {
@Override public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
});
webview.loadUrl("http://google.com");
setContentView(webview);
}
However, if you follow a link to an image it downloads instead of opens in the view. That’s the same thing the built in browser does, but wasn’t the behavior I expected. I’ll have to figure out how to handle that a bit better.
Monitoring Network Traffic Using OS X
Jun 1st
Here’s a tip for monitoring network traffic from your Android phone using OS X. The same thing works for iPhone (or any other mobile device you can configure to use a wifi connection). I frequently use it to see how some bit of client/server interaction is done.
- Install Wireshark
- Follow the instructions in the readme to also install the ChmodBPF script
- Under Sharing area of the OS X settings app configure your system to use an ethernet connection and share it out to wifi clients
- Now configure your device to connect to the wifi network provided by your system, test to make sure it works
- Startup Wireshark and set it to capture traffic (wifi is en1 on MacBook Pro systems, what I normally use)
And that’s it, you should now get quite readable dumps of what applications are doing to communicate. Lots of interesting things you can learn digging into how folks structure their client/server interaction.
Compiling C Code for Android Using OS X
May 27th
I wanted to try out compiling some native C code for use on my device, but I wanted to do it using my OS X machine. I found this post about using the prebuilt toolchain over at Android Tricks, but figured I would write up some additional details for those who might also be looking.
- Follow the instructions to download and build Android from source. Follow the whole thing (I had to create a case-sensitive disk image and all), including the actual build step. Otherwise you won’t have the libraries necessary and agcc will error out when you try to run it.
- Add the prebuilt/darwin-x86/toolchain/arm-eabi-4.2.1/bin subdirectory of where ever you built the source to your PATH, add it to your .bash_profile if you want.
- Download the agcc wrapper script, put it somewhere in your path, and make it executable.
Now you should be ready to compile a program and download it to your phone. This was my test app:
#include <stdio.h>
int main( int argc, char **argv ) {
printf("Hello from Droid Hacks!\n");
return 0;
}
And you should be able to compile it with “agcc hello.c -o hello” and end up with a hello executable:
~ > agcc hello.c -o hello ~ > file hello hello: ELF 32-bit LSB executable, ARM, version 1 (SYSV), dynamically linked (uses shared libs), not stripped ~ >
And you can move the file across to the phone and run it. You’ll have to make a directory to push it into. The sdcard is marked as noexec, so you can’t run stuff from there. And the data directory has more restrictive permissions. So you’ll have to su and create a directory on the data partition, and relax the perms on that directory:
~ > adb shell $ su # mkdir /data/droidtest # chmod 777 /data/droidtest # exit $ exit ~ > adb push hello /data/droidtest 418 KB/s (6747 bytes in 0.015s) ~ > adb shell $ cd /data/droidtest $ ls -l -rwxrwxrwx shell shell 6747 2009-05-27 08:56 hello $ ./hello Hello from Droid Hacks! $
And of course you can run the same thing from the terminal on your device as well: