Interactive communication for Android game development

Source: Internet
Author: User

Under the Android platform, applications can easily invoke the functionality of other applications to implement their own functional requirements, which is one of the features of Android, and this article will show readers how to communicate interactively within or between programs.

Basic components for 1.Android applications

The basic components of an Android application include activity, Service, Broadcastreceiver, and ContentProvider, and different components have different features and their own declaration cycles, and a brief introduction to each component follows.

(1) Activity components

Activity is the most common type of Android component, with each activity being a screen that provides the user with a visual interface for interacting. An application can include a live multiple activity as needed, which typically inherits from the activity class under the Android.app package, and the activities run independently of each other. The life cycle of an activity consists of 3 phases.

1) operation (running state)

The activity appears in the foreground of the screen and has the focus to interact with the user's actions.

2) Paused (paused state)

At this point, the activity loses focus and is replaced by other operating state activity in the screen foreground, the suspended state activity retains its state and members, and other information, when the system's memory is very scarce, suspended state activity will be ended to obtain more resources.

3) Stop states (stopped state)

Inactive activity not only has no focus, but is completely invisible, although it also retains information such as status and members, the inactive activity will be ended when the system needs it.

When activity switches between different states, you can write the actions that should be performed when the state changes by overriding the appropriate callback method.

(2) Service Components

The service does not provide a presentation layer to interact with the user. A service is an Android component that runs in the background, and when an application needs to do some computation or data processing that does not require a foreground display, it can start a service to complete, and each service inherits from the service class under the Android.app package.

The service is typically started by an activity or other context object, and when the service is started, the service will run in the background, even if the service activity or other component's life cycle has ended, The service continues to run until the end of its own life cycle. Each service should be declared in Androidmainfest.xml. There are two ways to start a service, and the corresponding life cycle is different.

(1) Start with the StartService method. When the system calls the StartService method, if the service has not yet started, call its OnCreate method and the OnStart method to start it in turn. Service execution is stopped when other context objects call the StopService method, the service calls a self-proclaimed stopself, or StopService method.

(2) Start with the Bindservice method. When the system calls the Bindservice method, if the service is not started, its OnCreate method is called to complete the initialization work, and then the service and the context object (such as activity) are bound. When the bound context object is destroyed, the service tied to it will also stop running.

It is important to note that, although there are two ways to start a service, it can be bound to a context object regardless of how the service was started.

(3) Broadcast receiver Components

Broadcast receiver also does not provide a presentation layer that interacts with the user, which is a component responsible for receiving broadcast messages and reacting to messages. There are many such broadcasts in Android systems, such as when the battery is too low or the signal is too low, and the system sends a broadcast notification.

If the application needs to respond to a broadcast message, it should register the corresponding Broadcastreceiver object, which inherits from the Broadcastreceiver class, which is located under the Android.content package.

1) How Broadcastreceiver publishes the broadcast

Publishing a broadcast is easier, creating a intent object where needed, encapsulating the content of the information and the information used to filter it, by calling the Context.sendbroadcast method, The Context.sendorderedbroadcast method or Context.sendstickybroadcast method broadcasts the intent object, and the difference between fading out the broadcast mode is as follows.

Typically, a intent that is sent out using Sendbroadcast or Sendstickybroadcast will execute its OnReceive method for all broadcastreceiver that meet the criteria. However, if there are multiple broadcastreceiver that satisfy the conditions, the order in which the OnReceive method is executed is not guaranteed. The intent, which is sent out by the Sendoederedbroadcast method, executes the OnReceive method according to the order of precedence Intentfilter set at Broadcastreceiver registration time. The order in which the broadcastreceiver of the same priority executes the Onreceiver method is not guaranteed.

Sendstickybroadcast The main difference is that the intent will persist after sending, and the intent object will be returned to the newly registered receiver directly at a later call to the Registerreceiver registered receiver.

2) How the Broadcastreceiver receives the broadcast

The broadcast entity that is published is intent, so it needs to be filtered by the Intentfilter object when it receives the broadcast. Broadcastreceiver life cycle is relatively simple, there is only one callback method--onreceive, the method in the application received to send their own broadcast when the call, so broadcastreceiver the use of the method is relatively simple, Only a reasonable rewrite of the OnReceive method is required, and the broadcast receiver can be registered where appropriate.

Declared in the Androidmainfest.xml file. The registration information is included in the <receiver></receiver> tab and the filter rules are set in the <intent-filter> tab.

Create and set the Intentfilter object in your code. The Intentfilter object contains the filtering rules for the broadcast, And then need to call the Context.registerreceiver method and Context.unregisterreceiver method to register and unregister, if registered in this way, when the context object is destroyed, the Broadcastreceiver also No longer exists.

3) Context Provider components

Context provider and other application components are largely unused, and the context provider is primarily used for data sharing between unused applications. Under the Android platform, each application has a separate memory space, and if an application needs to use data from other applications, the Contextprovider object must be used.

Each contextprovider is inherited from the Contextprovider class under the Android.context package, and its function is to provide its own data for use by external applications, providing data that can be stored as Android files, SQLite database file or other legitimate format.

Contextprovider provides data and access to the data interface, the real access to the data is the Contextresolver object, the object can communicate with the Contextprovider object to achieve the purpose of sharing data.

Interactive communication for Android game development

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.