标签:
关键点:如果service和访问者之间需要进行方法调用和交换数据,则应该使用bindService()和unbindService()方法启动、关闭Service。所以关键点在于Binder的使用!
Context的bindService()方法的完整方法签名为:bindService(Intent service,ServiceConnection conn,int flags),方法的三个参数解释如下:
1、service:该参数通过Intent指定要启动的service;
2、conn:该参数是一个ServiceConnection对象,该对象用于监听访问者与Service之间的连接情况。当访问者与Service之间连接成功是将回调该ServiceConnection对象的onServiceConnected(ComponentName name,IBinder service)方法;当service所在的宿主进程由于异常中止或其他原因终止,导致该service与访问者之间断开连接时回调该ServiceConnection对象的onServiceDisconnected(ComponentName name)方法。
3、flags:指定绑定时是否自动创建service(如果service还未创建)。该参数可指定为0(不自动创建),或BIND_AUTO_CREATE(自动创建)。
注意:ServiceConnection对象的onServiceConnection()方法中有一个IBinder对象,该对象即可实现与被绑定Service之间的通信。
当开发Service类时,该Service类必须提供一个IBinder onBind(Intent intent)方法,在绑定本地Service的情况下,onBind(Intent intent)方法所返回的IBinder对象将会传给ServiceConnection对象里onServiceConnected(CompontName name,IBinder service)方法的service参数,这样访问者就可通过该IBinder对象与Service进行通信了。
\MainActivity.java
1 package com.example.testservice1; 2 3 import android.support.v7.app.ActionBarActivity; 4 import android.content.ComponentName; 5 import android.content.Intent; 6 import android.content.ServiceConnection; 7 import android.os.Binder; 8 import android.os.Bundle; 9 import android.os.IBinder; 10 import android.util.Log; 11 import android.view.Menu; 12 import android.view.MenuItem; 13 14 public class MainActivity extends ActionBarActivity { 15 SimpleBinder sbinder = new SimpleBinder(); 16 17 @Override 18 protected void onCreate(Bundle savedInstanceState) { 19 super.onCreate(savedInstanceState); 20 setContentView(R.layout.activity_main); 21 Intent intent = new Intent(this, LocalService.class); 22 23 ServiceConnection sConnection = new ServiceConnection() { 24 25 @Override 26 public void onServiceDisconnected(ComponentName name) { 27 // TODO Auto-generated method stub 28 Log.d("connect", "connected failed"); 29 } 30 31 @Override 32 public void onServiceConnected(ComponentName name, IBinder service) { 33 // TODO Auto-generated method stub 34 sbinder = (SimpleBinder)service; 35 Log.d("connect", "connected succuss"); 36 } 37 }; 38 this.startService(intent); 39 this.bindService(intent, sConnection, 0); 40 int result = sbinder.Add(2, 5); 41 Log.d("result", String.valueOf(result)); 42 43 } 44 45 46 }
\LocalService.java
1 package com.example.testservice1; 2 3 import android.app.Service; 4 import android.content.Intent; 5 import android.os.IBinder; 6 import android.util.Log; 7 8 public class LocalService extends Service { 9 SimpleBinder binder ; 10 11 public LocalService() { 12 // TODO Auto-generated constructor stub 13 } 14 15 @Override 16 public IBinder onBind(Intent intent) { 17 // TODO Auto-generated method stub 18 return binder; 19 } 20 21 @Override 22 public void onCreate() { 23 // TODO Auto-generated method stub 24 super.onCreate(); 25 Log.d("service", "start service"); 26 } 27 28 public int serviceAdd(int a,int b){ 29 return a + b; 30 } 31 32 }
\SimpleBinder.java
1 package com.example.testservice1; 2 3 import android.os.Binder; 4 5 public class SimpleBinder extends Binder { 6 LocalService localService = new LocalService(); 7 8 public SimpleBinder() { 9 // TODO Auto-generated constructor stub 10 } 11 12 public int Add(int a,int b){ 13 return localService.serviceAdd(a, b); 14 } 15 16 }
MainActivity通过SimpleBinder来与LocalService进行通信,并能调用LocalService里的方法,从Logcat上可以看到,调用方法,输出的结果如下:
标签:
原文地址:http://www.cnblogs.com/arenzz/p/4897483.html