<span id="Label3"></p>Service One. Basic knowledge<p><p>Services are generally divided into two types:</p></p><p><p><strong>1: Local Service</strong> for internal Applications.</p></p><p><p>The service can invoke the Context. <strong>StartService</strong> () start, Invoke Context. <strong>StopService</strong> () End.</p></p><p><p>The service can be called Internally. <strong>stopself</strong> () or Service. <strong>Stopselfresult</strong> () to stop Himself. No matter how many times StartService () is called, only one stopservice () is called to Stop.</p></p><p><p><strong>2: remote Service</strong> is used between applications within the Android System. Interfaces can be defined and exposed for other applications to Operate.</p></p><p><p>Call the Context. <strong>Bindservice</strong> The () method establishes the connection and starts to invoke the Context. <strong>Unbindservice</strong> () Close the Connection.</p></p><p><p>Multiple clients can bind to the same service. If the service is not loaded at this time,<strong>bindservice</strong>() loads it First.</p></p><p><p>Available for reuse by other applications, such as defining a weather forecast service, and providing calls to other applications</p></p><p><p></p></p><p><p><strong>The Android service is something that runs in the background, with the same level of Activity. Since the service is running in the background services, then it is not visible, there is no interface of things. You can start a service to play music, record changes in your geographic location, start a service to run, and listen to some kind of action all the Time.</strong></p></p><p><p><strong>Service:</strong> Service is a mechanism for android, and when it runs, if it is a local service, then the corresponding service is running on the main thread of the master Process. Such as: Oncreate,onstart These functions are run on the main thread of the master process when they are called by the System. In the case of remote service, the corresponding service is running on the main thread of the standalone Process. So please do not interpret the Service as a thread, it has no relationship with the thread of half a dime!</p></p><p><p></p></p><p><p>OnCreate () is <span style="color: #ff0000;"><strong>created</strong></span> at the time of invocation, Initialization. Executed only at the time of the first Creation.</p></p><p><p>Onstartcommand () is invoked at <span style="color: #ff0000;"><strong>startup</strong></span> time and executes each time it is Started.</p></p><p><p>Ondestory () is called at the time of <span style="color: #ff0000;"><strong>destruction</strong></span> and generally reclaims resources that are no longer in Use.</p></p><p><p>Register inside the XML file.</p></p><p><p></p></p><p><p>StartService () Open Service</p></p><p><p>StopService () Stop Service</p></p><p><p>Both the StartService () and the StopService () methods are defined in the <span style="color: #ff0000;"><strong>Context</strong></span> class. <span style="color: #ff0000;"><strong>so we can call these two methods in the activity</strong></span> .</p></p><p><p>It is entirely up to the activity to decide when the service will stop (when the button clicks to start the service is on, click Stop Service to STOP)</p></p><p><p>The service stops its own way, calling the <span style="color: #ff0000;"><strong>stopself</strong></span>() method at any location in MyService () to stop the Service.</p></p>Communication between activities and services<p><p>Although the service is started in the activity, but after the start, the service is not related to the Activity.</p></p><p><p>The Onbind () method of directing the service to do in the Event.</p></p><pre><span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span> <span style="color: #0000ff;"><span style="color: #0000ff;">class</span></span><span style="color: #000000;"><span style="color: #000000;">MyService extends Service {</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Private</span></span>Downloadbinder Mbinder =<span style="color: #0000ff;"><span style="color: #0000ff;">New</span></span><span style="color: #000000;"><span style="color: #000000;">Downloadbinder (); </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">class</span></span><span style="color: #000000;"><span style="color: #000000;">Downloadbinder extends Binder{//start download</span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span> <span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span><span style="color: #000000;"><span style="color: #000000;">startdownload () {log.d (</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #800000;"><span style="color: #800000;">MyService</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span>,<span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #800000;"><span style="color: #800000;">Startdownload executed</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #000000;"><span style="color: #000000;">); }//view Download</span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span> <span style="color: #0000ff;"><span style="color: #0000ff;">int</span></span><span style="color: #000000;"><span style="color: #000000;">getprogress () {log.d (</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #800000;"><span style="color: #800000;">MyService</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span>,<span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #800000;"><span style="color: #800000;">Getprogress executed</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #000000;"><span style="color: #000000;">); </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">return</span></span> <span style="color: #800080;"><span style="color: #800080;">0</span></span><span style="color: #000000;"><span style="color: #000000;">; }} @Override</span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span><span style="color: #000000;"><span style="color: #000000;">IBinder onbind (Intent Intent) {</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">return</span></span><span style="color: #000000;"><span style="color: #000000;">mbinder; }}</span></span></pre><p><p></p></p><p><p>Create a dedicated binder object to manage the download Functionality. The above activities can control the logic inside the Service.</p></p><p><p>1, the creation of a Downloadbinder class, inherited from binder, the implementation of two simulation METHODS.</p></p><p><p>2. The Downloadbinder instance is created, and then the instance is returned in the Onbinder () method,</p></p><p><p>Binding service: Active Binding. ,</p></p><p><p>Unbind Service:</p></p><pre> <span style="color: #0000ff;"><span style="color: #0000ff;">Private</span></span><span style="color: #000000;"><span style="color: #000000;">Myservice.downloadbinder downloadbinder; </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Private</span></span>Serviceconnection connection =<span style="color: #0000ff;"><span style="color: #0000ff;">New</span></span><span style="color: #000000;"><span style="color: #000000;">serviceconnection () {@Override</span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span> <span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span><span style="color: #000000;"><span style="color: #000000;">onservicedisconnected (componentname name) {} @Override</span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span> <span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span><span style="color: #000000;"><span style="color: #000000;">onserviceconnected (componentname name, ibinder service) {downloadbinder</span></span>=<span style="color: #000000;"><span style="color: #000000;">(myservice.downloadbinder) service; Downloadbinder. <span style="color: #ff0000;"><strong>Startdownload</strong></span> (); Downloadbinder. <span style="color: #ff0000;"><strong>getprogress</strong></span> (); } }; @Override</span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span> <span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span><span style="color: #000000;"><span style="color: #000000;">OnClick (View V) {</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Switch</span></span><span style="color: #000000;"><span style="color: #000000;">(v.getid ()) {</span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> case</span><span style="color: #000000;"><span style="color: #000000;">r.id.start_service:intent startintent</span></span>=<span style="color: #0000ff;"><span style="color: #0000ff;">New</span></span>Intent (<span style="color: #0000ff;"><span style="color: #0000ff;"></span> this</span>, Myservice.<span style="color: #0000ff;"><span style="color: #0000ff;">class</span></span><span style="color: #000000;"><span style="color: #000000;">); <span style="color: #ff0000;"><strong>StartService</strong></span> (startintent); </span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> break</span><span style="color: #000000;"><span style="color: #000000;">; </span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> case</span><span style="color: #000000;"><span style="color: #000000;">r.id.stop_service:intent stopintent</span></span>=<span style="color: #0000ff;"><span style="color: #0000ff;">New</span></span>Intent (<span style="color: #0000ff;"><span style="color: #0000ff;"></span> this</span>, Myservice.<span style="color: #0000ff;"><span style="color: #0000ff;">class</span></span><span style="color: #000000;"><span style="color: #000000;">); <span style="color: #ff0000;"><strong>StopService</strong></span> (stopintent); </span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> break</span><span style="color: #000000;"><span style="color: #000000;">; </span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> case</span><span style="color: #000000;"><span style="color: #000000;">r.id.bind_service:intent bindintent</span></span>=<span style="color: #0000ff;"><span style="color: #0000ff;">New</span></span>Intent (<span style="color: #0000ff;"><span style="color: #0000ff;"></span> this</span>, Myservice.<span style="color: #0000ff;"><span style="color: #0000ff;">class</span></span><span style="color: #000000;"><span style="color: #000000;">); <span style="color: #ff0000;"><strong>Bindservice</strong></span> (bindintent, connection, <span style="color: #ff0000;"><strong>bind_auto_create</strong></span>); </span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> break</span><span style="color: #000000;"><span style="color: #000000;">; </span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> case</span><span style="color: #000000;"><span style="color: #000000;">r.id.unbind_service: <span style="color: #ff0000;"><strong>unbindservice</strong></span>(connection); </span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> break</span><span style="color: #000000;"><span style="color: #000000;">; </span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> case</span><span style="color: #000000;"><span style="color: #000000;">R.ID.START_INTENT_SERVICE:LOG.D (</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #800000;"><span style="color: #800000;">mainactivity</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span>,<span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #800000;"><span style="color: #800000;">Thread ID</span> is</span><span style="color: #800000;"><span style="color: #800000;">"</span></span>+<span style="color: #000000;"><span style="color: #000000;">thread.currentthread (). getId ()); Intent Intentservice</span></span>=<span style="color: #0000ff;"><span style="color: #0000ff;">New</span></span>Intent (<span style="color: #0000ff;"><span style="color: #0000ff;"></span> this</span>, Myintentservice.<span style="color: #0000ff;"><span style="color: #0000ff;">class</span></span><span style="color: #000000;"><span style="color: #000000;">); StartService (intentservice); </span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> break</span><span style="color: #000000;"><span style="color: #000000;">; </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">default</span></span><span style="color: #000000;"><span style="color: #000000;">: </span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> break</span><span style="color: #000000;"><span style="color: #000000;">; } }</span></span></pre><p><p></p></p><p><p>The anonymous class of serviceconnection, which overrides two methods, is called when the activity and the service are <span style="color: #ff0000;"><strong>successfully bound</strong></span> and unbound. <span style="color: #ff0000;"><strong></strong></span></p></p><p><p>So how do you bind it?</p></p><p><p><strong>Bindservice (intent,serviceconnection, Flag Bit)</strong></p></p><p><p><span><strong>Bind_auto_create indicates that the <span style="color: #ff0000;">service is created automatically</span> after the activity is bound to the Service. Will cause the Oncreat () method to execute, but the Onstartcommand () method will not Execute.</strong></span></p></p><p><p><span><strong>MyService can be bound not only to mainactivity but also to any other Activity.</strong></span></p></p>The life cycle of the service<p><p> </p></p><p><p>After both StartService () and Bindservice () are called, the StopService () and Unbindservice () methods must be called at the same time, and Ondestory () will not Execute.</p></p>Use of front desk services<p><p>Front desk service and General Service The biggest difference is that he will always have a running icon displayed in the status bar of the System.</p></p><pre><span style="color: #000000;"><span style="color: #000000;">@Override</span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span> <span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span><span style="color: #000000;"><span style="color: #000000;">onCreate () {super.oncreate (); Notification Notification</span></span>=<span style="color: #0000ff;"><span style="color: #0000ff;">New</span></span><span style="color: #000000;"><span style="color: #000000;">Notification (r.drawable.ic_launcher,</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #800000;"><span style="color: #800000;">Notification comes</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #000000;"><span style="color: #000000;">, System.currenttimemillis ()); Intent notificationintent</span></span>=<span style="color: #0000ff;"><span style="color: #0000ff;">New</span></span>Intent (<span style="color: #0000ff;"><span style="color: #0000ff;"></span> this</span>, Mainactivity.<span style="color: #0000ff;"><span style="color: #0000ff;">class</span></span><span style="color: #000000;"><span style="color: #000000;">); Pendingintent pendingintent</span></span>= Pendingintent.getactivity (<span style="color: #0000ff;"><span style="color: #0000ff;"></span> this</span>,<span style="color: #800080;"><span style="color: #800080;">0</span></span><span style="color: #000000;"><span style="color: #000000;">, notificationintent,</span></span><span style="color: #800080;"><span style="color: #800080;">0</span></span><span style="color: #000000;"><span style="color: #000000;">); Notification.setlatesteventinfo (</span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> this</span>,<span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #800000;">This is <span style="color: #800000;">title</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span>,<span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #800000;">This is <span style="color: #800000;">content</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #000000;"><span style="color: #000000;">, pendingintent); <span style="color: #ff0000;"><strong>Startforeground</strong></span> (</span></span><span style="color: #800080;"><span style="color: #800080;">1</span></span><span style="color: #000000;"><span style="color: #000000;">, notification); LOG.D (</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #800000;"><span style="color: #800000;">MyService</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span>,<span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #800000;"><span style="color: #800000;">OnCreate executed</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #000000;"><span style="color: #000000;">); }</span></span></pre><p><p></p></p><p><p>Create a Notification. Only the notification object created this time does not use <strong>Notificationmanager</strong> to display the notification, but instead calls the Startforeground () method.</p></p><p><p>Startforeground () The first parameter is the id, similar to the first parameter of the Notify () method, and the second argument is the constructed notifacation object.</p></p><p><p>Calling Startforeground () causes MyService to become a front-end Service. and display it in the system status Bar.</p></p>Using Intentservice<p><p>The code in the service is running in the main thread, and it is prone to ANR situations if you are dealing with some time-consuming operations directly in the SERVICE.</p></p><p><p>So this is the time to use the Android multi-threaded programming technology, in the service to open a Sub-thread.</p></p><p><p>Remember to open a thread and close this thread, stopself ().</p></p><p><p>Intentservice a good solution to the above two Problems.</p></p><pre><pre><span style="color: #0000ff;"></span> public <span style="color: #0000ff;">class</span> <span style="color: #000000;">Myintentservice extends Intentservice { </span>/<span style="color: #008000;"></span><span style="color: #008000;">* * * creates an intentservice. Invoked by your subclass ' s Constructor. * * @param name used to name of the worker thread, important only for debugging .</span> <span style="color: #008000;">*/</span> <span style="color: #0000ff;"></span> public <span style="color: #000000;">myintentservice (String name) { Super (name); } @Override </span><span style="color: #0000ff;">protected</span><span style="color: #0000ff;">void</span> <span style="color: #000000;"> onhandleintent (Intent Intent) { }}</span></pre></pre><p><p></p></p><p><p>You must provide an argument-free constructor, and you must call the parent Class's parameter constructor inside it. The abstract method of Onhandlerintent () is then implemented within the SUBCLASS. Some concrete logic can be dealt with in this method. Without worrying about the ANR Problem. Because this method has been executed in the sub-thread, here in order to confirm that we have printed the ID of the current thread in the Onhandlerintent () method, and according to the characteristics of intentservice, the service should be automatically stopped after the run is Finished.</p></p><p><p></p></p><p><p>Android--service</p></p></span>
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