Android learning path-activity (1) of four Android components and androidactivity

Source: Internet
Author: User

Android learning path-activity (1) of four Android components and androidactivity

1. What is Activity?

Activity is simply an interface. Every interface we see on the Android phone is an activity.


Ii. Create an Activity

1. Define a class to inherit the activity, and register the activity under the application node of the manifest. xml file in the list file. The activity is created successfully.

public class MyActivity extends Activity {}


2. register the activity in the inventory file
<application        android:allowBackup="true"        android:icon="@drawable/ic_launcher"        android:label="@string/app_name"        android:theme="@style/AppTheme" >        <activity            android:name="com.example.study.MainActivity"            android:label="@string/app_name" >            <intent-filter>                <action android:name="android.intent.action.MAIN" />                <category android:name="android.intent.category.LAUNCHER" />            </intent-filter>        </activity>                <activity android:name="com.yang.study.MyActivity" >        </activity>    </application>
MainActivity is the activity started by the program, which can be seen from the action of intent-filter.

The activity we define is the following MyActivity.

 <intent-filter>                <action android:name="android.intent.action.MAIN" />                <category android:name="android.intent.category.LAUNCHER" />            </intent-filter>
Note: Each application can create multiple icons in the launcher or android mobile phone starter. You only need to configure the intent-filter in the activity, of course, no one is doing this, and multiple application icons may cause user problems.


3. Start Activity

Since the application only has one entry to start, how can other activities be started?

Activity can also be started in two ways. The first is to display the startup, and the second is to start the activity implicitly.


A. Start the activity display

It indicates that the class to be started is specified when the intent (intent) is created.

1. Create intent

2. startActivity (intent)

You can use the setclass method to specify a class.

Intent intent = new Intent();intent.setClass(MainActivity.this, MyActivity.class);startActivity(intent);
Or setclassname to specify the class name

Intent intent = new Intent();intent.setClassName(MainActivity.this, "com.yang.study.MyActivity");startActivity(intent);
You can also specify classes when constructing intent directly.

Intent intent = new Intent(MainActivity.this, MyActivity.class);startActivity(intent);
All three forms can display the startup activity


Tips: Can I start the activity of other applications in one application?

The answer is yes. As long as the started activity is set to true in the export attribute in the configuration of the list file, it can be started in other applications. The method is as follows:

Intent intent = new Intent();intent.setClassName("com.yang.demo", "com.yang.demo.MainActivity");startActivity(intent);
The first parameter of the setClassName method is the package name of the application to be started, and the second parameter is the class name to be started.


B. Implicit startup of activity

The implicit intention is to start the activity by matching the action and data instead of specifying the class to be started at startup.

Eg. Enable the call interface implicitly

Intent intent = new Intent();intent.setAction(Intent.ACTION_CALL);intent.setData(Uri.parse("tel://18888009988"));startActivity(intent);
Start the browser implicitly:

Intent intent = new Intent();intent.setAction(Intent.ACTION_VIEW);intent.setData(Uri.parse("http://www.baidu.com"));
<span style="font-family: Arial, Helvetica, sans-serif;">startActivity(intent);</span>
The two examples above are some built-in interfaces for starting the system. There are many default interfaces for the system. You can refer to the documents on the Internet. Due to space limitations, I will not list them here.


If our activity is a browser tool, when we use the above implicit launch browser, we also hope that our activity will be started. What should we do?

At this time, we only need to set the intent-filter intent filter in the avtivity list file. When we use the above implicit browser startup method, our own defined activity will also be started.

Example:

<activity            android:name="com.yang.study.MyActivity"            android:exported="true" >            <intent-filter>                <action android:name="android.intent.action.VIEW" />                <data android:scheme="http" />                <category android:name="android.intent.category.DEFAULT" />            </intent-filter>        </activity>

In the configuration file, set the intent-filter scheme parameter to http so that all actions starting with android. intent. action. VIEW data can be intercepted.

When we use an implicit intention to start the activity, our own activity will also be started.

Intent intent = new Intent();intent.setAction(Intent.ACTION_VIEW);intent.setData(Uri.parse("http://www.baidu.com"));startActivity(intent);



4. activity display

Activity is an interface that users can see. How can we define the content to be seen? It is very simple. In the oncreat () method of activity, we can set the content to be displayed.

protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);}
SetContentView () method to set the interface to be displayed by the user, which is placed in the ayout layout file to be displayed.


5. Disable activity

Acitivty closes and calls the finish () method of activity directly.

protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);finish();}


This article briefly introduces the startup and shutdown of the activity. The key content is the display and implicit startup methods. The frequency of use is also very high and it is necessary to know.




















What are the four main components of Android? How do you understand them?

Android has four components: Activity, Service, Broadcast explorer, and Content Provider.

Activity

It is really difficult to build a complete Android program and don't want to use Activity, unless you want to be crazy. Because Activity is a window for interaction between Android apps and users, in my opinion, from this perspective, Android Activity is a webpage of a website.

Activity is undoubtedly the most complex among the four main components. During the past few years, a hook between something and the interface cannot be simplified. Think about it, you can figure out how much time it takes to create an independent application on the interface. In terms of visual effects, an Activity occupies the current window, responds to all window events, and has interface elements such as controls and menus. From the internal logic point of view, the Activity needs to do a lot of persistent things to maintain the status of each interface. It also needs to properly manage the lifecycle and some switch logic. For developers, they need to derive a subclass of the Activity, and then work hard to do the above. For more details about the Activity, see reference/android/app/Activity.html. In the future, we will provide a more detailed analysis.

Service

Service, in the most straightforward view, is to strip the Activity of the interface. They are very similar in many Android concepts and are encapsulated with a complete functional logic implementation, however, the Service is not exposed, but it is a solid backing silently.

But in fact, from another perspective, the services in Android are similar to the Windows Services and Web Background services that we usually call. These services are usually used for long running of the background and accept upper-layer commands, the module that completes related transactions. In running mode, Activity jumps from one to the other ..., this is a bit like a modal dialog box (or a web page ...), give an input (or no ...), then, regardless of whether you want it to run, the output (same as or without...) is returned when it leaves ...).

The Service is not, it is waiting, waiting for the upper layer to connect to it, and then generate a persistent and lingering communication, which is like an Ajax page, watching nothing changed, sneaky and Service, I don't know how many times it went.

However, unlike general services, the process model of Android services is configurable like all four components, both the caller and the producer can choose whether to run the component in the same process or in different processes. This sentence can be engraved into your mind, which highlights the running characteristics of Android. If a Service is expected to run in different processes of the caller, you need to use the RPC mechanism provided by Android to deploy a set of inter-process communication policies for it.

Shows the RPC implementation of Android, as shown in (well, it also comes from the SDK ...), nothing unusual. Based on an implementation of the proxy mode, a proxy class is generated on both the caller and the server side for serialization and deserialization, this allows both the caller and server to use the RPC interface just like calling a local interface.

The class used for data serialization in Android is Parcel. For details, see/reference/android/OS/Parcel.html. This class encapsulates the serialization details and provides an access interface that is sufficiently object-oriented, android is said to be very efficient.

There is also the Android Interface Definition Language (AIDL), an Interface-defined Language, and the RPC Interface of the service, which can be described by AIDL, the ADT can help you automatically generate a complete set of classes required for the proxy mode, which is the kind of kind that is hard to write. For more information, see guide & #47 ...... the remaining full text>

What are four components of android?

Android has four components: Activity, Service, Broadcast explorer, and Content Provider.

The communication between these components is mainly completed with Intent assistance.
Intent describes the actions, actions involving data, and additional data of an application. Android identifies the corresponding components based on the description of the Intent, pass Intent to the called component and complete the call of the component.
Therefore, Intent acts as a media intermediary here, providing information about component calls to each other to decouple callers from callers.
For example, in an application maintained by a contact, when we click a contact on a contact list screen (assuming the corresponding Activity is listActivity, you want to jump out of the contact's detail screen (assuming the corresponding Activity is detailActivity)
To achieve this purpose, listActivity needs to construct an Intent, which is used to tell the system that we want to perform the "View" action. The corresponding viewing object of this action is "a contact ", call startActivity (Intent intent ),
The constructed Intent is passed in. The system will find the Activity that meets the Intent requirements in ManiFest according to the description in this Intent. The system will call the Activity that is found, that is, detailActivity, and finally pass in the Intent, detailActivity performs the corresponding operation based on the description in the Intent.

If you agree with my answer, click the "select as satisfactory answer" button below. Thank you!

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.