Some basic knowledge of Android

Source: Internet
Author: User

Android Four components

Four components include: Activity (active), service (services), Broadcastreceiver (broadcast receiver), ContentProvider (content provider)

One of their traits is that they need to be registered in Androidmanifest.xml.

1:activity article

An activity is an interface that contains a variety of elements that are used primarily to interact with users.

1.1: How to start activity

1.1.1: Display startup, explicitly indicating the class name of the activity

New Intent (firstactivity.  this, secondactivity. class ); startactivity (intent);

1.1.2: Implicit startup, matching the name and type of an activity

Specify name (action) and type (category) in Androidmanifest.xml

<ActivityAndroid:name=". Secondactivity " >    <Intent-filter>        <ActionAndroid:name= "Com.example.activitytest.ACTION_START" />        <categoryAndroid:name= "Com.example.activitytest.MY_CATEGORY" />    </Intent-filter></Activity>

Code:

New Intent ("Com.example.activitytest.ACTION_START"); Intent.addcategory ("com.example.activitytest.MY_ CATEGORY "); StartActivity (intent) ;
1.2:activity startup mode

Through stack stack management, refer to

1.3:activity How to pass a value

Example of a value-fragment combination, reference

2:activity Life Cycle 2.1: creating

OnCreate, OnStart, Onresume

2.2: Destruction

OnPause, OnStop, OnDestroy

2.3: Life cycle

On current activity A, start activity B, at which point the order of execution is

A onPause, B, 1.1, a onStop

b Call Finish () to end itself, at which point the execution sequence is

B Execution 1.2->a onrestart->a onStart-A Onresume

3: Services (Service)

Service is a solution for running the program in the background of Android, and it is ideal for performing the
Tasks that do not require interaction with the user and require long-term operation. The operation of the service does not depend on any user interface, even if
When the program is switched to the background, or if the user opens another application, the service remains operational.
However, it is important to note that the service is not run in a separate process, but rather relies on the creation of the service
The application process where it resides. When an application process is killed, all services that depend on the process also stop
Operation.

3.1: Create a service

Inherit from the service parent class, remember to register the application node in the Androidmanifest.xml, example

