How to develop Android apps with PHP

Source: Internet
Author: User
Tags php define
Google's open-source Android mobile operating system is sweeping the global smartphone market, unlike Apple, which has stringent guidelines and requirements for developers who want to submit applications to the iphone App Store, and Google's Android platform is very open, You can even write Android apps in PHP, Irontech create a PHP porting program that runs on Android, and with the Android scripting Layer (Scripting layer for android,sl4a), you can build PHP Android app now.

In this article, we'll show you how to install, configure, and use PHP for Android and Sl4a,51cto will take a simple demo program as an example, if you don't know how to write a PHP Android app, then follow me!

Install PHP for Android

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

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

Setting up a PHP for Android development environment

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

PHP for Android

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

After creating the Droid2 simulator, click "Start" button, here will be a little trouble, because you can not only copy files to the virtual device, but also 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 the virtual device and then send the script to the server, and the following steps will help you set up and run quickly.

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

On the sl4a screen, click the menu button, select View, and then select Interpreter.

Click the "Menu" button again, select "Start Server" and select "Private".

Drag down the Android notification bar and you should be able to see the SL4A service (click on the service, note the port number your server listens to, such as 47000).

Open a shell or command prompt and use the ADB tool to set up 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, on UNIX or Mac, run "Export ap_port=9999", and on Windows, type "set ap_port=9999".

If you want to test your script on the emulator, 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, to make things easier, you should set up a android_home environment variable, point to the Android SDK location, and add the tools subdirectory in path.

Build Android Apps with PHP

After setting up the development environment, writing a PHP application to run on Android is actually very simple, you just need to pay attention to one thing, PHP for Android contains the PHP version is an extremely streamlined 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 these components are available when you use Java to develop an Android program.

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

<?php define (' Quote_server ', ' http://xxx.com/?ticker=%s '); Require_once ("android.php"); $droid = new Android (); $action = ' get_tickers '; $tickers = '; while (TRUE) {switch ($action) {case ' quote ': $droid->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_ratio "," 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_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 ';} 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 ' exit ': $droid->exit (); Exit ();     Break }}?>

Save the above code as a quoter4android.php file, upload it to your emulator, if your emulator is not running, start it first, configure your port forwarding using the ADB in the Android SDK tools directory, 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 copy the script to the sl4a/scripts directory more easily. But if you want to run the script on your phone, you must first unplug the USB cable, or you won't see any installed scripts when you click the sl4a icon.

You'll find that the first line of the code above sets a constant quote_server, and if you're used to traditional PHP Web applications, you don't have to worry about allocating your code or worrying about future changes, 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 app to Android Market, you can hard code a Web address that is not under your control and your app will retrace.

For example, before this stock program was actually getting stock information from a Yahoo Web service, instead of hard-coded direct access to Yahoo in the Android program, I created a simple Web service, as a connection between Android app and Yahoo Stock Service, So if Yahoo now decides to stop the service, or change the access method, I can just update my Web service in xxx.com, and the Android code doesn't need to be changed. In addition, by leveraging Web services, I can make some complex Android applications easier, and use full PHP functionality instead of a lite feature, where I wrote a Web service using Perl (Mod_perl).

Summary

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

  • Related Article

    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: info-contact@alibabacloud.com 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.