绑定形式Service
主界面:
未绑定Service单击获取Service数据效果:
绑定Service后单击获取Service数据效果
下面看HelloBindService代码:
package com.luohaibotestdemo12; import android.app.Service; import android.os.Binder; import android.content.Intent; import android.os.IBinder; import android.widget.Toast; public class HelloBindService extends Service { private final IBinder mBinder = new LocalBinder(); private String BOOKNAME = "Android开发入门与实战第二版"; public class LocalBinder extends Binder{ //提供一个getService方法,返回当前Service的上下文 HelloBindService getService(){ return HelloBindService.this; } } public HelloBindService() { // TODO Auto-generated constructor stub } @Override public IBinder onBind(Intent arg0) { // TODO Auto-generated method stub //重写onBind方法,使用Toast方法显示成功绑定的信息,并返回Binder对象 Toast.makeText(this, "成功绑定Service", 1000).show(); return mBinder; } @Override public boolean onUnbind(Intent intent) { // TODO Auto-generated method stub //重写onUnbind方法,使用Toast方法显示取消绑定信息 Toast.makeText(this, "成功取消绑定Service", 1000).show(); return false; } public String getBookName(){ return BOOKNAME; } }
MainActivity代码如下:
package com.luohaibotestdemo12; import com.luohaibotestdemo12.R.id; 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; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; public class MainActivity extends Activity { private HelloBindService bindService; private boolean isBind = false; private EditText editText; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); editText = (EditText)findViewById(R.id.editText); //绑定Service Button button1 = (Button)findViewById(R.id.button1); button1.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub` //如果当前没有绑定,则实例化一个Intent对象,调用bindService方法进行绑定 //为了让组件和Service建立连接,我们必须提供一个ServiceConnction对象即mConnection if (!isBind) { Intent serviceIntent = new Intent(MainActivity.this, HelloBindService.class); bindService(serviceIntent,mConnection,Context.BIND_AUTO_CREATE); isBind = true; } } }); //取消绑定Service Button button2 = (Button)findViewById(R.id.button2); button2.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub if (isBind) { isBind = false; //取消绑定 unbindService(mConnection); bindService = null; } } }); //获取Service数据 Button button3 = (Button)findViewById(R.id.button3); button3.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub if (bindService == null) { editText.setText("请先绑定服务"); return; } editText.setText(bindService.getBookName()); } }); } private ServiceConnection mConnection = new ServiceConnection() { @Override public void onServiceDisconnected(ComponentName name) { // TODO Auto-generated method stub bindService = null; } @Override public void onServiceConnected(ComponentName name, IBinder service) { // TODO Auto-generated method stub //通过IBinder对象得到HelloBinderService对象,得到HelloBInderService之后 //我们就可以对该类中的进行调用了 bindService = ((HelloBindService.LocalBinder)service).getService(); } }; @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; } }
布局文件代码如下:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="绑定Service"/> <Button android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="取消绑定Service"/> <Button android:id="@+id/button3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="获取Service数据"/> <EditText android:id="@+id/editText" android:layout_width="fill_parent" android:layout_height="wrap_content" /> </LinearLayout>
最后要记得在AndroidManifest文件中申明Service
<service android:name="com.luohaibotestdemo12.HelloBindService"></service>