Android Learning 1

Source: Internet
Author: User

Let's talk a little bit about it. Let's look at how to start programming for an android project.

First, create an android project, and then follow the steps.

Step 1. initialize the parameters in androidmanifest. xml.

 1:  <manifest xmlns:android="http://schemas.android.com/apk/res/android"
 2:      package="com.microjobsinc.mjandroid" android:versionCode="1" 
 3:        android:versionName="1.0">
 4:    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
 5:    <uses-permission android:name=
 6:        "android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" />
 7:    <uses-permission android:name="android.permission.CALL_PHONE" />
 8:    <uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION" />
 9:    <uses-permission android:name="android.permission.INTERNET" />
10:  
11:    <application android:icon="@drawable/icon2">
12:      <uses-library android:name="com.google.android.maps" />
13:      <activity android:name=".MicroJobs" android:label="@string/app_name">
14:        <intent-filter>
15:          <action android:name="android.intent.action.MAIN" />
16:          <category android:name="android.intent.category.LAUNCHER" />
17:        </intent-filter>
18:      </activity>

Explanation:

The first activity (Row 13) in the application node (row 11) is the entry point of your program.

 

Package = "com. microjobsinc. mjandroid"

Is the package name.

 

Android: versioncode

It automatically grows as your program is updated.

 

Android: versionname

It is up to you to decide what the name is, and it is a string for the user to see. For example, you can set it to "1.1.1 ".

 

<Uses-Permission Android: Name =...

Because your application needs to use some Android features to complete functions, such as connecting to the Internet and GPS .. The user's permission switch is declared here.

(There are a lot of built-in licenses in Android, you can find"Android. manifest. Permission"Documentation, you can also customize the license .) Several features are described as follows:

Access_fine_location:Get your location information from GPS.

Access_location_extra_commands:All location commands.

Call_phone: You can call.

Access_mock_location: Obtain a simulated location when running the simulator.

Internet: Connect to the Internet.

 

Android: icon = "@ drawable/icon2"

The image icon2 is under the drawable folder.

 

Focus on the first activity (main activity)

1:  <activity android:name=".MicroJobs" android:label="@string/app_name">
2:  

 

First, define some attributes for this main activity:

Android: Name

The name of the activity. The full name is packagename + activityname. In this example, Com. microjobsinc. mjandroid. microjobs. Remember that (.) is indispensable.

 

Android: Label

This lable is the content displayed at the top of the screen when the activity is displayed on the screen.

 

Then declare an intent filter to tell Android when to run the activity ):

1:  <intent-filter>
2:    <action android:name="android.intent.action.MAIN" />
3:    <category android:name="android.intent.category.LAUNCHER" />
4:  </intent-filter>

Action

For example, Android is trying to start an app, so it will first look for a specific activity. This activity must be ready to parse the main method (that is, its name is Android. intent. action. main ).

Any app that can be started must carry an action as the main activity.

 

Category

Specify category to display a shortcut on the UI to start the app.

 

Step 2. initialize the Java file.

Microjobs. Java is the entry of the program. The following analyzes the microjob. Java code.

1:  /**
2:  * MicroJobs
3:  */
4:  public class MicroJobs extends MapActivity { }

The vast majority of activities are inherited from the activity. In this Code, mapactivity is inherited because we want to use the powerful map function integrated within android, at the same time, most of our operations are performed on the map.

The initialization content must be placed in the oncreate method, which will be called at app startup.

As shown in:

 1:  MapView mvMap;
 2:  MicroJobsDatabase db;
 3:  MyLocationOverlay mMyLocationOverlay;
 4:  double latitude, longitude;
 5:   
 6:  /**
 7:  * Called when the activity is first created.
 8:  *
 9:  * @see com.google.android.maps.MapActivity#onCreate(android.os.Bundle)
10:  */
11:  @Override
12:  public void onCreate(Bundle savedInstanceState) {
13:  super.onCreate(savedInstanceState);
14:   
15:  setContentView(R.layout.main);

The oncreate method accepts a parameter whose type is bundle, and then calls the oncreate method of the base class. But why should we upload a bundle type of data? What is this bundle?

 

Bundle is a mechanism used by Android to transmit structured data between activities. It is essentially a key-object pair. The code here does not use any resources in the bundle, but it still uses the oncreate.

 

In the next line, setcontentview tells android that we want to display it on the screen.R. layout. Main. Java. This "R" is automatically generated.

 

Technorati Tag: Android, tutorial

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.