<android:name= ". Media. Bgmusicservice "></service>
3.2: How to start and close the service
Activities. StartService (new Intent (Context, services.  Class); activity. StopService (new Intent (Context, services.  Class));

4:broadcastreceiver (broadcast receiver)

A broadcast receiver can receive broadcasts from the system (such as power-on, network changes, etc.), or receive locally-issued custom broadcasts to handle them.

4.1: Create a broadcast receiver

Inherits from the Broadcastreceiver parent class, implementing the OnReceive method.

4.2: Start a broadcast receiver

Broadcasts can be registered in the Androidmenifest.xml, enabling a static boot (independent of the host lifecycle) or a context-based startup, depending on the purpose of the broadcast receiver

For example, we make a call to belong to the suspended window, it will need to start static, not dependent on the life cycle of the app.

For example, the network changes broadcast, we have an option, we need to frequently access the current network state whether WiFi, and then decide whether to download pictures. Therefore, this broadcast is required, and when the network changes, the new network state is assigned to a global variable.

It is important to note that most broadcast receivers require programs to allow access to certain permissions, do not forget yo ~, such as boot needs receive_boot_completed , network changes need access_network_state

4.2.1: Static start, such as monitoring incoming calls

 <receiverAndroid:name= ". Call. Incomingcallbroadcastreceiver ">            <Intent-filterandroid:priority= "+">                <ActionAndroid:name= "Android.intent.action.PHONE_STATE" />            </Intent-filter>        </receiver>

4.2.2: Manual Start

  New Intentfilter (connectivitymanager.connectivity_action);  Context.registerreceiver (Network status Broadcast receive class, Intentfilter);

Need to sign out manually oh

  Context.unregisterreceiver (receiver);

5:contentprovider content Provider

The Android system provides its own content provider, the most commonly used such as the Address Book, so in the program, we can be in the directory to increase the deletion

We can also create our own content providers.

Paste out an Address Book query with new code

 PackageZhexian.app.smartcall.lib;Importandroid.app.Activity;ImportAndroid.content.ContentResolver;ImportAndroid.content.ContentUris;Importandroid.content.ContentValues;ImportAndroid.content.Context;ImportAndroid.database.Cursor;ImportAndroid.net.Uri;Importandroid.provider.ContactsContract;Importzhexian.app.smartcall.contact.ContactEntity;ImportZhexian.app.smartcall.image.ZImage;Importzhexian.app.smartcall.tools.Utils;Import StaticAndroid.provider.ContactsContract.Data.CONTENT_URI;Import StaticAndroid.provider.ContactsContract.Data.MIMETYPE;Import StaticAndroid.provider.ContactsContract.Data.RAW_CONTACT_ID; Public classZcontact { Public Static Booleanisphoneexists (Context context, String phone) {URI URI= Uri.parse ("content://com.android.contacts/data/phones/filter/" +phone); Contentresolver Resolver=Context.getcontentresolver (); Cursor Cursor= Resolver.query (URI,NewString[]{contactscontract.data.display_name},NULL,NULL,NULL); BooleanIsexist =Cursor.movetofirst ();        Cursor.close (); returnisexist; }     Public Static voidAdd (activity activity, contactentity entity) {contentvalues values=Newcontentvalues (); Contentresolver content=Activity.getcontentresolver (); Uri Rawcontacturi=Content.insert (ContactsContract.RawContacts.CONTENT_URI, values); LongRawcontactid =Contenturis.parseid (Rawcontacturi); //namevalues.clear ();        Values.put (raw_contact_id, Rawcontactid);        Values.put (MIMETYPE, ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE);        Values.put (ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME, Entity.getusername ());        Content.insert (Content_uri, values); //Deptvalues.clear ();        Values.put (Android.provider.ContactsContract.Contacts.Data.RAW_CONTACT_ID, Rawcontactid);        Values.put (MIMETYPE, ContactsContract.CommonDataKinds.Organization.CONTENT_ITEM_TYPE);        Values.put (ContactsContract.CommonDataKinds.Organization.COMPANY, Entity.getcompany ());        Values.put (ContactsContract.CommonDataKinds.Organization.DEPARTMENT, Entity.getdepartment ());        Values.put (ContactsContract.CommonDataKinds.Organization.TITLE, Entity.getjobtitle ()); Values.put (ContactsContract.CommonDataKinds.Organization.TYPE,        ContactsContract.CommonDataKinds.Organization.TYPE_WORK);        Content.insert (Content_uri, values); //Phonevalues.clear ();        Values.put (Android.provider.ContactsContract.Contacts.Data.RAW_CONTACT_ID, Rawcontactid);        Values.put (MIMETYPE, ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE);        Values.put (ContactsContract.CommonDataKinds.Phone.NUMBER, Entity.getphone ()); Values.put (ContactsContract.CommonDataKinds.Phone.TYPE, ContactsContract.CommonDataKinds.Phone.TYPE_WORK_MOBILE        );        Content.insert (Content_uri, values); //Short phonevalues.clear ();        Values.put (Android.provider.ContactsContract.Contacts.Data.RAW_CONTACT_ID, Rawcontactid);        Values.put (MIMETYPE, ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE);        Values.put (ContactsContract.CommonDataKinds.Phone.NUMBER, Entity.getshortphone ());        Values.put (ContactsContract.CommonDataKinds.Phone.TYPE, ContactsContract.CommonDataKinds.Phone.TYPE_WORK);        Content.insert (Content_uri, values); //Avatarvalues.clear ();        Values.put (Android.provider.ContactsContract.Contacts.Data.RAW_CONTACT_ID, Rawcontactid);        Values.put (MIMETYPE, ContactsContract.CommonDataKinds.Photo.CONTENT_ITEM_TYPE); Values.put (ContactsContract.CommonDataKinds.Photo.PHOTO, Utils.convertbitmaptobyte (Entity.getavatarurl (),        Zimage.getinstance (). Getbitmap (Entity.getavatarurl ()));    Content.insert (Content_uri, values); }}
View Code

6:message,handler,message the relationship between Queue,looper.

When threads interact with UI elements, we use handler to avoid thread-safety issues.

The message is used as the handler method SendMessage () parameter, passing through when calling handler.

The message is placed inside the message queue, waiting for the Looper loop to take out, as a parameter callback to its handler Handlemessage method.

Some basic knowledge of Android

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.