BeanShell Android问题。无法远程启动活动

BeanShell Android问题。无法远程启动活动,android,beanshell,Android,Beanshell,你好。BeanShell最令人惊奇的地方是,我可以从服务器上动态地控制我想要做的事情,我认为这会令人惊奇 尽管我从未成功地做到这一点,而且似乎也没有其他人试图从beanshell开始活动 事情是这样的。我只想将代码从服务器端传递到Android,Android将在解释器中评估代码并运行它 问题是,无论我做什么尝试,我都会从BeanShell得到一个例外 服务器端的代码是下一个 $response['method'] = "import my.some.name.*;" .

你好。BeanShell最令人惊奇的地方是,我可以从服务器上动态地控制我想要做的事情,我认为这会令人惊奇

尽管我从未成功地做到这一点,而且似乎也没有其他人试图从beanshell开始活动

事情是这样的。我只想将代码从服务器端传递到Android,Android将在解释器中评估代码并运行它

问题是,无论我做什么尝试,我都会从BeanShell得到一个例外

服务器端的代码是下一个

   $response['method'] = "import my.some.name.*;"
        . "startActivity(new Intent(this,MyProfile.class))";
  try {
                String responseBody = response.body().string();
                JSONObject jsonObject = new JSONObject(responseBody);
                String method = jsonObject.optString("method");
                Interpreter interpreter = new Interpreter();
                try {
                    Object res = interpreter.eval(method);
                } catch (EvalError evalError) {
                    evalError.printStackTrace();
                }
            } catch (IOException e) {
                e.printStackTrace();
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
Android的代码是下一个

   $response['method'] = "import my.some.name.*;"
        . "startActivity(new Intent(this,MyProfile.class))";
  try {
                String responseBody = response.body().string();
                JSONObject jsonObject = new JSONObject(responseBody);
                String method = jsonObject.optString("method");
                Interpreter interpreter = new Interpreter();
                try {
                    Object res = interpreter.eval(method);
                } catch (EvalError evalError) {
                    evalError.printStackTrace();
                }
            } catch (IOException e) {
                e.printStackTrace();
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
但是我从BeanShell那里得到了下一个例外

Sourced file: inline evaluation of: ``import my.some.name.*;startActivity(new Intent(this,MyProfile.class));'' : Class: MyProfile not found in namespace : at Line: 1 : in file: inline evaluation of: ``import my.some.name.*;startActivity(new Intent(this,MyProfile.class));'' : MyProfile 

有什么想法吗?

可能的问题

我对BeanShell了解不多,但这里有几个问题

  • 您可以在运行时导入类(使用编译语言)
  • 你正在尝试做与之相当的事情(但没有做任何事情)
  • 安全。没有用户会同意你有权远程打开他们应用程序上的屏幕
  • 据推测,BeanShell应该在封面下进行反射,但在某种情况下,您将无法进行导入

    可能的解决方案

  • 使用库的类/活动应该导入所有内容(我不确定编译器是否会保留此内容)
  • 您可以直接使用反射,例如“来自名称的方法”。缺点是,除非您处理大量的情况,否则您可以从服务器发送的代码非常有限
  • 您只能发送名称/命令;到java应用程序中的特定端点(这是我推荐的),并提前计划您想要的操作

    $response['method'] = "my.some.name.MyProfile";
    
    JSONObject jsonObject = new JSONObject(response.body().string());
    String nameParam = jsonObject.optString("method");
    Class<? extends Activity> clazz = Class.forName(nameParam); //wrap with try
    startActivity( new Intent(this, clazz) )
    
    $response['method']=“my.some.name.MyProfile”;
    JSONObject=newJSONObject(response.body().string());
    字符串nameParam=jsonObject.optString(“方法”);
    
    类以防万一,如果有人需要相同的解决方案,我将发布给大家,让大家知道

    事情是这样的。

    首先,您需要知道,无论您在服务器端尝试做什么,请记住,BeanShell实际上对您传递的字符串代码一无所知,因为它将像开箱即用的代码一样对其进行解释,所以在关于全名路径的公共软件提示的帮助下,我成功地使其工作

    所以要做的第一步是初始化解释器。

    基本初始化如下所示:

      String responseBody = response.body().string();
                    JSONObject jsonObject = new JSONObject(responseBody);
                    String method = jsonObject.optString("method");
                    Interpreter interpreter = new Interpreter();
    
                    try {
                        interpreter.set("context",getApplicationContext());
                        Object res = interpreter.eval(method);
                    } catch (EvalError evalError) {
                        evalError.printStackTrace();
                    }
    
    请非常注意
    上下文
    ,因为这是我的主要问题,当我成功地强制BeanShell识别我的类时,BeanShell开始抛出
    方法未找到
    有关
    startActivity()的异常
    因此,通过逻辑思考,我们可以假设我们将上下文设置为活动,作为远程方法的父方法,并开始从
    上下文中评估所有内容。这里是远程代码的外观

    $response['method'] = "import ink.va.activities;"
        . "import android.content.Intent;"
        . "import android.content.*;"
        . "context.startActivity(new android.content.Intent(context, my.package.name.MyProfile.class));";
    
    这里需要注意的最重要的事情。

    •我们正在导入一切可能的东西,以便BeanSherll识别我们的类,即使它们是Android构建的,但不管怎样,我们仍然需要导入它们

    •如果要使用任何类,那么正如CommonWare注意到的,必须指定该类的完整路径,例如
    my.package.name.MyProfile.class


    •当我得到未找到的
    命令时
    我开始思考
    上下文。startActivity()
    因为我事先在BeanShell中定义了
    上下文
    ,作为我的父母,我将从中使用方法和Woala,一切都像一个魅力

    “我想这会很神奇”——这也很难保证,可能会导致诉讼或监禁。“知道发生了什么吗?”——试着为
    MyProfile
    使用完全限定的类名。那法律怎么办?它不是公共API,那么完全限定的名称怎么办?是的!没想到!谢谢威尔,他妈的,是的!我们做到了!!!这肯定有一个合法的使用案例,特别是针对设备即服务,作为一种根据需要缓解现场某些异常问题的方法。哦……这是我使用BeanShell的主要原因,因为在他们的示例中,我看到Toast能够运行,但如果我要事先这样做,在这种情况下根本没有必要:(理想情况下,我希望通知面板位于应用程序内部,每次点击都会得到不同的处理,这是我的主要想法,因此我试图实现这一点,但你的评论非常聪明