标签:
绑定服务主要是其他组件绑定服务(比如活动),然后发送请求,接收返回。这个服务主要是作为其他组件的佣人,不会再后台无限
地运行。个人认为关键要学习的是如何绑定以及服务和组件之间的通信。
Binder
that either:
Service
instance, which has public methods theclient can call
Binder
from the
onBind()
callback method.
Binder
from the
onServiceConnected()
callback method andmake calls to the
bound service using the methods provided.public class LocalService extends Service { // Binder given to clients private final IBinder mBinder = new LocalBinder(); // Random number generator private final Random mGenerator = new Random(); /** * Class used for the client Binder. Because we know this service always * runs in the same process as its clients, we don‘t need to deal with IPC. */ public class LocalBinder extends Binder { LocalService getService() { // Return this instance of LocalService so clients can call public methods return LocalService.this; } } @Override public IBinder onBind(Intent intent) { return mBinder; } /** method for clients */ public int getRandomNumber() { return mGenerator.nextInt(100); } }
标签:
原文地址:http://blog.csdn.net/qingziguanjun1/article/details/51345646