【问题标题】:Android & Felix OSGI - Communication with Android GUI?Android & Felix OSGI - 与 Android GUI 的通信?
【发布时间】:2012-10-01 02:06:04
【问题描述】:

我们在 Android (2.1) 上成功运行了 Apache Felix 4.0.3,并且可以在运行时部署/删除 Bundle。对于 OSGI Bundle 之间的依赖管理,我们使用 Felix DependenyManager。

现在我们想要将数据从正在运行的 OSGI Bundle 发送到 Android GUI 并显示出来。

我们怎样才能完成这项任务?我们可以使用某种回调吗?

【问题讨论】:

  • 您是否查看了this 以获取有关如何操作的信息?
  • 是的,我这样做了,但没有关于此主题的信息。不过谢谢你的回答

标签: android osgi apache-felix


【解决方案1】:

假设“发送数据”是指与捆绑包提供的服务进行交互,这并没有什么特别之处:只要确保您持有 BundleContext 实例,您的 Felix 实例为您提供,并且用它来请求服务。绑定数据的方式完全取决于您,就像在任何其他 Java 项目中一样。

只显示数据

作为一个相当人为的例子,你可以做类似的事情

Map<String, Object> config = new HashMap<String, Object>();
/// make settings here, including providing the bundles to start
Felix felix = new Felix(config);

    felix.start();

BundleContext context = felix.getBundleContext();

// now get some service! Remember to do nullchecks.
ServiceReference<PackageAdmin> ref = context.getServiceReference(PackageAdmin.class);
PackageAdmin admin = context.getService(ref);
ExportedPackage[] exportedPackages = admin.getExportedPackages(felix);

// use the result to update your UI
TextView field = (TextView) findViewById(R.id.textfield);
field.setText(exportedPackages[0].getName());

设置框架,获取一些服务,并使用一些数据更新 UI。

绑定

没有可以使用的默认回调,但我特别喜欢的一个技巧是让 UI 元素了解它们的 OSGi 环境;这样,您可以让他们“聆听”您框架中的更改。下面是我使用的简化视图,我更喜欢将复杂的东西委托给Apache Felix Dependency Manager

例如,假设您有一些侦听器接口。

public interface ClockListener {
    public void timeChanged(String newTime);
}

并且您有一些服务会定期调用当前时间实现此接口的所有服务。您现在可以创建一个TextField,它会在每次调用此方法时自行更新。比如,

public class ClockTextField extends TextView implements ClockListener {
    public ClockTextField(Context context) {
        super(context);
    }

    public void timeChanged(String newTime) {
        setText(newTime);
    }

    public void register(BundleContext bundleContext) {
        // remember to hold on to the service registration, so you can pull the service later.
        // Better yet, think about using a dependency management tool.
        bundleContext.registerService(ClockListener.class, this, null);
    }
}

【讨论】:

  • 谢谢,看起来很不错!我本来想在有时间实施后回复,但这不会很快发生。测试后我会接受答案。
猜你喜欢
  • 2016-02-23
  • 2018-05-14
  • 2012-07-14
  • 2014-12-19
  • 2014-01-08
  • 2013-10-25
  • 2014-05-22
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多