Need suggestion for porting from C to Java app


  1. diddum

    diddum New Member

    I've programmed in c/c++ in the last 15 years but I've little
    experience in Java (and Android), so I would like a suggestion
    on how to structure a very simple (but computationally intensive)
    app which has the following general characteristics (no details needed).

    The app has some data (say 2MB) statically stored.
    When the app starts this data is preprocessed and
    a large structure of about 40Mb is created.

    After the initialization, the app has one text field
    for input, one for output and a button (for start/stop).

    The user enters a query then press the start button.
    The app computes a result using the large array.
    If the calculation on the input takes too long,
    the user can stop the processing with the stop button and
    start again with a new input.

    When I was doing this under windows/linux with C/C++, I had
    no problems: I used a large global array and two threads,
    one to draw interface and respond to user
    (mouse, clicks, etc.) and one to perform the computation.

    Which is the standard (best) way to structure this kind of app
    in Java/Android ?

    Should I use an Activity for the graphic interface and a
    Service for the computation ? Can I build the large array in the
    Activity when the app starts and pass a reference to it to the Service every time Start button is pushed ?

    I just need to be pointed in the right direction, to avoid
    some blind alleys.

    In a sense using c/c++ would be better, but I read that
    developing in Java (for Android) is much easier so I will
    try first with Java and revert to C only if the app will turn out
    to be too slow.

    thanks,
    Giovanni

    Advertisement
  2. jonbonazza

    jonbonazza Well-Known Member

    I would suggest creating a sort of splash screen where you load all of your data and populate your data structure. Once this task completes, I would spawn a new activity that contains your UI controls. Java and subsequently Android are event driven in nature. Once you have your UI set up, you will need to register a listener with your button object that will be fired automatically when it is clicked. Inside this listener, you will need to spawn off a new thread that will perform the network related code. When the code completes, you will need to use a Handler object to notify the UI thread that it has completed and to display the result. Rather than using a Thread and Handler object, you can simplify things by using Android's built-in AsyncTask which handles all of the threading issues for you.
  3. galth

    galth Member

    With 15 years of C/C++ experience and a computationally expensive program you might be better off checking out the NDK. Especially if your existing codebase is significant. It should allow you to use much of your existing code. You could still do the UI in java and just have your processing thread go through your C-code. It really is dependent on the size of the code your converting though as NDK has its own idiosyncracies, but the support for NDK has improved quite a bit in the last couple years when there was virtually no support or examples.

Share This Page