Example of a binding service
The start service can be started in this way using StartService, and we can also start the service by using the BIND service. How to bind it? Please read the following code.
Sample Project Learnservice? The code for Myservice.java is as follows:
package Com.example.learnservice; import android.app.Service; import android.content.Intent; import android.os.Binder; import android.os.IBinder; public class myservice extends service { Public myservice () {} @Override
public ibinder
onbind (Intent Intent) {
return
new Binder (); }}
The code for the example project Learnservice?activity_main.xml is as follows:
<LinearLayout xmlns: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=". Mainactivity " android:orientation="Vertical"> <TextViewandroid:layout_width="Wrap_content"android:layout_height= "Wrap_content" Android:text="@string/hello_world" /> <buttonandroid:id="@+id/btnstartservice"android:layout_width= "Wrap_content" android:layout_height="Wrap_content"android:text="Start service" /> <button android:id = "@+id/btnstopservice" android:layout_width = "wrap_content" android:layout_height = "wrap_content" android:text = "stop service" /> <button android:id = "@+id/btnbindservice" android:layout_width = "wrap_content" android:layout_height = "wrap_content" android:text = "binding service" /> <buttonandroid:id= "@+id/btnunbindservice"android:layout_width=" Wrap_content "android:layout_height=" Wrap_content "android:text=" Unbind Service " /> </linearlayout>
Sample Project Learnservice? The code for Mainactivity.java is as follows:
PackageCom.example.learnservice;Importandroid.app.Activity;ImportAndroid.content.ComponentName;ImportAndroid.content.Context;ImportAndroid.content.Intent;ImportAndroid.content.ServiceConnection;ImportAndroid.os.Bundle;ImportAndroid.os.IBinder;ImportAndroid.view.Menu;ImportAndroid.view.View;ImportAndroid.view.View.OnClickListener; Public class mainactivity extends Activity implements Onclicklistener , serviceconnection{ PrivateIntent Intent;@Override protected void onCreate(Bundle savedinstancestate) {Super. OnCreate (Savedinstancestate); Setcontentview (R.layout.activity_main);/* The instance of the service has only one * intent used in the operating system to configure the service information to be started by the program * Define a member variable intent for StartService, StopService use * */Intent =NewIntent (mainactivity. This, Myservice.class);/ * If more than one button executes the function, you can use this to implement the event listener while allowing the mainactivity to implement the Onclicklistener interface * /Findviewbyid (R.id.btnstartservice). Setonclicklistener ( This); Findviewbyid (R.id.btnstopservice). Setonclicklistener ( This); Findviewbyid (R.id.btnbindservice). Setonclicklistener ( This); Findviewbyid (R.id.btnunbindservice). Setonclicklistener ( This); }@Override Public Boolean Oncreateoptionsmenu(Menu menu) {//inflate the menu; This adds items to the action bar if it is present.Getmenuinflater (). Inflate (R.menu.main, menu);return true; }@Override Public void OnClick(View v) {/ * Judging by the ID of the clicked View * / Switch(V.getid ()) { CaseR.id.btnstartservice:startservice (Intent); Break; CaseR.id.btnstopservice:stopservice (Intent); Break; CaseR.id.btnbindservice:/* First parameter: Intent * Second parameter: The connection of the service, which is used primarily to listen for the status of the service * third parameter: constant * */Bindservice (Intent, This, context.bind_auto_create); Break; CaseR.id.btnunbindservice:unbindservice ( This); Break; } }/ * Service is executed after the binding succeeds * / @Override Public void onserviceconnected(ComponentName arg0, IBinder arg1) {System.out.println ("Service Connected"); }/* Execute when the process of the service crashes or is killed */ @Override Public void onservicedisconnected(ComponentName arg0) { }}
Sample Project Learnservice? Some of the main codes of Androidmanifest.xml are as follows:
<applicationandroid:allowbackup="true"android:icon="@drawable/ Ic_launcher "android:label=" @string/app_name "android:theme=" @style/ Apptheme " > <activityandroid:name="com.example.learnservice.MainActivity"android: Label="@string/app_name" > <intent-filter> <action android:name="Android.intent.action.MAIN" /> <category android:name="Android.intent.category.LAUNCHER" /> </intent-filter> </activity> <serviceandroid:name=". MyService "android:enabled=" true "android:exported=" true " > </Service> </Application>
Second, the program operation description
Run the program, click on the binding service, you can see the word "service Connected" in Logcat to prove that the service was successfully bound. Unbind, click Unbind Service.
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Android Basics _ Binding Service