Android Service Basic operation

Source: Internet
Author: User

Service is one of the four components, open can be running in the system backstage. As for the other conceptual issues, I'm not going to talk about it. Direct manipulation to include specific content in code comments

Let's see.

The layout file on the

1 <LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"2 Xmlns:tools= "Http://schemas.android.com/tools"3 Android:layout_width= "Match_parent"4 Android:layout_height= "Match_parent"5 android:orientation= "vertical"6 Tools:context=". Mainactivity " >7 8     <TextView9         Android:id= "@+id/textview1"Ten Android:layout_width= "Wrap_content" One Android:layout_height= "Wrap_content" A Android:text= "@string/hello_world" /> -  -     <Button the         Android:id= "@+id/button1" - Android:layout_width= "Wrap_content" - Android:layout_height= "Wrap_content" - Android:text= "Start Service" + Android:onclick= "StartService" /> -  +     <Button A         Android:id= "@+id/button2" at Android:layout_width= "Wrap_content" - Android:layout_height= "Wrap_content" - Android:text= "Stop Service" - Android:onclick= "StopService" /> -  -     <Button in         Android:id= "@+id/button3" - Android:layout_width= "Wrap_content" to Android:layout_height= "Wrap_content" + Android:text= "Bind service" - Android:onclick= "Bindservice" /> the  *     <Button $         Android:id= "@+id/button4"Panax Notoginseng Android:layout_width= "Wrap_content" - Android:layout_height= "Wrap_content" the Android:text= "Unbind Service" + Android:onclick= "Unbindservice" /> A  the </LinearLayout>

Focus on the Java code

Package Com.zw.service;import Android.os.bundle;import Android.os.ibinder;import android.app.activity;import Android.content.componentname;import Android.content.context;import Android.content.intent;import Android.content.serviceconnection;import Android.view.menu;import Android.view.view;public class MainActivity Extends Activity {private Intent intent;private Myservicecon conn = new Myservicecon (); @Overrideprotected void OnCreate (B Undle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (r.layout.activity_main); intent = new Intent ("Com.zw.service.MyService");//intent Intent = new Intent (this,myservice.class);} public void StartService (View v) {StartService (intent);} public void StopService (View v) {StopService (intent);} Bind service public void Bindservice (View v) {Bindservice (intent, Conn, context.bind_auto_create);} public void Unbindservice (View v) {unbindservice (conn);} Class Myservicecon implements serviceconnection{@Overridepublic void onserviceconnected (ComponentName namE, IBinder Service) {//Bind completed callback method (Activity) and service Connection} @Overridepublic void onservicedisconnected (componentname name) {//Service Unbind}}}

Service class

Package Com.zw.service;import Android.app.service;import Android.content.intent;import android.os.IBinder;public Class MyService extends Service {@Overridepublic void OnCreate () {//TODO auto-generated method Stubsuper.oncreate (); System.out.println ("MyService-----onCreate");} @Overridepublic void OnStart (Intent Intent, int startid) {//TODO auto-generated method Stubsuper.onstart (Intent, Startid ); System.out.println ("MyService-----OnStart");} @Overridepublic ibinder onbind (Intent arg0) {System.out.println ("myservice-----onbind"); return null;} @Overridepublic boolean onunbind (Intent Intent) {System.out.println ("myservice-----onunbind"); return Super.onunbind (intent);} @Overridepublic void OnDestroy () {//TODO auto-generated method StubSystem.out.println ("MyService-----OnDestroy"); Super.ondestroy ();}}

The next thing to say is to configure the service in the manifest file

  <android:name= "Com.zw.service.MyService">            <   >                <android:name= "Com.zw.service.MyService"  />                            </intent-filter>         </  Service>

Well let's run it myself look at the

Android Service Basic operation

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.