Android Course---Learning about service (running in the background)

Source: Internet
Author: User

Mainactivity.java

 PackageCom.hanqi.test2;ImportAndroid.content.ComponentName;ImportAndroid.content.Context;Importandroid.content.Intent;Importandroid.content.ServiceConnection;ImportAndroid.os.IBinder;Importandroid.support.v7.app.AppCompatActivity;ImportAndroid.os.Bundle;ImportAndroid.util.Log;ImportAndroid.view.View;ImportAndroid.widget.Toast; Public classMainactivityextendsappcompatactivity {protected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);    Setcontentview (R.layout.activity_main); }    //General way to start service//refer to how activity is started     Public voidBt1_onclick (View v) {//Through Intent Intent//an explicit intentIntent Intent =NewIntent ( This, TestService1.class);        StartService (Intent); Toast.maketext (mainactivity. This, "Start Service", Toast.length_short). Show (); }     Public voidBt2_onclick (View v) {//Through Intent Intent//an explicit intentIntent Intent =NewIntent ( This, TestService1.class);        StopService (Intent); Toast.maketext (mainactivity. This, "Stop Service", Toast.length_short). Show ();    } serviceconnection SC;  Public voidBt3_onclick (View v) {//Through Intent Intent//an explicit intentIntent Intent =NewIntent ( This, TestService1.class); if(sc = =NULL) {SC=Newserviceconnection () {@Override Public voidonserviceconnected (componentname name, IBinder service) {LOG.E ("TAG", "Connection service"); } @Override Public voidonservicedisconnected (componentname name) {LOG.E ("TAG", "Disconnect service");        }            }; }        //Parameters: Intent, connection, bind mode bind_auto_create: Auto CreateBindservice (INTENT,SC, context.bind_auto_create); Toast.maketext (mainactivity. This, "Bind service succeeded", Toast.length_short). Show (); }     Public voidBt4_onclick (View v) {//determines whether the connection is empty, if it is already bound        if(SC! =NULL)        {            //UnbindUnbindservice (SC); SC=NULL; Toast.maketext (mainactivity. This, "Unbind", Toast.length_short). Show (); }        Else{toast.maketext (mainactivity). This, "not yet bound", Toast.length_short). Show (); }} @Overrideprotected voidOnDestroy () {if(SC! =NULL) {            //UnbindUnbindservice (SC); SC=NULL; }        Super. OnDestroy (); }}

Testservice1.java

 PackageCom.hanqi.test2;ImportAndroid.app.Service;Importandroid.content.Intent;ImportAndroid.os.Binder;ImportAndroid.os.IBinder;ImportAndroid.util.Log; Public classTestService1extendsService { PublicTestService1 () {LOG.E ("TAG", "Construct service"); }    //callback method for binding//you have to override@Override Publicibinder onbind (Intent Intent) {//Todo:return The communication channel to the service. //throw new Unsupportedoperationexception ("not yet implemented");LOG.E ("TAG", "Bind is called"); return NewBinder (); } @Override Public voidonCreate () {LOG.E ("TAG", "Create service"); Super. OnCreate (); } @Override Public voidOnDestroy () {LOG.E ("TAG", "Destroy service"); Super. OnDestroy (); } @Override Public intOnstartcommand (Intent Intent,intFlagsintStartid) {LOG.E ("TAG", "Start command"); return Super. Onstartcommand (Intent, flags, Startid); } @Override Public BooleanOnunbind (Intent Intent) {LOG.E ("TAG", "Unbind"); return Super. Onunbind (Intent); }}

Activity_main.xml

<?XML version= "1.0" encoding= "Utf-8"?><LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Xmlns:tools= "Http://schemas.android.com/tools"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"Android:paddingbottom= "@dimen/activity_vertical_margin"Android:paddingleft= "@dimen/activity_horizontal_margin"Android:paddingright= "@dimen/activity_horizontal_margin"Android:paddingtop= "@dimen/activity_vertical_margin"Tools:context= "Com.hanqi.test2.MainActivity"android:orientation= "vertical">    <LinearLayoutAndroid:layout_width= "Match_parent"Android:layout_height= "Wrap_content">        <ButtonAndroid:layout_width= "0DP"Android:layout_height= "Wrap_content"Android:layout_weight= "1"Android:text= "General start service"Android:onclick= "Bt1_onclick"/>        <ButtonAndroid:layout_width= "0DP"Android:layout_height= "Wrap_content"Android:layout_weight= "1"Android:text= "General Stop Service"Android:onclick= "Bt2_onclick"/>    </LinearLayout>    <LinearLayoutAndroid:layout_width= "Match_parent"Android:layout_height= "Wrap_content">        <ButtonAndroid:layout_width= "0DP"Android:layout_height= "Wrap_content"Android:layout_weight= "1"Android:text= "bind start service"Android:onclick= "Bt3_onclick"/>        <ButtonAndroid:layout_width= "0DP"Android:layout_height= "Wrap_content"Android:layout_weight= "1"Android:text= "Unbind stop service"Android:onclick= "Bt4_onclick"/>    </LinearLayout></LinearLayout>

Android Course---Learning about service (running in the background)

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.