Android Learning Note Five

Source: Internet
Author: User

Two other components of Android: Broadcastreceiver and service.

First, Broadcastreceiver broadcast receiver, broadcast is the way to pass events in Android, there are two aspects: Broadcast sender and broadcast receiver.

1, the broadcast sender can send two kinds of broadcasts: synchronous broadcast and orderly broadcast , the difference is

① the difference in the order of reception: Synchronous broadcast synchronous reception, ordered broadcasts can set the receive order by specifying the priority level

The difference between whether the ② can be interrupted: Because ordered broadcasts are sequential, it is possible to interrupt the broadcast and not allow subsequent receivers, while synchronous broadcasts cannot be interrupted.

2. Broadcast receiver: The specific content sent by the broadcast is an implicit intent object (because it is usually used for another application), and the receiver implements it in two steps.

The ① is configured in the manifest file,

② inherits the Broadcastreceiver object and overrides the OnReceive method, in which the functionality can be implemented.

Ordered broadcasts determine the order of receipt by specifying a priority, from -1000~1000,

① if the same priority is in the same app, the first one in the manifest file receives the

② if the priority in different apps is the same, the first installed app receives

Reference: http://blog.csdn.net/xiazdong/article/details/7768807,

Second, service, services are no interface and long-term operation in the background of the program.

1. In Android, pressing the back button to exit an app does not (when memory is sufficient) destroy a process directly, so that the child threads can also run in the background, the difference between such threads and services is different priority, if memory is low, the order of destruction is different. And even if the service's process is destroyed, it will automatically rerun if the memory is sufficient. For Android process content reference: Http://mobile.51cto.com/aprogram-459724.htm.

As one of the four components of Android, the service inherits the service class and is configured in the manifest file to be used.

2, there are two ways to open Service,① use StartService (); ② use Bindservice (). The difference between the two is

① The former creates a service, it is not associated with the service, because there is no way to get an instance of the service. However, the connection requires complex operations and also distinguishes between in-process binding and out-of-process bound remote services

② The service is turned on by binding, and once the activity that invokes the service is destroyed, it is destroyed. That service cannot exist for a long time.

