Android Otto framework

Source: Internet
Author: User
Tags vector font

Android Otto framework

Today, we will introduce a framework Otto which Android event bus EventBus mode is widely used in android.

Otto: http://square.github.io/otto/


1. Configure Otto in Android Studio (directly download jar package import in Eclipse)

Like other frameworks described earlier, it simply needs to configure the following red line in build. gradle.

Dependencies {
Compile fileTree (dir: 'libs', include: ['*. jar'])
Compile 'com. android. support: appcompat-v7: 19. +'
//Otto dependent packages
Compile 'com. squareup: otto: +'

}


Ii. Analysis of Otto event bus framework

1. Why the Otto framework?

The main function of the Otto framework is to help us reduce the Coupling Degree (decoupling) between multiple classes ).

For example, if A wants to operate A method in B between Class A and Class B.


Traditional Method: A directly calls the method of object B (coupled together)

Event bus mechanism: A does not need to call class B methods, but only needs to generate and issue an "Event Notification". If B subscribes to this "Event"

Then it will accept this event and perform corresponding operations. This will be decoupled.


2. Use the Otto framework (in combination with the code Introduction)

The Otto framework is easier to understand than the previous open-source Android frameworks.

It mainly applies to one class: Bus class (used for registration class, logout class, publish event)

Two Annotations: @ Subscribe (subscription) @ Produce (production) [All annotations for "events]




For convenience, I have not used two classes for this Demo. However, if you want to try it, remember that

Both the publisher and subscriber classes must use Bus for class registration and cancellation. Otherwise, it cannot be recognized by the Bus.

In the Demo below, all the classes that subscribe to the event will receive the event and accept the order after the "event" is generated, beyond our control!

Public class MyActivity extends ActionBarActivity {@ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_my); findViewById (R. id. button_change ). setOnClickListener (new View. onClickListener () {@ Override public void onClick (View view) {BusProvider. getBusInstance (). post (new DataChangedEvent ("this is changed String"); // release event }}) ;}@ Override protected void onResume () {super. onResume (); BusProvider. getBusInstance (). register (this); // register} @ Override protected void onPause () {super. onPause (); BusProvider. getBusInstance (). unregister (this); // logout} @ Subscribe // Subscribe event DataChangedEvent public void sayGoodOnEvent (DataChangedEvent event) {Log. e ("event", "good") ;}@ Subscribe // Subscribe event public void sayBadOnEvent (DataChangedEvent event) {Log. e ("event", "bad") ;}@ Produce // generate the event public DataChangedEvent produceDataChangedEvent () {return new DataChangedEvent ("this is changed String ");}}


For adding a service in the Android framework

Refer to the examples in framework/base/services/java/com/android/server. Generally, a server is composed of xxxxService. java xxxxManager. java IxxxxManager. aidl. Manager and aidl are in frameworks/base/core/java/android /. If you want to run the system at startup, you need to add the code to start your Service in SystemServer. java.

What is the five application development frameworks of android?

The android Application Development Framework consists of five parts: Linux Kernel, Android Runtime, Libraries, Application Framework, and Applications. The second part will detail these five parts. The bottom-up analysis layers are as follows.

Android Architecture

1. Linux Kernel

Android provides core system services based on Linux 2.6, such as security, memory management, process management, network stack, and driver model. Linux Kernel is also an abstraction layer between hardware and software. It hides specific hardware details and provides unified services for the upper layer. If you have learned computer networks and know OSI/RM, you will know that the benefit of layering is to use the services provided by the lower layer to provide unified services for the upper layer, shielding the differences between the current layer and the following layers, when the current layer and the following layers change, the upper layer is not affected. That is to say, each layer performs its own duties, and each layer provides a fixed SAP (Service Access Point). The professional points can be said to be high cohesion and low coupling. If you are only engaged in application development, you do not need to have a deep understanding of the Linux Kernel layer.

2. Android Runtime

Android contains a collection of core libraries, providing most of the functions available in the core library of Java programming language. Every Android application is an instance in the Dalvik Virtual Machine and runs in its own process. The Dalvik virtual machine is designed to efficiently run multiple virtual machines on one device. The Dalvik Virtual Machine executable file format is. dex. dex is a compression format designed for Dalvik, suitable for systems with limited memory and processor speed. Most virtual machines, including JVM, are stack-based, while Dalvik virtual machines are register-based. The two architectures have their own advantages and disadvantages. Generally, the stack-based machine requires more commands, while the register-based machine requires more commands. Dx is a set of tools that can convert Java. class to. dex format. A dex file usually has multiple. classes. Because dex sometimes has to be optimized, it will increase the file size by 1 to 4 times and end with ODEX. The Dalvik Virtual Machine relies on the Linux kernel to provide basic functions, such as thread and underlying memory management.

3. Libraries

Android contains a collection of C/C ++ libraries for various components of the Android system. These functions are exposed to developers through the Android application framework. The following lists some core libraries: system C library-the BSD derivative of the Standard C system library (libc), adjusted to the embedded Linux device media library-The PacketVideo-based OpenCORE. These libraries support playing and recording many popular audio and video formats, as well as static image files, including MPEG4 and H. 264. Manage MP3, AAC, AMR, JPG, and PNG interfaces-manage the access display subsystem and seamlessly combine the 2D and 3D graphics layers LibWebCore of multiple applications-a new Web browser engine, driver Android browser and Embedded web View SGL-Basic 2D graphics engine 3D library-Based on OpenGL ES 1.0 APIs implementation. Libraries use hardware 3D acceleration or include highly optimized 3D software grating FreeType -- bitmap and vector font rendering SQLite -- powerful and lightweight relational database engines available for all applications

4. Application Framework

By providing an open development platform, Android enables developers to develop extremely rich and novel applications. Developers can freely take advantage of the hardware advantages of devices, access location information, run background services, Set alarms, and add notifications to the status bar. Developers can fully use the APIs framework used by core applications. Architecture of the application ...... the remaining full text>

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.