January 17, 2021
Hot Topics:

Build Your First PHP for Android Application, Page 2

  • By Keith Vance
  • Send Email »
  • More Articles »

Building an Android Application with PHP

Writing a PHP application to run on Android is really pretty simple after you set up your development environment. One thing you'll notice is that the version of PHP included with PHP for Android is an extremely stripped down build. You basically have the core PHP functions and JSON support -- that's about it. And if you're an Android developer who's familiar with the Java framework, you'll notice that the Scripting Layer for Android doesn't provide access to all of the components you're used to having when building a full-blown Android application with Java.

What SL4A does provide are "facades" to a subset of the Android APIs. (A complete listing of all of the methods available via the SL4A is available here. ) But what's fun about PHP for Android is that you can quickly prototype an application and see it in action with just a few lines of code. I'll demonstrate this with an application for checking stock quotes that's less than 60 lines of code.

define('QUOTE_SERVER', 'http://quoter.take88.com/?ticker=%s');
$droid = new Android();
$action = 'get_tickers';
$tickers = '';
while (TRUE) {
switch ($action) {
case 'quote':
$droid->dialogCreateSpinnerProgress("Querying stock information server ...", "Please wait");
$quotes = @array_slice(json_decode(file_get_contents(sprintf(QUOTE_SERVER, $tickers))), 0, 3);
// Possible data points.
$output = '';
for ($i = 0, $cnt = count($quotes); $i < $cnt; $i++) {
$output .= "Company: " . $quotes[$i]->NAME ."\n";
$output .= "Ticker: " . $quotes[$i]->SYMBOL . "\n";
$output .= "Last trade: $" . $quotes[$i]->LAST_TRADE . "\n";
$output .= "\n";
$output = html_entity_decode($output, ENT_QUOTES, "UTF-8");
// Something is wrong with '
$output = str_replace("'", "'", $output);
$droid->dialogCreateAlert("Your stock quotes", $output);
$droid->dialogSetPositiveButtonText("Get new quote");
$response = $droid->dialogGetResponse();
if ($response['result']->which == 'negative') {
$action = "exit";
} else {
$action = 'get_tickers';
case 'get_tickers':
$response = $droid->getInput("Stock Tickers (max. 3)", "Enter Tickers.\nSeparate with spaces.");

$tickers = str_replace(' ', '+', $response['result']);
$action = 'quote';
case 'exit':

Copy and paste the above code into your editor and save it as quoter4android.php and upload it to your emulator. If your emulator isn't running, fire it up, configure your port forwarding and upload the quoter4android.php file with the adb application included in the Android SDK tools directory.

To run the application in your emulator, go to the Applications screen, click the SL4A icon and click the quoter4android.php option.

To install quoter4android.php on your phone, you can set up port forwarding. But it's easier to just plug the phone into your computer via USB and copy the script into the sl4a/scripts directory. However, to run scripts on your phone, you have to unplug it from your computer or else you won't see any of the installed scripts when you click on the SL4A icon.

You'll notice that the first line of this application sets up a constant QUOTE_SERVER. If you're used to building traditional PHP Web applications, you don't have to worry about distributing your code and making changes to it in the future -- that's not how it works with Android. You have to distribute your actual PHP code. So if you decide to put your PHP Android application in the Android Market and you hardcode a Web address in it that you don't control, your application could break down the road.

For example, this stock quote application actually pulls the stock information from a Yahoo Web service. But rather than hardcoding direct access to Yahoo into the Android application, I created a simple Web service as a link between the Android application and the Yahoo stock service. So now if Yahoo decides to stop offering this service, or if they change the way it's accessed, I can just update my Web service located at quoter.take88.com. The Android code doesn't need to change, and nobody's walking around with a broken application on their phone. Also, by leveraging a Web service, I was able to take some of the complexity out of the Android application and move it to my Web service, where I have access to full-blown languages and not just a stripped down version of PHP. In this case, I wrote the Web service in Perl using mod_perl.



There's a lot you can do with the SL4A and PHP for Android; this article only scratches the surface of what's possible. While both projects are very young -- in fact, a new version of SL4A dropped while I was writing this story (feel free to run the newest version) -- as they mature, more possibilities will present themselves. In any case, keep your Android applications small, tight and light.

Code Download

About the Author

Keith Vance is a software engineer and a journalist. He's been developing web applications professionally since 1997 and he received his journalism degree from the University of Washington in 2008.

Originally published on https://www.developer.com.

Page 2 of 2

This article was originally published on September 20, 2010

Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Thanks for your registration, follow us on our social networks to keep up-to-date