Issue with background task in android - Quick Java fix for experts

Hi experts,

I have one issues displaying a progress dialog in a thread.

I need to get the latitude and longitude either from SIM or from GPS. I do this process(fetching lat and long) in an async task(seperate thread) ..fetching co-ordinates works perfect... I have given the classes which I am using in the links given below..my question is, I need to show the progress dialog, until the entire background process is finished..so, I start my progress dialog in pre execute and dismisses the dialog in post execute..

but the issue i have is, the progress dialog gets dismissed before the required task of getting latitude and longitude is finished(ie; before the latitude and longitude gets printed inside the doinbackground method ). I do not know why!

I know that this is a quick java fix,

My front end class -- http://pastebin.com/iVTBmBKt
My location fetching class -- http://pastebin.com/PAs2FQXv

Anyone who can help me resolve this will be well appreciated with points for sure.

Any queries/clarifications, please let me know..

Best Regards,
Sree
Sreejith22Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Sreejith22Author Commented:
Hi

Is there anyone in this forum who knows android? or Java?

garypfirstechCommented:
What sort of results are you getting?  Do you ever get the output of the System.out.println in doInBackground()?

I'm thinking that you may be throwing an exception and completing abnormally and that is what is dismissing your progress dialog.  Can you set a breakpoint or Log something or do a System.out.println as the first statement in onPostExecute()?
Sreejith22Author Commented:
Hi,

In the attached code below, it reaches up to -- locationResult = new LocationResult() {  

After that, it does not get inside the method -- public void gotLocation(Location location) {  

Instead of getting inside that method, it goes straight to postexecute!!


 public void gotLocation(Location location) {  ----  it is a method inside the abstract class LocationResult .. the cllass link is given below(same as given in previous post)
http://pastebin.com/PAs2FQXv
protected Void doInBackground(String... args) {
			Looper.prepare();
			boolean isLoc = myLocation.getLocation(LocationServices.this,locationResult);
			System.out.println("testing whether location is enabled" + isLoc);
			try {
				if (isLoc) {
					System.out.println("testing whether location is enabled inside");
					locationResult = new LocationResult() {

						public void gotLocation(Location location) {
							// TODO Auto-generated method stub
							System.out.println("Progress dialog should be present now - latitude"+ location.getLatitude());
							System.out.println("Progress dialog should be present now - longitude"+ location.getLongitude());
						}
					};

				}
			} catch (Exception e) {
				System.out.println("exception is" + e);
			}
			return (null);
		}

Open in new window

OWASP: Forgery and Phishing

Learn the techniques to avoid forgery and phishing attacks and the types of attacks an application or network may face.

Sreejith22Author Commented:
gone?
ValeriCommented:
I think that this line:
boolean isLoc = myLocation.getLocation(LocationServices.this,locationResult);
initializes member LocationResult locationResult; in MyLocation(!!!) class with null, because it's still not initialized, that's way gotLocation(... is never invoked.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
garypfirstechCommented:
In your revised version, your are invoking myLocation.getLocation too soon as pointed out by Valeri in the previous post.  Your original version was much better.

Going back to your original version, I would add System.out.println statements in there like this:

protected Void doInBackground(String... args) {
                        System.out.println("Got to doInBackground.");
                        Looper.prepare();
                        locationResult = new LocationResult() {
                                public void gotLocation(Location location) {
                                        // TODO Auto-generated method stub
                                        // LocationServices.this.location = location;
                                        System.out.println("Progress dialog should be present now - latitude"+location.getLatitude());
                                       System.out.println("Progress dialog should be present now - longitude"+location.getLongitude());
                                }
                        };
                        System.out.println("About to invoke getLocation()");
                        myLocation.getLocation(LocationServices.this, locationResult);
                        System.out.println("Return from getLocation()");
                        return (null);
                }

Open in new window


Also, I'm not sure why you have the Looper.prepare() call in there but that's another story.
garypfirstechCommented:
More important regarding your original question is the fact that I think there are issues with your MyLocation class.  The getLocation method will return as soon as it schedules GetLastLocation -- not when GetLastLocation executes and returns.  Your call to timer1.schedule(new GetLastLocation(), 20000) returns right away but schedules GetLastLocation to launch 20 seconds after the call is made.  So your method call to getLastLocation is returning right away but getLastLocation is not running until later.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Android

From novice to tech pro — start learning today.