简体   繁体   English

android可以执行字符串作为代码吗?

[英]Can android execute string as code?

So basically I would like my app to read info from a database, this info would be a string that is valid java code. 所以基本上我希望我的应用程序从数据库中读取信息,这个信息将是一个有效的java代码字符串。 Once read, I would like my app to execute this string as if it were code. 一旦阅读,我希望我的应用程序执行此字符串,就像它是代码一样。 Is there any functionality packaged with the android sdk that supports this? Android sdk是否包含支持此功能的任何功能?

Essentially I want the phone to populate some data with information queried from a database. 基本上我希望手机使用从数据库查询的信息填充一些数据。 One of these pieces of information would be a statement like: 其中一条信息可能是:

"return data.ZombieKillTotal >= 100000;"

Which would be used inside a statement like: 这将在如下声明中使用:

registerAchievement(new Achievement("Zombie Killer", new AchievementValidator() { 
    public boolean isSatisfied(Data data) { ExecStringAsCode(query result) } 
    });

I just don't know what to use for 'ExecStringAsCode' :( 我只是不知道'ExecStringAsCode'用什么:(

I ended up using the JRE library and imported javax.script.*; 我最终使用了JRE库并导入了javax.script.*;

http://java.sun.com/developer/technicalArticles/J2SE/Desktop/scripting/ http://java.sun.com/developer/technicalArticles/J2SE/Desktop/scripting/

The Java programming language doesn't really provide a feature for the execution of code in text strings. Java编程语言实际上并没有提供在文本字符串中执行代码的功能。

You could embed an interpreter for a more script-friendly language (perhaps Python) and execute code that way. 您可以为更易于编写脚本的语言(可能是Python)嵌入一个解释器,并以这种方式执行代码。

如果你想执行动态加载的代码,你将不得不使用像COdeDOM这样的东西来创建代码并编译它然后使用反射来加载和执行它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM