Using PHP to develop Android application technology Introduction _android

Source: Internet
Author: User
Tags compact install php php define php script port number

Google's Open-source Android mobile operating system is sweeping the global smartphone market, unlike Apple, which has strict guidelines and requirements for developers who want to submit apps to the iphone App Store, and Google's Android platform is very open, You can even write Android apps in PHP, Irontech creates a PHP porting program that runs on Android, and combines the Android script layer (scripting Layer for android,sl4a) to build PHP Android apps.

In this article, we'll explain how to install, configure, and use PHP for Android and Sl4a,51cto will take a simple demo program as an example, and if you're not sure how to write PHP Android apps, follow me!

Install PHP for Android

The prerequisite for installing PHP for Android is that you have a phone or simulator with Android 1.5 or higher and open "unknown source" under "Application Settings", and you can install the SL4A environment and PHP for Android APK.

Installing sl4a is fairly straightforward, but after installing PHP for Android, you'll need to install it again to install all of its features, and if you're having trouble installing it, here's a video demo.

Set up the PHP for Android development environment

Theoretically, once you've installed PHP for Android, you can start writing PHP Android apps, but it doesn't work very well, you should download the Android SDK, create an emulator, and then write code with your favorite editor.

PHP for Android

Download the Android SDK, unzip it to the specified directory, run the Android program in the Tools directory, create an emulator, choose "Virtual Device" from the Android SDK and AVD Manager menu, click the "New" button, and give the simulator a name (such as Droid2), Select the target platform Android 2.2,SD card size input 10MB, and finally click "Create AVD".

After creating the Droid2 simulator, click the "Start" button, there will be a little trouble, because you can not only copy files to the virtual device, you need to set up, you must set up port forwarding, using a program called ADB to push your PHP script to the virtual device, ADB is part of the Android SDK, It is also located in the Tools directory.

Next, you start a server on a virtual device and then send the script to the server, and the steps below will help you quickly set up and run it.

1. When your new virtual device is running, go to the application screen and click "sl4a".
2. On the sl4a screen, click the "Menu" button, select "View" and select "Interpreter".
3. Click the "Menu" button again, select "Start Server" and select "Private".
4. Drag down the Android notification bar, you should be able to see the SL4A service (click on this service, pay attention to your server listening to the port number, such as 47000).
5. Open a shell or command prompt, use the ADB tool to set port forwarding, for example, enter the command "ADB forward tcp:9999 tcp:47000", replace 47000 with your port number.
6. Set the AP_PORT environment variable, run "Export ap_port=9999" on Unix or Mac, and type "set ap_port=9999" on Windows.
7. If you want to test your script on the simulator, you can run "adb push my_script.php/sdcard/sl4a/scripts" and replace "my_script.php" with your script name.

You can also test on a real phone, and to make things easier, you should set up a android_home environment variable, point to the Android SDK location, and add the tools subdirectory to the path.

Building an Android application with PHP

After setting up a development environment, writing a PHP application to run on Android is actually very simple, you just have to pay attention to one thing, the PHP for Android contains PHP version is an extremely compact version, basically contains only the core PHP functions and JSON support, If you are familiar with the Java framework, you will find that SL4A does not provide access to all the components you want to use, and that these components are available when you use Java to develop Android programs.

SL4A provides a subset of the Android API (see here for a list of all sl4a methods), using PHP for Android You can quickly create a program prototype, such as the following I use a very short code to achieve the stock price display and inspection.

Copy Code code as follows:

<?php define (' Quote_server ', ' ');
Require_once ("android.php");
$droid = new Android ();
$action = ' get_tickers '; $tickers = ';
while (TRUE) {switch ($action) {case ' quote ': $droid->dialogcreatespinnerprogress ("Querying the stock information SE RVer ... "," Please Wait "); $droid->dialogshow ();
$quotes = @array_slice (Json_decode (file_get_contents (sprintf (Quote_server, $tickers)), 0, 3);
$droid->vibrate ();
$droid->dialogdismiss ();//Possible Data points.//"SYMBOL", "NAME", "Last_trade", "More_info", "Last_trade_date", " Last_trade_time "," OPEN "," Days_high "," Days_low "," Dividend_share "," Pe_ratio "," 52_week_low "," 52_week_high "," VOLUME "$output =";
($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");
$droid->dialogsetnegativebuttontext ("Exit");
$droid->dialogshow ();
$response = $droid->dialoggetresponse ();
if ($response [' result ']->which = = ' negative ')
$action = "Exit";
else {
$action = ' get_tickers ';
Case ' get_tickers ': $response = $droid->getinput ("The Stock tickers" (max. 3) "," Enter tickers.\nseparate with Spaces. ");
$tickers = Str_replace (', ' + ', $response [' result ']);
$droid->vibrate ();
$action = ' quote ';
Case ' exit ': $droid->exit ();
Exit ();

Save the above code as a quoter4android.php file, upload it to your simulator, and if your emulator is not running, start it and use the ADB in the Android SDK Tools directory to configure your port forwarding, and upload the quoter4android.php file.

If you want to run the application in your emulator, go to the Application screen, click the sl4a icon, and then click the quoter4android.php option.

If you want to install quoter4android.php on your phone, you can set up port forwarding, connect your phone to the computer via USB, and make it easier to copy the script to the Sl4a/scripts directory. But if you want to run the script on your phone, you must unplug the USB cable, or you will not see any installed script when you click the sl4a icon.

You'll find that the first line of the above code sets a constant Quote_server, and if you're used to a traditional PHP Web application, you don't have to worry about assigning your code or worrying about future changes, and now we're going to look at how it works in Android, You have to assign your real PHP code, so if you decide to submit your PHP Android application to Android Market, you can hard-code a Web address that is not under your control, and your application will be netizens.

For example, the previous stock program was actually a stock information obtained from a Yahoo Web service, rather than a hard coded direct access to Yahoo in Android, I created a simple Web service as a link between the Android app and the Yahoo Stock Service, So if Yahoo now decides to stop the service or modify the access method, I can just update my Web service, and the Android code won't have to change anything. In addition, by leveraging Web services, I can make some complex Android applications simpler, and this allows you to use the full PHP functionality rather than a streamlined version, where I write a Web service using Perl (Mod_perl).


There are a lot of things you can do with sl4a and PHP for Android, this article only talks about something very superficial, these two projects are very young, in fact, when I write this article, SL4A released a new version, as they become more mature, the function will be more and more powerful. Finally, remember, in any case, keep your Android application small and compact.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.