Sarath Babu
Sarath Babu

Reputation: 922

Astronomical Calculations: J2SE code woking, but not in android

I am new to android development, I was trying to develop a calendar application. I was able to generate a calendar. Later I was trying to add vedic astrology information to each date. I had a Java program for computing these information. So I copied all these code (18 java files, and lot of double value operations) into my calendar app and tried to run, but this time I was getting only a black screen.

ComputeDailyPanchangam computeDailyPanchangam=new ComputeDailyPanchangam();
computeDailyPanchangam.setPanchangamType(Panchangam.Type.VEDIC_ENGLISH);
computeDailyPanchangam.setLongitudeLatitude(-67.28, 9.97);
GregorianCalendar temp=(GregorianCalendar)gCalendar.clone(); //GregorianCalendar instance for a date
computeDailyPanchangam.setDate(temp);
DailyPanchangam panchangam=computeDailyPanchangam.getDailyPanchangam();//Computes astrology information for the day,
                                                                      // Nakshatra, thithi, karana, yoga etc.

The code has lot of computations like, moon ecliptic longitude and sun ecliptic longitude calculation, sunrise time calculation etc for a date, also nee to instantiate many objects for.

Can any body help me to complete this application.

Upvotes: 1

Views: 466

Answers (1)

user184994
user184994

Reputation: 18281

The reason you only get a black screen is because you are doing these calculations on the UI thread (the main thread in Android is also the UI thread). The view will not be returned until these long calculations are complete.

To fix this, you will need to move the calculations to another thread. I would recommend using AsyncTask, which allows you to update the UI when calculations are complete.

Create an inner class like this:

private class MyTask extends AsyncTask<Void, Integer, Void> { 

    protected Void doInBackground(Void... params) {
        // Add the long running calculations in here...
        // If you want, you can make periodic calls to the UI thread,
        // by using the publishProgress() method, for example:
        for (int x = 0 ; x < 10 ; x++) {
            publishProgress(x);
        }
    }

    @Override
    protected void onProgressUpdate(Integer... values) {
        super.onProgressUpdate(values);
        // This method will be called everytime you call publishProgress().
        // Everything within this method will be called on the UI thread.
        // You can delete this method if you don't want to use it.
    }

    protected void onPostExecute(Void results) {
        // Anything you put in here will be called on the UI thread,
        // once doInBackground has finished
    }
}

Upvotes: 1

Related Questions