views:

81

answers:

3

Hi,

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.

A: 

you can do it with intents :)

check android-developers

ykatchou
Can you elaborate on that?What's "android-developers"? A forum? mailinglist?
Folkert van Heusden
@Folkert: Google returns [this](http://developer.android.com/).
imgx64
A: 

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.

Morrison Chang
A: 

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)

Chris Stratton