Gia*_*nzi 8 android ipc android-service android-binder
我已经阅读了有关绑定服务的文档,其中显示您可以轻松地通过消息从活动传递到远程(即不在相同的上下文中)服务但是有没有办法将消息从服务发送到绑定活动?例如,我的活动绑定到同一个应用程序的正在运行的后台服务,向它发送一条消息,一旦收到此消息,服务就会向活动回复一条消息.我该如何实现?你能指点我一些解释这个主题的文档吗?
Dan*_*ofr 29
注意:这仅适用于进程内服务和活动,而不是像问题一样远程.
使用服务与活动进行通信涉及创建可以从活动传递到服务的侦听器.
您需要创建绑定到活动的服务.
第一步是提供服务.在服务中,确保您有一个Binder对象和返回binder对象的方法.下面是我在服务中用来检索我的活页夹的示例.另请注意,此绑定程序有一个设置侦听器的方法,该侦听器将作为BoundServiceListener类型字段保存在服务中.
/**
* 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 DownloadBgBinder extends Binder {
public DownloadBgService getService() {
// Return this instance of LocalService so clients can call public methods
return DownloadBgService.this;
}
public void setListener(BoundServiceListener listener) {
mListener = listener;
}
}
@Override
public IBinder onBind(Intent intent) {
return mBinder;
}
Run Code Online (Sandbox Code Playgroud)
现在,您需要创建某种接口,您可以将其传递给服务可用于发送更新的binder对象.下面是我的BoundServiceListener.
public interface BoundServiceListener {
public void sendProgress(double progress);
public void finishedDownloading();
}
Run Code Online (Sandbox Code Playgroud)
现在,在您的活动中,您需要创建一个用于绑定到服务的ServiceConnection对象.所以添加这样的东西.
/** Defines callbacks for service binding, passed to bindService() */
private ServiceConnection mConnection = new ServiceConnection() {
@Override
public void onServiceDisconnected(ComponentName arg0) {
mBound = false;
}
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
// We've bound to LocalService, cast the IBinder and get LocalService instance
DownloadBgBinder binder = (DownloadBgBinder) service;
mService = binder.getService();
binder.setListener(new BoundServiceListener() {
@Override
public void sendProgress(double progress) {
// Use this method to update our download progress
}
@Override
public void finishedDownloading() {
}
});
mBound = true;
}
Run Code Online (Sandbox Code Playgroud)
现在要注意的关键是
binder.setListener(new BoundServiceListener() {
@Override
public void sendProgress(double progress) {
// Use this method to update our download progress
}
@Override
public void finishedDownloading() {
}
});
Run Code Online (Sandbox Code Playgroud)
这部分是我实际将BoundServiceListener接口发送到服务类的地方.然后,服务类在此处使用该侦听器对象
if (mListener!=null)
mListener.finishedDownloading();
if (mListener!=null)
mListener.sendProgress(percent);
Run Code Online (Sandbox Code Playgroud)
现在,您可以将它放在服务类中的任何位置,您的活动将收到进度更新.
还要确保在您的活动中包含以下实际绑定和启动服务.
Intent intent = new Intent(this, DownloadBgService.class);
startService(intent);
bindService(intent, mConnection, Context.BIND_AUTO_CREATE);
Run Code Online (Sandbox Code Playgroud)
请记住,即使您绑定到服务,它也不会实际启动,直到您调用启动服务.绑定到服务只是将服务连接到活动.startService()方法调用服务
onStartCommand(Intent intent, int flags, int startId)
Run Code Online (Sandbox Code Playgroud)
还要在清单中声明您的服务
<service android:name=".services.DownloadBgService" />
Run Code Online (Sandbox Code Playgroud)
在活动离开时也取消绑定服务
@Override
protected void onStop() {
super.onStop();
// Unbind from the service
if (mBound) {
unbindService(mConnection);
mBound = false;
}
}
Run Code Online (Sandbox Code Playgroud)
希望这可以帮助.
| 归档时间: |
|
| 查看次数: |
9600 次 |
| 最近记录: |