Ten, the Nineth day of Android Learning--Summary

Source: Internet
Author: User

(Transferred from: http://wenku.baidu.com/view/af39b3164431b90d6c85c72f.html)

Ten, the Nineth day of Android Learning--Summary

Through this period of study, this evening to make a small summary ~ ~ ~

As early as we learned, Android four important parts:

First, activity--façade, is the user interface we see

Ii. intent--data transfer across the application

Third, service--not visible, for the entire application to provide a service support

Iv. Content provider--interface to provide data for applications

At this point, we have already had a preliminary understanding of these four important parts. But not every Android application needs these four pieces, which is not required. At some point, we only need several of these four kinds of combinations into our application.

Activity

At this point, the activity we have been very clear, we need to pay attention to the use of activity, need to register in the manifest.xml.

Intent

Android provides the intent mechanism to assist interaction and communication between applications, intent is responsible for describing the actions, data, and additional data of one operation in the application, and Android is responsible for locating the corresponding components based on these intent descriptions. Pass intent to the calling component and complete the call to the component.

Intent can be used not only between applications, but also between Activity/service within an application. As a result, intent here is the role of a media intermediary that specifically provides information about the components that are called to each other, enabling decoupling between the caller and the callee.

One of the keys to understanding intent is to understand the two basic uses of intent:

1, the explicit intent--is to construct the intent object when the designated recipient

2, implicit intent--in the construction of intent objects, do not know and do not care who is the recipient, in favor of reducing the sender and the recipient of the coupling.

Here are some common uses of intent:


Intent Intentone = new Intent (Intentactivity.this,
Resultactivity.class);
IntentActivity.this.startActivity (Intentone);

------------------------------------------------------------------------


Intent intenttwo = new Intent (Intentactivity.this,
Resultactivity.class);

Put the data into bundles and pass them on.
Bundle bundle = new bundle ();
Bundle.putstring ("name", "Chenzheng");
Intent.putextra can be used to place key value pairs
Intenttwo.putextras (bundle);
IntentActivity.this.startActivity (Intenttwo);

------------------------------------------------------------------------

// .............. For data reception ..... .......

Bundle Resultbundle = Getintent (). Getextras ();
String name = resultbundle.getstring ("name");

------------------------------------------------------------------------


Intent resultintent = Getintent ();
Bundle Resultbundletwo = new bundle ();
Resultbundletwo.putstring ("name", "This was from showmsg");
Resultintent.putextras (Resultbundletwo);
Setresult (RESULT_OK, resultintent);

Service

Service is an application component

Service does not have a graphical interface

Service is typically used to handle long-consuming operations such as downloading, playing MP3 files, and more.

You can use service update ContentProvider, send intent and start system notifications, and more.

We should note that the service is not a separate process, nor is it a thread.

Don't forget to write a service to register in Manifest.xml.

ContentProvider

Applications can save their data to a file, to a SQL database, or even to any valid device. Content provider becomes useful when you want to share your data with other apps. A content provider class implements a standard set of methods that enable other applications to save or read the various data types processed by this content provider.

--------------------------------------------------------------------------

Toast

I have been interested in this toast, so I found an example from the Internet to practice the next, and share with you:

In the following example, there are five kinds of toast usage

First type: Default Style

The default toast style
Toast.maketext (toastactivity.this, "Default Toast style", Toast.length_short)
. Show ();

The results of the operation are as follows:

The second type: Custom display location

Toast with custom location
Toast toast = Toast.maketext (Toastactivity.this, "toast at custom location",
Toast.length_long);
Customize where toast is displayed
Toast.setgravity (gravity.center, 0, 0);
Toast.show ();

The results of the operation are as follows:

The third type: toast with pictures

Toast with a picture
Toast toast = Toast.maketext (Toastactivity.this, "toast with pictures",
Toast.length_long);
Set where toast is displayed
Toast.setgravity (gravity.center, 0, 0);

LinearLayout Toastview = (linearlayout) Toast.getview ();
System.out.println ("Getapplicationcontext:"
+ Getapplicationcontext ());

ImageView imagecodeproject = new ImageView (Getapplicationcontext ());
Find the picture file that needs to be displayed
Imagecodeproject.setimageresource (R.drawable.mickey);
Toastview.addview (imagecodeproject, 0);
Toast.show ();

The results of the operation are as follows:

Fourth type: fully customizable toast

Fully customizable toast, every option requires us to manually set
Layoutinflater inflater = Getlayoutinflater ();
View layout = Inflater.inflate (R.layout.custom,
(ViewGroup) Findviewbyid (r.id.lltoast));
ImageView image = (ImageView) layout
. Findviewbyid (R.id.tvimagetoast);
Image.setimageresource (R.drawable.mickey);
TextView title = (TextView) Layout.findviewbyid (r.id.tvtitletoast);
Title.settext ("Attention");
TextView Text = (TextView) Layout.findviewbyid (r.id.tvtexttoast);
Text.settext ("Fully customizable toast");
Toast toast = new Toast (Getapplicationcontext ());
Toast.setgravity (Gravity.right | Gravity.top, 12, 40);
Toast.setduration (Toast.length_long);
Toast.setview (layout);
Toast.show ();

The results of the operation are as follows:

Fifth type: Other threads

Toast for other threads
New Thread (New Runnable () {

@Override
public void Run () {
Showtoast ();
}
}). Start ();

public void Showtoast () {
Handler.post (New Runnable () {
@Override
public void Run () {
Toast.maketext (Getapplicationcontext (), "I'm from another thread!" ",
Toast.length_short). Show ();

}
});
}

The results of the operation are as follows:

Ten, the Nineth day of Android Learning-summary (Turn)

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.