Android Study Notes (3)

Source: Internet
Author: User

Android Study Notes (3)

ContentProvider Introduction

ContentProvider is a standard API for data exchange between different applications. When an application needs to expose its own data to other programs for use, this application can be implemented by providing ContentProvider, other applications can use ContentResolver to manipulate the data exposed by ContentProvider. If an application exposes its own data operation interface through ContentProvider, other applications can use this interface to operate internal data of the application regardless of whether the application is started, this includes adding, deleting, modifying, and querying data.

Android: authorities attributes

The defined ContentProvider class must be declared in AndroidManifest. xml before use. The authorization attribute must be added to the Declaration.

Android: authorities: Specifies the domain name for the ContentProvider. The authority of Content: // authority/optionalPath/optionalId in the content uri is the value specified by the android: authorities attribute. The property value is defined as the package name + "." + The ContentProvider name.

Service itself has two problems

1. The Service does not start a separate process. The Service and its application are in the same process.

2. The Service is not a new thread. Therefore, time-consuming operations should not be processed directly in the Service. A new thread should be enabled for time-consuming operations.

Handling time-consuming tasks

If the development requires processing time-consuming tasks in the Service module, a new thread is generally started in the Service, but cannot be processed by the promoter thread in other application components, because the Activity may be exited by the user, and the life cycle of BroadcastReceiver is very short, it is very likely that the sub-thread has not ended, the Activity has been exited by the user, or the BroadcastReceiver has ended and there is a null thread.

Introduction to IntentService

IntentService can make up for two defects of the Service: IntentService uses the queue to manage the Intent of the request. When the client code initiates the IntentService through the Intent request, IntentService adds the Intent to the queue, start a new worker thread to process the Intent. For Asynchronous startService () requests, IntentService processes the Intent in sequence. This thread ensures that only one Intent is processed at the same time. Because IntentService uses a new worker thread to process Intent requests, IntentService does not block the main thread, so IntentService can process time-consuming tasks by itself.

Static code block

That is, the class does not contain any static code block of the method body. Its format is static {...}. This static code block is executed only once when the class is loaded. Static code blocks are generally used to initialize class attributes.

GetResources (). getStringArray (...) Function

GetResources (). getStringArray (R. array. setting) is a method to obtain array resources in an application. If you want to use the array resources defined in the application, you need to use getResources (). getStringArray (R. array. *). The return value is an array.

Example of the getXxx () method of the TelephonyManager service class

1. TelephonyManager. getDeviceId () // obtain the device ID

2. TelephonyManager. getDeviceSoftwareVersion () // obtain the version of the system platform

3. TelephonyManager. getNetworkOperator () // get the network operator code

4. TelephonyManager. getNetworkOperatorName () // obtain the Network Carrier name

5. TelephonyManager. getPhoneType () // obtain the network type of the mobile phone.

6. TelephonyManager. getCellLocation () // obtain the device location

7. TelephonyManager. getSimCountryIso () // country to which the SIM card is obtained

8. TelephonyManager. getSimSerialNumber () // obtain the SIM card serial number

9. TelephonyManager. getSimState () // get SIM card status

The returned values are of the String type.

Introduction to PhoneStateListener

PhoneStateListener is a call status listener. The onCallStateChanged (intstate, String number) method must be implemented when the listener is defined. The main static constant of the call status: int CALL_STATE_IDLE // idle status. There is no activity int CALL_STATE_OFFHOOK // The status of the disconnection. There is at least one call activity, which can be called or called, or on hold. And no call is in the ringing or waiting intCALL_STATE_RINGING // call status. During the time when the call rings, or when the call is in progress, the new call has to wait.

How to obtain the SMS Manager

SmsManager sManager = SmsManager. getDefault (); // obtain the SmsManager of the application using this method.

Introduction to PendingIntent objects

The PendingIntent object is a packaging of the Intent. Generally, the PendingIntent object is obtained by calling the getActivity (), getService (), and getBroadcastReceiver () Static Methods of PendingIntent. Unlike the Intent object, PendingIntent is usually passed to other application components to execute the Intent encapsulated by PendingIntent by other applications.

Introduction to BroadcastReceiver

BroadcastReceiver is a system-level listener and has its own processes. As long as a matching Intent is broadcast, BroadcastReceiver can be stimulated. When it is triggered, the onReceive () method is automatically triggered. After the onReceive () method is executed, the BroadcastReceiver instance is destroyed. Different from the Activity component, when the system starts a specified Activity component through Intent, if the appropriate Activity component is not found, the program will terminate abnormally. However, when the system fires BroadcastReceiver through Intent, if you cannot find the proper BroadcastReceiver component, the application will not have any problems. In addition, you cannot perform time-consuming operations in onReceive (). You can use Intent to start a Service to complete this operation. However, you cannot use a new thread to perform time-consuming operations. Because the life cycle of BroadcastReceiver itself is very short, the subthread may not end yet, And BroadcastReceiver has exited.

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.