Tips and Tricks for Android Users
Development
Communicating Linux Chroot to Android
Dec 5th
The technique of installing a full Linux distro on your Android phone has been around for a long time. I’ve used it in the past, but for a while didn’t have a phone with a physical keyboard, which made it less useful. Now that I have CM7.1 on my G2 I’ve installed it again. I’m actually using an updated prepackaged version of Ubuntu, which has been fantastic.
However, generally speaking you can run the command line stuff and have it manipulate stuff on the filesystem, but the stuff you run “in Linux” don’t interact with Android. Poking around the other day I noticed that SL4A, the Android scripting system has a “server mode”. Normally the scripts run by SL4A communicate with a generic RPC service which hooks the interpreters back into the Android system. There’s also a mode to just startup the RPC service and bind it to the loopback port or a public interface. And it talks JSON!
So, theoretically, I should be able to startup a SL4A server, run scripts in the Linux environment, and they can send messages to the outside Android system. I needed to test it out of course. If you have SL4A installed you select View from the top level menu, then Interpreters, and then “Start Server” from the menu there, and select “Private” from the context menu that pops up. There should now be a SL4A entry in the dropdown notification area, if you tap on that you can see the running server and it’ll tell you the port. Or, you could just get it from the command line, since thats where we’re going next.
Login via ADB or terminal, or however you prefer. Swap over to Linux, or mount up the unionfs stuff if you’re doing it Saurik’s way. And then you can use anything that can connect to a TCP socket and emit some JSON to send messages. I used netcat for the simplest example:
echo ‘{“id”:1,”method”:”makeToast”,”params”:["The Great HooDoo ..."]}’ | nc 127.0.0.1 38804
And tada! The message shows up on my device:
Really hacky thing to do right now, but it’s interesting in that it would potentially let that stuff running as Linux commands to interact with the rest of the system. Even just being able to deliver local notifications so that you know to check back on something running in terminal to see updates is pretty cool.
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.
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:

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.
