Android:使用 I/O 重定向调用常规 Java 风格的程序

发布于 2024-09-24 04:11:03 字数 240 浏览 1 评论 0原文

我有一个带有“public static void main”方法(java 风格)的 apk(或 .class,无论如何),它可以执行一些操作。编译并安装 apk 可以正常工作(来自 eclipse)。

现在,我想从常规 Android 应用程序调用该代码,同时将其 stdin/stdout 重定向到输入/输出流对象。这可能吗?如果是这样:怎么办?如果没有:是否有其他方法可以通过某种管道/io 重定向构造在后台运行活动?

谢谢。

I have an apk (or .class, whatever) with a 'public static void main'-method (java style) which does some things. Compiling and installing the apk this gives works fine (from eclipse).

Now from a regular Android app I would like to invoke that code while redirecting its stdin/stdout to Input-/Output- Stream objects. Is this possible? And if so: how? And if not: is there some other way in which I can run an activity in the background with some kind of pipe/io-redirection construction?

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

孤独难免 2024-10-01 04:11:03

Android 绝对支持管道以及 unix 域套接字。不鼓励使用 exec,但目前可以使用。

请参阅任何具有本地 shell 选项的 Android 终端模拟器的源代码,了解如何执行此操作的示例。本质上,你的 GUI 只是替换了终端仿真器,而你的引擎则替换了​​ shell。

如果将来使用 exec 成为问题,您将需要将引擎编译为 jni 库而不是独立的可执行文件。这不一定太难 - 只需将其移植到 ndk hello-jni 示例中,并有一个调用 main() 的 jni 函数即可。从 java 线程调用它。像以前一样与管道进行通信,或者使用 jni 设置一些其他消息传递方案。

请注意,“使用服务”答案也需要服务中的 java 包装器。目前,除非您是制作和注册系统服务的平台供应商,否则您无法使用任何支持/认可的机制制作纯本机服务。

另请注意,您的引擎需要能够保存任何状态,并且同时 java 端需要在 android 活动生命周期下执行此操作(本质上,一旦您暂停,您就会被杀死,恕不另行通知)

Android absolutely supports pipes as well as unix domain sockets. Use of exec is somewhat discouraged, but works at the moment.

See the source of any android terminal emulator with a local shell option for an example of how to do it. Essentially your gui just replaces the terminal emulator, and your engine replaces the shell.

If using exec becomes a problem in the future, you will need to compile your engine as a jni library rather than a stand alone executable. That's not necessarily too hard - just graft it onto the ndk hello-jni example, and have a single jni function that calls main(). Call this from a java thread. Communicate with pipes as before, or rig up some other message passing scheme using jni.

Note that the "use a service" answers are going to require a java wrapper in the service too. As of the moment, you can't make a pure-native service using any supported/endorsed mechanism unless you are the platform vendor making and registering a system service.

Also be aware that your engine will need to be able to save any state, and do so at the same time the java side would need to under the android activity lifecycle (essentially, once you get paused you become killable without further notice)

墨洒年华 2024-10-01 04:11:03

您可能会为您的应用程序寻找基于服务和意图的设计。 Android 设计/架构/理念不支持进程之间的流/管道。对于类似国际象棋的应用程序,UI 将是一个活动,而引擎可能是一项服务。对于 Android 来说,用户界面是最重要的,你的设计必须考虑到这一点。当进行移动时,可以从 UI 向后台服务发出一个意图,该后台服务将包装引擎代码(Java 或可能通过 NDK 使用 C 语言)。一旦回复移动准备就绪,它将返回到 UI Activity 并显示。如果您无法在 Intent 中容纳所需的所有数据,您可能需要创建一个 Content Provider,以允许 UI 获取缺少的元素。我再次考虑制作一个制作精良的应用程序。如果游戏逻辑不需要太长的时间(或者您不需要完全“良好”的 UI 体验,即如果您离开游戏则重新启动游戏),您可以将所有内容都放在一个 Activity 中并使用 AsyncTask/Thread用于游戏逻辑。

You'll probably be looking at a Service and Intent based design for your app. The Android design/architecture/philosophy doesn't support streams/pipes between processes. In the case of a chess-like app the UI would be an activity and the engine might be a service. For Android the UI is the most important thing and your design will have to take that into account. When a move is made an intent could be fired off from the UI to the background service which would wrap the engine code (Java or perhaps in C via NDK). Once the reply move is ready, it would be returned back to the UI Activity and displayed. If you can't fit all of the data you want in the Intent, you may need to create a Content Provider which would allow the UI to get the missing elements. Again I'm thinking about a well made app. If the game logic doesn't take too long (or you don't need a completely 'nice' UI experience, i.e. game restart if you leave the game) you may be able to stick everything in one Activity and use an AsyncTask/Thread for game logic.

梅倚清风 2024-10-01 04:11:03

你可以有意图地做到这一点:)

检查 android-developers

you can do it with intents :)

check android-developers

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文