Android编程心得-Service数据绑定初步
在Android里,Service的数据绑定是一种重要的用法,我们知道Service与Activity一样是运行在当前应用进程的主线程里面的,他们之间交互的方式有多种,下面我来介绍一下如何使用数据绑定的方法通过Service向Activity交互数据
1.首先我们要定义一个接口,接口里定义我们需要实现的方法
public interface ICount { public int getcount(); }
2.接下来我们需要一个Service的子类实现本接口,定义一个ServiceBinder的内部类,通过它的对象来绑定数据,要注意的是我们如果要进行耗时操作的话,仍然需要在Service中创建线程,Service自身就是运行在主线程中的。还有一个就是OnBind的返回值是IBinder,但是这里我使用ServiceBinder对象是继承Binder的,那为什么这里可以这么写呢?因为Binder是Base class for a remotable object, the core part of a lightweight remote procedure call mechanism defined by IBinder,是直接从IBinder这里的直接子类
。
public class BackGroundService extends Service implements ICount { private boolean disable; //线程是否执行的标识位 private int count; //计数 private ServiceBinder serviceBinder = new ServiceBinder(); public class ServiceBinder extends Binder implements ICount { @Override public int getcount() { // TODO Auto-generated method stub return 0; } } @Override public IBinder onBind(Intent intent) { // TODO Auto-generated method stub return serviceBinder; } @Override public void onCreate() { // TODO Auto-generated method stub super.onCreate(); new Thread(new Runnable() { // @Override public void run() { while (!disable) { try { Thread.sleep(1000); } catch (InterruptedException e) { } count++; System.out.println("CountService Count is " + count); } } }).start(); } @Override public int onStartCommand(Intent intent, int flags, int startId) { // TODO Auto-generated method stub return super.onStartCommand(intent, flags, startId); } @Override public int getcount() { return count; // TODO Auto-generated method stub } @Override public void onDestroy() { // TODO Auto-generated method stub this.disable = true; // Log.v(" CountService ", " on destroy "); System.out.println("Service destroy..."); super.onDestroy(); } }
3.定义完了Service这一部分的内容,接下来就需要在AndroidManifest.xml中注册了,需要注意的是这里不仅要对我们包类的Service进行声明,同时还需要声明intent的过滤器
<service android:name="com.yqc.testservice.BackGroundService"> <intent-filter> <action android:name="com.yqc.testservice.BackGroundService" /> </intent-filter> </service>
4.定义完毕以后,接下来需要在Activity中的交互部分了,刚才在配置文件中定义的Intent的过滤器这里就用到了,在bindService方法中,这就是他的一个参数,然后通过ServiceConnetion的对象与Service建立连接并取得后台的参数。
public class MainActivity extends Activity { Button btn_start; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); this.bindService(new Intent("com.yqc.testservice.BackGroundService"), this.serviceConnection, BIND_AUTO_CREATE); btn_start = (Button) findViewById(R.id.btn_start); btn_start.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub } }); } @Override protected void onDestroy() { // TODO Auto-generated method stub this.unbindService(serviceConnection); System.out.println("Activity Destroy..."); super.onDestroy(); } private ICount countentity; private ServiceConnection serviceConnection = new ServiceConnection() { @Override public void onServiceConnected(ComponentName name, IBinder service) { // TODO Auto-generated method stub countentity = (ICount) service; System.out.println(" CountService on serivce connected, count is " + countentity.getcount()); } @Override public void onServiceDisconnected(ComponentName name) { // TODO Auto-generated method stub countentity = null; } }; }
5.最后得到的结果如下