1 Importandroid.app.Activity;2 ImportAndroid.content.ComponentName;3 Importandroid.content.Intent;4 Importandroid.content.ServiceConnection;5 ImportAndroid.os.Bundle;6 ImportAndroid.os.IBinder;7 ImportAndroid.view.View;8 9  Public classMainactivityextendsActivity {Ten     PrivateMyConn Conn; One     PrivateImiddleperson MP; A  - @Override -     protected voidonCreate (Bundle savedinstancestate) { the         Super. OnCreate (savedinstancestate); - Setcontentview (r.layout.activity_main); -     } -      + @Override -     protected voidOnDestroy () { +SYSTEM.OUT.PRINTLN ("Ah ah, I'm activity, I'm dead.")); A         Super. OnDestroy (); at     } -  -     //Binding Service -      Public voidbind (view view) { -         //3.activity uses a binding approach to open the service.  -             //Intent to activate a service inIntent Intent =NewIntent ( This, MyService.class); -             //Connect object to keep in touch toconn =Newmyconn (); + Bindservice (Intent, Conn, bind_auto_create); -          the     } *     //Unbind Service $      Public voidUnbind (view view) {Panax Notoginseng Unbindservice (conn); -     } the      +     Private classMyConnImplementsserviceconnection{ A         //4. Called when the service is connected, when the service is successfully bound. IBinder is the return value of the Onbind () of the bound service, the         //!! This is where the contact is . + @Override -          Public voidonserviceconnected (componentname name, IBinder service) { $System.out.println ("succeeded in getting a middleman in the activity.")); $MP =(Imiddleperson) service; -         } -         //call when the service loses connection (normal process hangs, service is killed abnormally) the @Override -          Public voidonservicedisconnected (componentname name) {Wuyi              the         } -     } Wu  -     //invoke the method inside the service.  About      Public voidCall (view view) { $         //5. Invoke the method inside the service through the middleman.  -Mp.callmethodinservice (55); -     } -}
Calling Party
1 ImportAndroid.app.Service;2 Importandroid.content.Intent;3 ImportAndroid.os.Binder;4 ImportAndroid.os.IBinder;5 ImportAndroid.widget.Toast;6 7  Public classMyServiceextendsService {8 9     //2. Implement the code that the service successfully binds, returning an intermediary (a class that meets the IBinder requirements, here is the inner class below). Ten @Override One      Publicibinder onbind (Intent arg0) { ASYSTEM.OUT.PRINTLN ("The service was successfully bound ....") "); -         return NewMiddleperson (); -     } the      - @Override -      Public BooleanOnunbind (Intent Intent) { -System.out.println ("Onunbind"); +         return Super. Onunbind (intent); -     } +      A @Override at      Public voidonCreate () { -System.out.println ("OnCreate"); -         Super. OnCreate (); -     } -      - @Override in      Public intOnstartcommand (Intent Intent,intFlagsintStartid) { -System.out.println ("Onstartcommand"); to         return Super. Onstartcommand (Intent, flags, startid); +     } -      the @Override *      Public voidOnDestroy () { $System.out.println ("Ondestory");Panax Notoginseng         Super. OnDestroy (); -     } the      +     /** A * This is a method of service inside the      */ +      Public voidMethodinservice () { -Toast.maketext ( This, "Haha, service to you to run a temporary residence permit." ", 0). Show (); $     } $      -     //1. First step service to expose methods you have to have an intermediary . -     Private classMiddlepersonextendsBinderImplementsimiddleperson{ the         /** - * Agent Temporary residence permitWuyi          * @paramMoney gives $50 or more to the office.  the          */ -          Public voidCallmethodinservice (intMoney ) { Wu             if(money>=50){ - Methodinservice (); About}Else{ $Toast.maketext (Getapplicationcontext (), "Get ready for some more money." ", 0). Show (); -             } -         } -         /** A * Play Mahjong with the leader +          */ the          Public voidPlaymajiang () { -System.out.println ("accompany the leader to play Mahjong.") "); $         } the     } the}
Service Party

NOTE: The upper-middle class of the service side is private, so it cannot be thrown directly when it is passed out, so an interface is used here as a transit, exposing the method that needs to be exposed through the interface.

Reference: http://blog.csdn.net/ryantang03/article/details/7770939.

3, in addition to binding (this emphasizes that the binding is the case of data sharing, if only to activate a service do not need to write their own services (in the same process), you can also bind the system/others write service. This involves interprocess communication, which is implemented in the Binder form on Android, and provides a lightweight implementation of AIDL. The use of the process is similar to the above, the difference has 2 points:

①service side: External exposure method using Aidl (Android Interface Definition language) instead of interface in Java, except that there is no modifier, eclipse can automatically generate an interface class that can be used for interprocess communication (in the/gen directory)

② caller: In the caller's app, you also need to create the Aidl file in the same way, and then pass the class name. Stub.asinterface (service) to get intermediate objects.

4. Mixed Invocation Service: The service is invoked using both the binding and the Start method. This is to solve the shortcomings of the two methods (start mode can not communicate, binding mode can not be long-lived)

Third, Intent: can be understood as a request or will, can be used to activate Activity,service and broadcast Receiver, the corresponding component as long as the configuration is added intent-filter to get the specified intent.

Intentions are divided into two kinds: explicit, implicit intent. The difference is that the former assigns a specific recipient through the parameters of the constructor, and the intent filter is used to filter the implicit intent, which is typically used to activate its own component, while the implicit intent is usually used to activate the components of another's/system .

A intent can set the action (action), data (Data-->uri), type (type-->mime), Classification (category), additional information (Extras), and a filter matching intent is the need to make three matches, one action, one category, and one data, which can be all or part of a match.

Reference: http://blog.csdn.net/think_soft/article/details/7330095,

http://blog.csdn.net/t12x3456/article/details/7688154,

Http://fenglingdangyang.blog.sohu.com/231856618.html,

Android Learning Note Five

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.