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 appstore, google's Android platform is very open and can even write Android applications using PHP. Irontech created
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 created
Google's open-source Android mobile operating system is sweeping the global smartphone market. Unlike AppleApplicationProgramDevelopers who submit to the iPhone App Store have strict guidelines and requirements. Google's Android platform is very open and can even use PHPWriteAndroidApplicationProgramIrontech created a PHP port running on AndroidProgram, Combined with the Android script Layer (Scripting Layer for Android, SL4A), you can build PHP AndroidApplicationProgram.
In this article, we will introduce how to install, configure, and use PHP for Android and SL4A.ProgramFor example, if you do not know howWritePHP AndroidApplicationProgramPlease come with me!
Install PHP for Android
To install PHP for Android, you must have a mobile phone or simulator installed with Android 1.5 or later.ApplicationProgramOpen "Unknown Source" under "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 startWritePHP AndroidApplicationProgramBut it does not work very well. You should download the Android SDK, create a simulator, and then write the code in your favorite editor.
PHP for Android
Download the Android SDK, decompress it to the specified directory, and run AndroidProgramCreate a simulator. From the menu of Android SDK and AVD manager, select "Virtual Device" and click "new" to name the simulator (such as Droid2 ), select the target platform Android 2.2, 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 an adbProgramPush your PHP script to a virtual device. adb is part of the Android SDK, which 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, goApplicationProgramOn the screen, 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.
Build Android with PHP
Application
Program
After the development environment is set,WriteOne PHPApplicationProgramRunning 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 simplified version, basically, only the core PHP functions and JSON support are included. 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, android development using JavaProgramThese components are usable.
SL4A provides a subset of the Android API (for the list of all SL4A methods, see here). You can quickly createProgramPrototype. For example, I used a short code to display and check the stock price.
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_TIE","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 ickers.\nSeparate with spaces.");
$tickers = str_replace('', '+', $response['result']);
$droid->vibrate();
$action ='quote';
break;
case'exit':$droid->exit();
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.
If you want to runApplicationProgram, GoApplicationProgramOn the 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 the traditional PHP WebApplicationProgramYou don't have to worry about how to allocate your code or 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 set your PHP AndroidApplicationProgramSubmitted to the Android Market, You can hard code a Web address that is not under your control in it, yourApplicationProgramThe image is displayed.
For example, the previous stockProgramIn fact, it is the stock information obtained from a Yahoo Web service, not in AndroidProgramHard coding directly accesses Yahoo!. I created a simple Web service as AndroidApplicationProgramSo if Yahoo decides to stop the service or modify the access method, I can only update my Web service on quoter.take88.com, android Code does not need to be changed. In addition, by using Web services, I can make some complex AndroidApplicationProgramIt becomes simpler and can use the complete PHP function instead of a lite version. Here I use PerlWriteA Web Service (using mod_perl ).
Summary
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 AndroidApplicationProgramSmall and compact.
Download this article: http://www.developer.com/img/2010/09/quoter4android.zip
Http://www.developer.com/lang/php/article.php/3904261/Build-Your-First-PHP-for-Android-Application.htm: