UI object in UI thread

Put problem concerning MapActivities, MapViews, Overlays and GPS in this forum.

UI object in UI thread

Postby andykalbvleesch » Wed Jan 21, 2009 6:44 pm

I am making an application that use skyhoo technology. I am pretty new to java and apparently I am making a mistake: I am modifying a UI object in a non-UI thread at:

printLocation(location.getLatitude(), location.getLongitude());

I heard I should use a handler and did look at that but wonder if there is a simpler way ? I have no clue how to implement a handler... Second I dont know what is should use in private done()


package nl.freshsolutions.kmregistratie;


import java.text.DateFormat;
import java.util.Date;

import com.skyhookwireless.wps.WPS;
import com.skyhookwireless.wps.WPSAuthentication;
import com.skyhookwireless.wps.WPSContinuation;
import com.skyhookwireless.wps.WPSLocation;
import com.skyhookwireless.wps.WPSLocationCallback;
import com.skyhookwireless.wps.WPSReturnCode;
import com.skyhookwireless.wps.WPSStreetAddressLookup;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.content.Context;
import android.content.Intent;

public class aanmaaknieuw extends Activity {

private EditText mystart;
private EditText myend;
private EditText mydistance;
private EditText mydiscription;
private EditText mydate;
private Long mRowId;
private NotesDbAdapter mDbHelper;
private static final int ACTIVITY_OVERZICHT=1;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);

mDbHelper = new NotesDbAdapter(this);
mDbHelper.open();

setContentView(R.layout.aanmaaknieuw);

mystart = (EditText) findViewById(R.id.start);
myend = (EditText) findViewById(R.id.end);
mydistance = (EditText) findViewById(R.id.distance);
mydiscription = (EditText) findViewById(R.id.discription);
mydate = (EditText) findViewById(R.id.date);

Date now = new Date();
DateFormat df = DateFormat.getDateInstance();
String s = df.format(now);
mydate.setText(s);

Button safe = (Button) findViewById(R.id.safe);


safe.setOnClickListener(new View.OnClickListener() {

public void onClick(View view) {

String start = mystart.getText().toString();
String end = myend.getText().toString();
String distance = mydistance.getText().toString();
String discription = mydiscription.getText().toString();
String date = mydate.getText().toString();

if (mRowId == null) {
long id = mDbHelper.createNote(start, end, distance, discription, date);
if (id > 0) {
mRowId = id;
okaytextvullen();
}
} else {
//do some errorhandling of een andere actie
}
}

});


Button getlocation = (Button) findViewById(R.id.getlocation);

getlocation.setOnClickListener(new View.OnClickListener() {

public void onClick(View view) {


Context myAndroidContext = view.getContext();
// Create the authentication object
// myAndroidContext must be a Context instance
WPS wps = new WPS(myAndroidContext);
WPSAuthentication auth = new WPSAuthentication("andykalbvleesch", "freshsolutions.nl");


// Callback object
WPSLocationCallback callback = new WPSLocationCallback()
{
// What the application should do after it's done
public void done()
{
// after done() returns, you can make more WPS calls.

}


// What the application should do if an error occurs
public WPSContinuation handleError(WPSReturnCode error)
{
//handleWPSError(error); // you'll implement handleWPSError()

// To retry the location call on error use WPS_CONTINUE,
// otherwise return WPS_STOP
return WPSContinuation.WPS_STOP;
}


// Implements the actions using the location object
public void handleWPSLocation(WPSLocation location)
{

printLocation(location.getLatitude(), location.getLongitude());

}

};

// Call the location function with callback
wps.getLocation(auth, WPSStreetAddressLookup.WPS_NO_STREET_ADDRESS_LOOKUP,
callback);

}});

}

private void printLocation(double latitude, double longitude) {
String strlat = Double.toString(longitude)+ " - " + Double.toString(latitude);
mystart.setText("Location:" + strlat);

}

private void okaytextvullen()
{
//nog een tekstje dat de data is opgeslagen

Intent i = new Intent(this, overzicht.class);
startActivityForResult(i, ACTIVITY_OVERZICHT);


}

}
andykalbvleesch
Junior Developer
Junior Developer
 
