Interactive communication in Android game development

Source: Internet
Author: User

Interactive communication in Android game development

On the Android platform, applications can conveniently call functions of other applications to meet their functional requirements. This is one of the features of Android, this article will introduce you to the interactive communication methods within or between Android programs.

1. Basic Components of Android applications

The basic components of the Android application include Activity, Service, BroadcastReceiver, and ContentProvider. Different components have different features and their declaration cycles. Next, we will give a brief introduction to each component.

(1) Activity components

Activity is the most common Android component. Every Activity is equivalent to a screen, providing users with a visual interface for interaction. Applications can contain multiple active activities as needed. These activities generally inherit from the Activity classes under the android. app package, and the operations between these activities are independent of each other. The lifecycle of an Activity consists of three stages.

1) running state)

At this time, the Activity is displayed on the front-end of the screen and has a focus, which can interact with user operations.

2) paused state)

In this case, the Activity loses focus and is replaced by other running Activity on the front-end of the screen. The paused Activity retains its status, members, and other information. When the system memory is very scarce, the paused Activity is terminated to obtain more resources.

3) stopped state)

The Activity in the Stop State has no focus and is completely invisible. Although it retains the status and member information, the Activity in the Stop State will be terminated as needed by the system.

When the Activity switches between different States, you can rewrite the corresponding callback method to write the action that should be executed when the status changes.

(2) Service Components

The Service does not provide a presentation layer for interaction with users. A Service is an Android component running in the background. When an application requires computing or data processing that does not need to be displayed in the foreground, it can start a Service, each Service is inherited from android. service class under the app package.

The Service is generally started by Activity or other Context objects. After the Service is started, the Service will run in the background, even if the lifecycle of the Activity or other components that start the Service has ended, the Service continues until its lifecycle ends. Each Service should be declared in AndroidMainfest. xml. There are two ways to start the Service, and the corresponding lifecycle is also different.

(1) start using the startService method. When the system calls the startService method, if the Service is not started, the onCreate method and onStart method are called in sequence to start the Service. When other Context objects call the stopService method, the Service calls the self-claimed stopSelf or stopService method, the Service Execution is stopped.

(2) start using the bindService method. When the system calls the bindService method, if the Service is not started, it will call its onCreate method to complete initialization, and then bind the Service to the Context object (such as Activity, when the bound Context object is destroyed, the Service associated with it stops running.

Although there are two ways to start the Service, you can bind the Service to the Context object no matter how it is started.

(3) Broadcast Receiver component

Broadcast Receiver does not provide a presentation layer for interaction with users. It is a component responsible for receiving Broadcast messages and responding to messages. There are many such broadcasts in the Android system. For example, when the battery is too low or the signal is too low, the system will send 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 in the Android. content package.

1) BroadcastReceiver publishing Method

It is easier to publish a broadcast. Create an Intent object as needed, encapsulate the information content and the information used for filtering, and call Context. sendBroadcast method, Context. sendOrderedBroadcast method or Context. the sendStickyBroadcast method broadcasts the Intent object. The differences between the two methods are as follows.

Generally, the Intent sent by sendBroadcast or sendStickyBroadcast is used to execute the onReceive method for all BroadcastReceiver that meets the conditions. However, if there are multiple BroadcastReceiver that meet the conditions, the order in which the onReceive method is executed is not guaranteed. The Intent sent by using the sendOederedBroadcast method executes the onReceive method based on the priority set by IntentFilter during BroadcastReceiver registration. The order in which BroadcastReceiver of the same priority executes the onReceiver method is not guaranteed.

The main difference between sendStickyBroadcast and sendStickyBroadcast is that Intent will always exist after being sent, and the Intent object will be directly returned to the newly registered Receiver when registerizer is called to register a Matched Receiver.

2) BroadcastReceiver receives broadcast messages

The published broadcast entity is Intent. When receiving the broadcast, you must use the IntentFilter object to filter. The life cycle of BroadcastReceiver is relatively simple. There is only one callback method-onReceive, which is called when the application receives a message sent to its own broadcast. Therefore, BroadcastReceiver is relatively simple to use, you only need to properly override the onReceive method and register the Broadcast Receiver ER in the appropriate place.

Declare it in the AndroidMainfest. xml file. The registration information is included in Label, and Set filter rules in the tag.

Create and set the IntentFilter object in the code. This IntentFilter object contains a filter rule for broadcast, and then calls Context where necessary. registerReceiver method and Context. the unregisterReceiver method is used to register and cancel registration. If this method is used for registration, the BroadcastReceiver will no longer exist when the Context object is destroyed.

3) Context Provider component

Context Provider and other application components are quite unnecessary. Context Provider is mainly used for data sharing between unused applications. On the Android platform, each application has an independent memory space. If an application needs to use data from other applications, the ContextProvider object must be used.

Each ContextProvider inherits from Android. the ContextProvider class in the context package provides external applications with their own data. The provided data can be stored as Android files, SQLite database files, or other legal formats.

ContextProvider provides interfaces for data and data access. The ContextResolver object is used to access data. This object can communicate with the ContextProvider object to share data.

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.