PHP to develop Android apps

Source: Internet
Author: User
Tags command line final interface key php script version window

The first part is to write the PHP script code directly on the mobile phone of the Android system and run it immediately;
The second part continues to explain how to package the written PHP script code into a AKP installation file.

First, install two APK packs on your phone.

One is sl4a (scripting Layer for Android), this is the Android scripting environment, formerly ASE (Android scripting Environment), All scripts on the Android system will be executed in this environment, download the address: sl4a_r3, the extension will be changed to. AKP can also be downloaded to the official Google http://code.google.com/p/android-scripting/ Downloads/list. (Note: If you only focus on the second part you can skip here)
Another is PFA (PHP for Android), this is the Android system to provide PHP script support for the installation package, download address: PHPFORANDROID_R1, the same underground download the extension to AKP can be, official downloads http:// Phpforandroid.net/start.

Run sl4a, the interface is as follows:

Here are some examples of how we perform hello_world.php try:

Select the second option to run the results as follows:

The script has been successfully executed.
Let's create a script ourselves and try:

We modified the $droid->getinput of line fourth to Dialoggetinput (since PFA was replaced with Dialoggetinput in the API after one version of the update):

Select API Browser to see all the APIs that provide us with:

On the menu, choose Save & Run, and the results are as follows:

Well, the first part is done.

How to package the PHP script file into a APK installation package, I refer to Google's official steps (http://code.google.com/p/android-scripting/wiki/SharingScripts) and collected some other relevant tutorials on the Internet, summarized as follows:

First, build a good Android development environment (Jdk+eclipse+android SDK).
Description: Because I have not touched Java, is also the first time to use eclipse, explain if there are naïve local experts do not laugh ha.

1, download Java JDK, enter the Web page http://java.sun.com/javase/downloads/index.jsp (or directly click to download) the following figure:

Select the Download jdk to download only the JDK, no need to download the JRE, and then the installation is complete without configuring the environment variable (which can be performed on the command line for java-version validation).

2, download eclipse, enter the page http://www.eclipse.org/downloads/(or directly click to download) the following figure:

We chose the first (that is, the Eclipse IDE for Java EE developers) and unzipped after the download is complete.

3, download the Android SDK, there are two versions of the download, one is included a specific version of the SDK, one is to include only the upgrade tool and not the specific SDK version, recommended the use of the latter, here is a case for the next to explain, enter the page http:// Androidappdocs.appspot.com/sdk/index.html (or just click here to download) the following figure:


We are here to select the top of the Windows platform, download complete after decompression.

4, Configuration development environment:

(1), install the Android development plug-in, open Eclipse, on the menu bar select Help->install New SoftWare appears as follows:

Click the Add button and the following interface appears:

Name:android (this can be customized), location:https://dl-ssl.google.com/android/eclipse/(if error, change HTTPS to HTTP), click OK, the following interface will appear:

Click the Next button and the following interface appears:


(Note: If an incorrect eclipse version is used, there is a good chance of error)

Click the Next button and the following interface appears:

Select I Accept the terms of the license agreements click Finish to enter the Install plugin interface:

After the installation is complete, the following interface appears:

Click the Yes button to restart Eclipse.

(2), configure the Android SDK, click the menu Window-> preferences, enter the following interface:

Choose your Android SDK after the extracted directory, the wrong error will be the fault, this is the upgrade tool, there is no version of the SDK.

Upgrade SDK version, select Menu Window-> Android SDK and AVD Manager appears as follows:

Select the Update All button and the following interface appears:

Select an item on the left, click Accept to express installation, click Reject means not installed, we only selected the SDK 2.1 and samples for API 7, you can customize, OK, select the Install button, enter the installation interface as follows:

The installation is complete as follows:

Create a new AVD (Android Vitural device) and, as above, go to the Android SDK and AVD Manager and select Vitural Devices:

After clicking the New button, enter the following interface:

Name can be arbitrarily taken, target select the SDK version you need, SD card size customization, click Create AVD, get the following results:

As shown on the Create AVD complete.

(3), New Android project, select Menu file-> new-> Other Enter the following interface:

Select the new Android project and click on the Next button to enter the following interface:

Name customization, application name customization, package name must contain more than one point, and integer must be entered in min SDK version.

Click Finish to appear the following interface:

Note: If there are errors such as: Project ... is missing required source folder: ' Gen ', then the Gen-> android.test-> r.java This file is deleted and eclipse will regenerate this File and does not have an error.

(4), Configuration Run, right key project-> run as-> run Configuration into the following interface:

The interface selects the AVD to run and sets the box in front of the AVD to the selected state.

(5), test project run, right key project name-> Run as-> Android application can start running the Android program as follows:

is entering

Test program Run Results

The APK installation package for this test program has been automatically generated in the bin subdirectory of the project engineering directory.

At this point, the Android development environment is configured.

Second, use the established Android development environment to package the script file as a APK installation package.

1, Download Script template project, script_for_android_template, official download address Http://android-scripting.googlecode.com/hg/android/script_for_ Android_template.zip

2. Import Template project, open Eclipse, select File-> Import on the menu bar to appear the following interface:

Select Existing Projects into Workspace and click Next to enter the following interface:

Select Archive file, click Browse, find just download a good template engineering file, click Finish.

At this point, if the console appears unable to resolve target ' android-3′ such errors indicate that the API versioning settings need to be adjusted to open the Default.properties file in the directory as follows:

Since we use the API version 7, the inside of the target=android-3 modified to Target=android-7, save, right-click template Project Select Refresh can be refreshed.

Now if error:unable to open class file C:\eclipse\workspace\ScriptForAndroidTemplate\gen\com\dummy\fooforandroid\ appears again R.java:no such file or directory such errors, previously mentioned the workaround, delete the R.java files can be again Refresh will appear R.java was removed! Recreating r.java! So that the template project was successfully imported.

3, rename the template project name and package name, right-click Template Project Selection Rename pop-up as follows:

Change the name to their own needs, I will temporarily change to Scriptforandroidmyphptest bar, click OK to complete the template project name changes.

Continue right-click Template Project Select Android Tools-> Rename application Package, pop-up windows are as follows:

I'll change it to com.pfatest here:

Click OK to appear the following interface:

Click Finish to eject the following window:

Click Yes to complete the change of the package name, and Eclipse will automatically help us update the package name in the Androidmanifest.xml file under the project home directory, otherwise we will need to update it proactively.

4, put our own PHP script file into the engineering directory under the Res/raw directory, remove the inside template from the script.py, and synchronize to modify the Src/com/dummy/fooforandroid/script.java specified script name in this line  Public final static int ID = R.raw.script; -> public final static int ID = R.raw.mytest;

Save.

5, declare all the permissions used in the script program, open the Androidmanifest.xml file, select Androidmanifest.xml below the window to edit the source file, as shown in the following figure:

Android.permission.INTERNET permissions have been turned on by default, and because my test program uses vibration and phone capabilities, you must also open Android.permission.VIBRATE and Android.permission.CALL_PHONE These two permissions, open the method: Will <uses-permission
Android:name= "Android.permission.x"/> Before and after the annotation <!–*–> removal can be as follows:

Save.

6, compile, package, run, or right click the project name, select Run as-> run configurations ... To open the Run Configuration window:

In the list, select the emulator we want to run, and click Run below.

At this point, the hands of a friend of the machine may wish to the bin directory has been generated a good APK packet copy to the phone to install and run to try.



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.