gpt4 book ai didi

android - 远程服务如何向绑定(bind)的 Activity 发送消息?

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:47:52 26 4
gpt4 key购买 nike

我读过 documentation about Bound Services ,其中表明您可以通过 Messages 轻松地从 Activity 到远程(即不在同一上下文中)服务进行通信,但是有什么方法可以从 Service 发送消息em> 到绑定(bind) Activity?例如,我的 Activity 绑定(bind)到同一应用程序的正在运行的后台服务,向它发送一条消息,并且在收到此消息后服务回复一条消息给 Activity ..我如何实现这个?你能给我指点一些解释这个主题的文档吗?

最佳答案

注意:这仅适用于进程内服务和 Activity ,而不是远程问题。

使用服务与 Activity 通信涉及创建一个监听器,您可以将其从 Activity 传递给服务。

您需要创建一个绑定(bind)到 Activity 的服务。

第一步是提供服务。在服务中确保您有一个 Binder 对象和返回一个 Binder 对象的方法。下面是我在服务中用来检索 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;
}

现在您需要创建某种接口(interface),您可以将其传递给 Binder 对象,您的服务可以使用该对象向其发送更新。下面是我的 BoundServiceListener。

public interface BoundServiceListener {

public void sendProgress(double progress);
public void finishedDownloading();
}

现在,在您的 Activity 中,您需要创建一个用于绑定(bind)到服务的 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;
}

现在这里要注意的关键行是

binder.setListener(new BoundServiceListener() {

@Override
public void sendProgress(double progress) {
// Use this method to update our download progress
}

@Override
public void finishedDownloading() {

}
});

这部分是我实际将 BoundServiceListener 接口(interface)发送到服务类的地方。服务类然后在此处使用该监听器对象

    if (mListener!=null)
mListener.finishedDownloading();
if (mListener!=null)
mListener.sendProgress(percent);

现在您可以将它放在服务类中您需要的任何位置,您的 Activity 将收到您的进度更新。

还要确保在您的 Activity 中包含以下内容以实际绑定(bind)和启动服务。

Intent intent = new Intent(this, DownloadBgService.class);
startService(intent);
bindService(intent, mConnection, Context.BIND_AUTO_CREATE);

请记住,即使您绑定(bind)到服务,它也不会真正启动,直到您调用启动服务。绑定(bind)到服务只是将服务连接到 Activity 。 startService() 方法调用服务

onStartCommand(Intent intent, int flags, int startId)

同时在您的 list 中声明您的服务

<service android:name=".services.DownloadBgService" />

当 Activity 离开时也取消绑定(bind)服务

@Override
protected void onStop() {
super.onStop();
// Unbind from the service
if (mBound) {
unbindService(mConnection);
mBound = false;
}
}

希望这对您有所帮助。

关于android - 远程服务如何向绑定(bind)的 Activity 发送消息?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10547577/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com