简体   繁体   中英

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. 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?

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' :(

I ended up using the JRE library and imported javax.script.*;

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.

You could embed an interpreter for a more script-friendly language (perhaps Python) and execute code that way.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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