如何在BeanShell for SL4A中获取Android上下文?

时间:2014-01-10 07:39:46

标签: android android-context sl4a beanshell

我开始为SL4A探索BeanShell,因为我read它可以访问整个Android API。这将有助于试验API功能和编程思想,而无需计算机或编译。

但是,大部分API都是通过Context访问的,我不知道如何获取它。尽管SL4A和BeanShell都有详细记录,但两者的结合似乎记录得非常糟糕。

例如,要访问android.net.ConnectivityManager,开发人员引用声明我需要调用Context.getSystemService(Context.CONNECTIVITY_SERVICE)来获取实例。但是如果没有上下文,我认为我无法访问ConnectivityManager

的方法

那么如何获得Context

3 个答案:

答案 0 :(得分:1)

... Dahrrr

在研究和制定问题时,我发现了is an unresolved issue

  

Java解释器说明

     

Beanshell和Rhino都可以直接访问android api。然而,许多Android api调用需要一个上下文,由于它们的运行方式,这些解释器没有。正在寻求解决方案...建议赞赏。

答案 1 :(得分:0)

我不知道sl4a或者是豆壳,但我知道Rhino。所以我想你也可以创建可编写脚本的对象。如果这是正确的,你可以这样做(在java中):

// first create a simple scope called -> scope


// inject context.
 Object injectObject = Context.javaToJS(android_context, scope);    // ('Context' of rhino library)
 ScriptableObject.putProperty(scope, "android_context_name", injectObject);

// so then execute your script with the injected object
 execute(javascript_context, host, scope, scriptId, source, settings);

当您完成注入上下文后,您可以通过以下方式访问他:“android_context_name”。 (在你的脚本中)。

答案 2 :(得分:-1)

当你运行你的bean shell脚本时,它将通过一个Android应用程序?

有这样的可用

例如:BeanShell Executor,它允许您运行脚本。

所以我假设上下文将从应用程序传递到正在执行的脚本。