Link to home
Create AccountLog in
Avatar of Sreejith22
Sreejith22Flag for India

asked on

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
Avatar of Sreejith22
Sreejith22
Flag of India image

ASKER

Hi

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

Avatar of garypfirstech
garypfirstech

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()?
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

gone?
ASKER CERTIFIED SOLUTION
Avatar of Valeri
Valeri
Flag of Bulgaria image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
SOLUTION
Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
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.