[Android Development Notes] 2. First demo

Source: Internet
Author: User
Document directory
  • Preface
  • Body
  • End
Preface

The android development environment has been set up in the previous section. In this section, a new demo will be created to familiarize yourself with eclipse. Including detailed development steps, how to use simulators, how to connect to a mobile phone, how to generate an installation package, etc.

Statement

This series of articles is not a tutorial, but only for notes. please correct me if you have any mistakes.

Welcome to reprint, reprint Please retain the original source: http://www.cnblogs.com/rayee

Body

Create a demo and run it.

1. Create a project

Open eclipse, file-> New-> Project

Select Android Project

Select the android SDK version (which determines the devices on which the program runs. Android supports backward compatibility. That is to say, the android 4.0.3 device can run a program developed based on the Android 1.6 SDK. In turn, the android program cannot run. You can select the lowest version of android1.6 to be compatible with more devices, but you cannot use features in a later version in the program. The so-called "fish and bear's paw" cannot have both features. The actual situation is the King .)

Select android2.2 and click Next

Enter the program name at the first place of the program annotation, that is, the name displayed at the top of the program during running.

Everyone who has learned Java knows what a "package" is. For those who haven't touched the "package" yet, I will give a personal explanation:

Package: to prevent conflicts between classes with the same name and assign them to different groups, this group is a "package", in the form of folders, such as COM. android. demo. Under the SRC project, you can see COM/Android/demo, which contains three folders in sequence. >> Click here to view details

Note: 1. The package name cannot contain Chinese characters, 2. It cannot end with a separator ".", and 3. It cannot be less than two layers.

Tags are marked as the name of the main activity in three parts, which is automatically generated and does not need to be modified.

Activity is the most basic and commonly used component in Android components. In an Android app, an activity is represented as a separate screen (similar to the card in WML ). Every activity is implemented as an independent class and inherits from the base class of activity.> Click here to view details

The lowest supported version is the SDK version selected in the previous step.

Click Finish. The project is created.

II. Introduction to engineering documents

Is the directory structure of a project. The following describes the directory structure in sequence:

BytesSRC folder

This folder contains the Java program source code of the program. You can see that the COM. Android. Demo and demoactivity. Java packages exist, which are automatically generated when the project is created. Later coding work will be carried out in this folder.

BytesGen folder

The COM. android. demo, the package contains an R. java file, which stores information about the resources used, which is automatically maintained by the IDE and does not need to be manually modified. I will discuss it in detail later.

BytesFor Android 2.2

This is the SDK package. If you select another SDK, it will be different here.

BytesAssets folder

Folder for storing asset files.

BytesBin folder

The compiled APK program is stored in this folder.

BytesRes folder

It is also a resource folder, but it is different from the assets Folder: resources added to this folder will be automatically added to the R. in the Java folder, you can use the R. java accesses resources, and resources in the assets folder must be maintained by themselves.

The res folder contains images, text, layout, and other files.

BytesAndroidmanifest. xml file

This file is the configuration file of the program, including global variables, application components, classes used by application components, and other information. This file is very important and is required!

3. Run the program using a simulator

Next, let's run the program in the simulator...

What? You said I didn't write anything. How can I run it? Didn't ide automatically create a series of files when creating a project? Yes, these automatically created programs can run without modification.

Right-click the project demo on the left and choose run as> Android Application

And so on... Let's see what's going on. (if there is no error, it means you have configured the simulator, or your mobile phone is connecting to your computer ?)

Looking at the console log, the appropriate AVD is not found! (AVD: Android vitual device, Android virtual device, or simulator)

By the way, there is no simulator. Click "yes" to create a new one:

On the simulator Management page, click Note 1. The add interface is displayed, as shown in:

Enter the name at the second place in the "remark" field. You can understand it as needed.

The Android version is selected for the three parts marked in "tags" to match your program.

The screen size is the main aspect of the simulator. Here I select hvga.

Click "create AVD" to add a simulator. On the simulator Management page, you can see that the Simulator version 2.2 already exists.

Here I will add something that happened after clicking "Run as Android Application:

