Compile Android applications with PHP

Source: Internet
Author: User
Google's open-source Android mobile operating system, which uses PHP to write Android applications, is sweeping the global smartphone market. Unlike Apple, it has strict guidelines and requirements for developers who want to submit their applications to the iPhone Appstore. Google's Android platform is very open and can even use PHP to compile Android applications.
Google's open-source Android mobile operating system is sweeping the global smartphone market. Unlike Apple, it has strict guidelines and requirements for developers who want to submit applications to the iPhone App Store. Google's Android platform is very open and can even write Android applications in PHP, irontech has created a PHP porting program running on Android. combined with the Scripting Layer for Android and SL4A, you can build a PHP Android application.

In this article, we will introduce how to install, configure, and use PHP for Android and SL4A. 51CTO will take a simple demo program as an example, if you still don't know how to compile the PHP Android application, please come with me!

Install PHP for Android
To install PHP for Android, you must have a mobile phone or simulator with Android 1.5 or later installed and open "unknown source" under "application settings ", after setting, you can install the SL4A environment and PHP for Android APK.

Installing SL4A is quite simple, but after installing PHP for Android, you need to install it again to install all its functions. if you encounter any problems during installation, here is a video demonstration.

Set the PHP for Android development environment
Theoretically, once you have installed PHP for Android, you can write the PHP Android application, but it does not work well. you should download the Android SDK, create a simulator and then use your favorite editor to write code.

PHP for Android

Download the Android SDK, decompress it to the specified directory, run the Android program under the tools Directory to create a simulator, and select "virtual device" from the Android SDK and AVD manager menu ", click "new", give the simulator a name (such as Droid2), select Android 2.2 as the target platform, enter 10 MB in the SD card size, and click "create AVD ".

After creating the Droid2 simulator, click the "start" button, which will be a little troublesome, because you cannot just copy the file to the virtual device, but also need to set up, you must set port forwarding, use a program called adb to push your PHP script to a virtual device. adb is part of the Android SDK and is also located in the tools Directory.

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

◆ When your new virtual device is running, go to the application screen and click "SL4A ".

◆ On the SL4A screen, click "menu", select "View", and select "interpreter ".

◆ Click "menu" again, select "start server", and select "private ".

◆ Drag down the Android notification bar and you will see the SL4A service (click this service and pay attention to the port number that your server listens to, such as 47000 ).

◆ Open a shell or command prompt and use the adb tool to set port forwarding. for example, enter the command "adb forward tcp: 9999 tcp: 47000" and replace 47000 with your port number.

◆ Set the AP_PORT environment variable and run "export AP_PORT = 9999" on Unix or Mac. on Windows, type "set AP_PORT = 9999 ".

◆ To test your script on the simulator, run "adb push my_script.php/sdcard/sl4a/scripts" and replace "my_script.php" with your script name ".

You can also perform a test on a real mobile phone. to make things easier, you should set an ANDROID_HOME environment variable, point to the Android SDK location, and add the tools subdirectory to the Path.

Use PHP to build Android applications
After setting up the development environment, writing a PHP application to run on Android is actually very simple. you only need to pay attention to one thing, the PHP version included in PHP for Android is a very simplified version. it basically only contains 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. these components can be used when developing Android programs using Java.

SL4A provides a subset of the Android API (for the list of all SL4A methods, see here). With PHP for Android, you can quickly create a program prototype, for example, the following code shows and checks the stock price.

DialogCreateSpinnerProgress ("Querying stock information server... "," 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_RA TIO "," 52_WEEK_LOW "," 52_WEEK_HIGH "," VOLUME "$ 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_repla Ce ("'", "'", $ 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';} break; case 'get _ tickers': $ response = $ droid-> getInput (" Stock Tickers (max. 3) "," Enter Tickers. \ nSeparate with spaces. "); $ tickers = str_replace ('', '+', $ response ['result']); $ droid-> vibrate (); $ action = 'quote '; break; case 'eg': $ droid-> exit (); break ;}}?> Save the above code as quoter4android. php file, uploaded to your simulator. if your simulator is not running, start it first, configure your port forwarding using adb under the Android SDK tools directory, and upload quoter4android. php file.

To run the application in your simulator, 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 mobile phone, you can configure port forwarding and connect your mobile phone to your computer through USB. it is easier to copy the script to the sl4a/scripts directory. If you want to run the script on your mobile phone, you must unplug the USB cable first. Otherwise, you will not see any installed script when you click the SL4A icon.

You will find that the first line of the above code sets a constant QUOTE_SERVER. if you are used to traditional PHP Web applications, you do not have to worry about how to allocate your code, you don't have to worry about future changes. now we have to look at how it works in Android. you have to allocate your real PHP code. therefore, if you decide to submit your PHP Android application to the Android Market, you can hardcode a Web address that is not under your control, and your application will be listed as follows.

For example, the previous stock program actually obtained the stock information from a Yahoo Web service, instead of accessing Yahoo directly through hard code in the Android program. I created a simple Web service, as the connection between the Android app and Yahoo stock service, if Yahoo decides to stop this service or modify the access method, I can only update my Web service on, android code does not need to be changed. In addition, by using Web services, I can make some complex Android applications simpler and use the complete PHP functions instead of a lite version, here I wrote a Web service (using mod_perl) using Perl ).

You can do a lot of things with SL4A and PHP for Android. This article only talks about things that are very superficial. both projects are very young. In fact, when I write this article, SL4A has released a new version. as their maturity increases, their functions become more and more powerful. Remember to keep your Android applications small and compact in any situation.

Download this article:


Original article: Build Your First PHP for Android Application

Author: Keith Vance

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.