Posts: 10
Joined: Tue Jan 20, 2009 2:36 pm

Top

Postby MrSnowflake » Wed Jan 21, 2009 7:22 pm

Make printLocation() do this:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. runOnUiThread(new Runnable() {
  2.  
  3.   public void run() {
  4.  
  5.     String strlat = Double.toString(longitude)+ " - " + Double.toString(latitude);
  6.  
  7.     mystart.setText("Location:" + strlat);
  8.  
  9.   }
  10.  
  11. });
Parsed in 0.031 seconds, using GeSHi 1.0.8.4
User avatar
MrSnowflake
Moderator
Moderator
 
Posts: 1439
Joined: Sat Feb 16, 2008 3:11 pm
Location: Flanders, Belgium

Please insert this code in my code you would help me so much

Postby andykalbvleesch » Wed Jan 21, 2009 7:29 pm

Could you please insert your code in mine and post it all back ? When I try to do what you say I get only errors...
andykalbvleesch
Junior Developer
Junior Developer
 
Posts: 10
Joined: Tue Jan 20, 2009 2:36 pm

I have it

Postby andykalbvleesch » Wed Jan 21, 2009 8:23 pm

man I love you snowflakes....
andykalbvleesch
Junior Developer
Junior Developer
 
Posts: 10
Joined: Tue Jan 20, 2009 2:36 pm

Postby MrSnowflake » Wed Jan 21, 2009 8:49 pm

I hope you relalize it's MrSnowflake :)
User avatar
MrSnowflake
Moderator
Moderator
 
Posts: 1439
Joined: Sat Feb 16, 2008 3:11 pm
Location: Flanders, Belgium

I am not worth thy... master MrSnowflake

Postby andykalbvleesch » Wed Jan 21, 2009 9:02 pm

Thanx.
andykalbvleesch
Junior Developer
Junior Developer
 
Posts: 10
Joined: Tue Jan 20, 2009 2:36 pm

Top

Postby andyedward83 » Fri Jun 05, 2009 4:50 am

Hi andykalbvleesch, can you show how you manage to put the library files into the right directory?

how about a simple step-by-step guide in installing the skyhook api to the android project?

rgdrs,

andyedward
andyedward83
Freshman
Freshman
 
Posts: 4
Joined: Sun May 03, 2009 5:00 pm

Postby andykalbvleesch » Fri Jun 05, 2009 10:41 am

when you create a new project in eclipse you just add the wpsapi.jar in the same folder as your project. You can then use the skyhook technology. I have posted a working piece of sample code in the skyhook user group.

http://skyhook.googlegroups.com/web/XPS ... XZc5buSfmx

However to be honest, I wouldn't spent too much time on skyhook. The problem with skyhook is that they haven't covered the whole world yet and in rural areas you never get a fix....
andykalbvleesch
Junior Developer
Junior Developer
 
Posts: 10
Joined: Tue Jan 20, 2009 2:36 pm

Postby andyedward83 » Fri Jun 05, 2009 1:08 pm

Hi andy, cool man..thx a lot..when i downloaded the sdk from the skyhook website, i picked the windows32 platform but i never found the .jar file to put in to my project..

maybe you can point to the correct link (to download the .jar) for others if they are willing to try skyhook..

so you're not using skyhook anymore? for my case as i'm living in singapore, it's quite accurate so i'm giving it a try for my next version of the app..

thx again for .jar

andyedward
andyedward83
Freshman
Freshman
 
Posts: 4
Joined: Sun May 03, 2009 5:00 pm

Postby andykalbvleesch » Fri Jun 05, 2009 1:36 pm

If you download the zipped sample from my post on skyhook platform you can find the .jar as well
andykalbvleesch
Junior Developer
Junior Developer
 
Posts: 10
Joined: Tue Jan 20, 2009 2:36 pm

Top

Return to Map Problems

Who is online

Users browsing this forum: Yahoo [Bot] and 4 guests