Eclipse automatically finds out if there is a suitable device (mobile phone or simulator). If it is not found, it will prompt you to add a simulator. If multiple devices that meet the specified conditions (the device version is equal to or higher than the program version) are found, the online devices are preferentially selected, if multiple online devices exist, the selection window is displayed, and you manually select the device. Then, the program is automatically installed on the selected device and runs.

You can specify manual selection in the project properties so that you need to manually select a device for each operation. Shows how to set the parameters:

Right-click mark 1 and choose proterties to open the Project Properties window.

Select manual or automatic for labeling 4. If yes, select the preferred device for labeling 4.

Turn off the simulator Management window and run the program again (right-click the project demo on the left-side and choose run as> Android Application)

Wait for the simulator to start... (If the machine configuration is poor, you can create a noodle first)

As you can see, the simulator automatically installs programs after startup and runs automatically! Are you excited? (The first time I used VB to write and drag out a window to generate an EXE program and run it, I was so excited. Because I used to tamper with HTML, I could see the source code directly. It was nothing amazing, it's amazing to get a binary exe ...)

If you are not familiar with Android, you can try this simulator to see what the Android phone looks like.

Exit the program in the simulator and you can see the program in the program list:

The simulator does not need to be closed, so it is idle and will be used in the next debugging; otherwise it can be soaked up...

4. Connect to the mobile phone to run the program

After running the simulator, you can easily run it on your mobile phone. eclipse has done it for you. You can see the result.

Connect your USB cable to your computer. Right-click the project demo on the left and choose run as> Android Application.

In the displayed device selection window, select the mobile phone:

Click "OK". After the program is installed on the mobile phone, you can see the running program on the mobile phone.

In this step, many students will encounter a problem where they cannot find their mobile phones. The problem is that the device selection window does not pop up and runs automatically in the simulator (that is, this problem occurs when I run it for the first time ), after searching, the possible reasons are as follows:

1. The mobile phone driver is not installed.

Yes, that's right! You need to install the mobile phone driver! Otherwise, the computer is just a charger and does not know the mobile phone. Install different drivers based on your mobile phone model. After the driver is installed on your mobile phone, you can see "USB connected" in the notification bar of your mobile phone. Then you can run the driver again.

2. debug mode not enabled

On the mobile phone, choose Settings> application settings> development and USB debugging.

3. No response from connecting to a computer's cell phone

Try another USB connector or USB cable

V. Program packaging and signing

Unfortunately, this section does not involve coding, and it takes time to familiarize yourself with the IDE. I will practice packaging and signing this empty demo program.

Right-click the project demo and choose export...

Mark 2: Select export Android Application and click "Next". Select the project to be packaged and click "Next" to go To the keystone page.

Select "create new keystore" because we have no keystore. If yes, select.

Select the location for storing the generated keystore and set the password (this password is very important ).

Note:

The keystore is actually a digital certificate used to sign the android program. If you do not know the digital certificate,> here to view the relevant information.

Android uses a digital certificate to determine the uniqueness of the package, establish a trust relationship with the program, and use the pre-declared resources with the user's approval. This digital certificate does not need to be authenticated by an authoritative Digital Certificate Signing organization.

We recommend that you use the same digital certificate to sign your work, which has the following benefits:

  1. It is conducive to program upgrade. When the new version of the program and the old version of the digital certificate are the same, the android system will think that these two programs are different versions of the same program. If the digital certificates of the New and Old programs are different, the android system considers them different programs and conflicts with each other, and requires the new program to change the package name.
  2. It facilitates modular design and development of programs. The Android system allows a program with the same digital signature to run in a process. The Android program regards them as the same program. Therefore, developers can develop their programs into modules, and users only need to download the appropriate modules as needed.
  3. Data and code can be shared among multiple programs through permission. Android provides a digital certificate-based permission granting mechanism. Applications can share functions or data with other programs to those programs that have the same digital certificate as themselves. If the protectionlevel of a permission (permission) is signature, this permission can only be granted to programs with the same digital certificate as the package where the permission is located.

Enter the certificate information:

Enter your own information and click "Next" to generate the keystore to the next page:

Select the location where the APK file is generated and click Finish.

Manually install demo.apk on your mobile phone to check whether it works properly.

End

This section is familiar with eclipse and Android development without coding. Poor Foundation.

The next section will start coding and familiarize yourself with Android SDK and Java.

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.