Home  >  Article  >  Backend Development  >  How to develop android applications with php

How to develop android applications with php

伊谢尔伦
伊谢尔伦Original
2016-12-01 11:22:462105browse

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 applications to the iPhone App Store, Google’s Android platform is very open. You can even write Android applications in PHP. Irontech has created a PHP porting program that runs on Android. Combined with Android's Scripting Layer for Android (SL4A), you can build PHP Android applications.

In this article, we will introduce how to install, configure and use PHP for Android and SL4A. 51CTO will use a simple demo program as an example to explain. If you still don’t know how to write PHP Android applications, then please Come with me!

Install PHP for Android

The prerequisite for installing PHP for Android is that you must have a mobile phone or emulator with Android 1.5 or higher installed, and open "Unknown Sources" under "Application Settings" and set it up After that, 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 in order to install all its features. If you have trouble during the installation, here is a video demonstration.

Set up PHP for Android development environment

Theoretically, once you have PHP for Android installed, you can start writing PHP Android applications, but it doesn't work very well, you should download the Android SDK, create An emulator, then code in your favorite editor.

How to develop android applications with php

PHP for Android

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

After creating the Droid2 emulator, click the "Start" button. There will be a little trouble here, because you can't just copy the files to the virtual device, you also need to set it up. You must set up port forwarding and use a program called adb to transfer your The PHP script is pushed to the virtual device, adb is part of the Android SDK, it is also located in the tools directory.

Next, you will start a server on the virtual device and then send the script to the server. The following steps will help you get up and running quickly.

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

On the SL4A screen, click the "Menu" button, select "View" and then "Interpreter".

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

Drag the Android notification bar down and you should be able to see the SL4A service (click this service and pay attention to the port number your server is listening 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". 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", replacing "my_script.php" with your script name.

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

Build Android applications using PHP

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, that is, the PHP version included in PHP for Android is an extremely streamlined version. The version basically only contains 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 when developing Android programs using Java. usable.

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 program prototypes. For example, below I use a very short code to display and check stock prices. .

<?php define(&#39;QUOTE_SERVER&#39;, &#39;http://xxx.com/?ticker=%s&#39;); 
require_once("Android.php"); 
$droid = new Android(); 
$action = &#39;get_tickers&#39;;$tickers = &#39;&#39;; 
while (TRUE) { switch ($action) { case &#39;quote&#39;:$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 = &#39;&#39;;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 &#39; 
$output = str_replace("&#39;", "&#39;", $output); 
$droid->dialogCreateAlert("Your stock quotes", $output); 
$droid->dialogSetPositiveButtonText("Get new quote"); 
$droid->dialogSetNegativeButtonText("Exit"); 
$droid->dialogShow(); 
$response = $droid->dialogGetResponse(); 
if ($response[&#39;result&#39;]->which == &#39;negative&#39;)  
{ 
    $action = "exit"; 
} 
 else { 
    $action = &#39;get_tickers&#39;; 
} 
break; 
    case &#39;get_tickers&#39;:$response = $droid->getInput("Stock Tickers (max. 3)", "Enter Tickers.\nSeparate with spaces."); 
$tickers = str_replace(&#39; &#39;, &#39;+&#39;, $response[&#39;result&#39;]); 
$droid->vibrate(); 
$action = &#39;quote&#39;; 
break; 
    case &#39;exit&#39;:$droid->exit(); 
exit(); 
break; 
    } 
} 
?>

Save the above code as quoter4android.php file and upload it to your emulator. If your emulator is not running yet, please start it first, use adb in the Android SDK tools directory to configure your port forwarding, and Upload the quote4android.php file.

If you want to run the application in your emulator, go to the Applications screen, click on the SL4A icon and then click on 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 scripts to the sl4a/scripts directory to make it easier. But if you want to run the script on your phone, you must unplug the USB cable first, otherwise you will not see any installed scripts when you click on 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 don’t have to worry about how to distribute your code, and you don’t have to worry about future changes. Now we are going to look at How it works in Android, you have to distribute your real PHP code, so if you decide to submit your PHP Android app to the Android Market, you can hardcode a web address in it that you don't control The application will search according to the picture.

For example, the previous stock program actually gets the stock information from a Yahoo web service. Instead of hardcoding direct access to Yahoo in the Android program, I created a simple web service as an Android application and Yahoo stock The connection between services, so if Yahoo decides to stop this service now, or changes the access method, I can only update my Web service at xxx.com, and the Android code does not need to make any changes. Additionally, by leveraging web services, I can make some complex Android applications simpler and use full PHP functionality instead of a stripped-down version. Here I wrote a web service in Perl (using mod_perl ).

Summary

There is a lot you can do with SL4A and PHP for Android, this article only scratches the surface, both projects are very young, in fact, as I write this, SL4A has released a new version , as they become more mature, their functions will become more and more powerful. Finally, remember that in any case, keep your Android apps small and compact.


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn