The Friend Finder - MapActivity using GPS - PART: I / II

Tutorials that use the MapActivity. Many using GPS functionality.

The Friend Finder - MapActivity using GPS - PART: I / II

Postby plusminus » Thu Nov 22, 2007 9:04 pm

[align=center]The Friend Finder - MapActivity using GPS - Part: I / II [/align]
[align=center]See also: >> Part II / II <<[/align]
[align=center]View: >> Full Source for this Tutorial <<[/align]

What is this: This tutorial shows how to create a relatively complex application, that takes advantage of LocationManager (GPS-Access), to show your contacts on a Map.

What you will learn:
  • You will learn how to extend the MapActivity to create a Map-Application.
  • Use the LocationManager to track your GPS-Position.
  • Work with Cursors to grab data from the contents.
  • Override the onFreeze(), onResume(), onXYZ()-methods which are i.e. called, when another activity gets on top of ours.
  • Draw things on the MapView using an OverlayController.
  • How to create menus.
  • Many more.....

:?: Problems/Questions: post right below...

Difficulty: 3.5 of 5 :)

What it will look like:
1. Always updating Screen
2. FriendFinder-Map toggled to SatelliteView
3. FriendFinder-Map toggled to Streets-View (also updating ^^ own position got outside of the map)
[align=center]Image
Image Image[/align]

Description:
So the first thing as always is, to think about what we want to do and what we need for that...
  • We want a ListView of our Contacts --> We need a ListView (awesome conclusion... :roll: )
  • We want to do sth. with geo-Location(GPS) --> We need a LocationManager and his friends ;)
  • We want to get the location of our friends --> We need a place to define them!
  • We want to display a Map --> We need a second (Map)Activity
    a. We need to register that second Activity
  • We want to draw sth over the Map --> We need an OverlayController


:idea: As we need to store the GPS-Location of our contacts somewhere, I chose the Notes-Section in each Contacts-Profile. Saving a GPS-location is pretty simple, as we choose a well-structured style of data: a URI-String, which can easily found and extracted from the Notes-String using Regular Expressions. (all that is explained in details below)

[align=center] As a human you can see the format of the URI: "geo:[-]XXX.XXXXX,[-]XXX.XXXXXX#".
Simply setup two or three sample-contacts (with different GPS-Locations) just like in the image below.
Or take these:
geo:37.402346,-122.075014# (Google-Headquarters, SF, California)
geo:37.444608,-122.216034# (McDonalds, :roll: near MountainView, SF, California)
geo:37.41622,-122.089919# (Starbucks, somewhere in SF, California)
or get your own on GoogleMaps (Hover the "Link to this Page"-Link)

Image[/align]

[align=center]>> Part I / II <<[/align]
[align=center]GoTo: >> Part II / II <<[/align]

0. Create an new Android-Project (HowTo).
:idea: I called it "FriendFinder" as that is an extremely perfect name :roll:

As we are planning to access the Contacts of our Android-Phone, we make that public in the AndroidManifest.xml . If we wouldn't do that, our Application would 'crash'(Exception followed by App-End) right when we would try to access the Contacts for the first time.
Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1.    ...
  2.  
  3. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  4.  
  5.    package="org.anddev.android.friendfinder">
  6.  
  7.     <uses-permission id="android.permission.READ_CONTACTS"/>
  8.  
  9.     <application android:icon="@drawable/icon">
  10.  
  11.         <activity class=".FriendFinder" android:label="@string/main_title">
  12.  
  13.    ...
Parsed in 0.001 seconds, using GeSHi 1.0.8.4

<?xml version="1.0" encoding="utf-8"?>

We need to define all the Strings we are going to use in our application in the 'res/values/strings.xml'.
(We could hardcode them too, but that would be very bad :cry: )
Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1. <resources>
  2.  
  3.     <string name="main_title">FriendFinder - anddev.org</string>
  4.  
  5.     <string name="main_menu_open_map">Open visual FriendTracker</string>
  6.  
  7.     <string name="main_list_format">$name ($distance km)</string>
  8.  
  9.     <string name="main_list_geo_not_set">not set</string>
  10.  
  11.     <string name="map_title">FriendFinder - anddev.org</string>
  12.  
  13.     <string name="map_menu_zoom_in">Zoom in (Key: I)</string>
  14.  
  15.     <string name="map_menu_zoom_out">Zoom out (Key: O)</string>
  16.  
  17.     <string name="map_menu_back_to_list">Back to list</string>
  18.  
  19.     <string name="map_menu_toggle_street_satellite">Toggle View: Street / Satellite (Key: T)</string>
  20.  
  21.     <string name="map_overlay_own_name">Me</string>
  22.  
  23. </resources>
Parsed in 0.003 seconds, using GeSHi 1.0.8.4


We will also have a class: 'Friend' (in Friend.java) which combines a Name with a Location:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. public class Friend{
  2.  
  3.         public Location itsLocation = null;
  4.  
  5.         public String itsName = null;
  6.  
  7.         public Friend(Location aLocation, String aName){
  8.  
  9.                 this.itsLocation = aLocation;
  10.  
  11.                 this.itsName = aName;
  12.  
  13.         }
  14.  
  15. }
Parsed in 0.031 seconds, using GeSHi 1.0.8.4


1. So lets start where all Activities start:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.         /** Called when the activity is first created. */
  2.  
  3.         @Override
  4.  
  5.         public void onCreate(Bundle icicle) {
  6.  
  7.                 super.onCreate(icicle);
  8.  
  9.  
  10.  
  11.                 /* The first thing we need to do is to setup our own
  12.  
  13.                  * locationManager, that will support us with our own gps data */
  14.  
  15.                 this.myLocationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
  16.  
  17.  
  18.  
  19.                 /* Update the list of our friends once on the start,
  20.  
  21.                  * as they are not(yet) moving, no updates to them are necessary */
  22.  
  23.                 this.refreshFriendsList();
  24.  
  25.                
  26.  
  27.                 /* Initiate the update of the contactList
  28.  
  29.                  * manually for the first time */
  30.  
  31.                 this.updateList();
  32.  
  33.  
  34.  
  35.                 /* Prepare the things, that will give
  36.  
  37.                  * us the ability, to receive Information
  38.  
  39.                  * about our GPS-Position. */
  40.  
  41.                 this.setupForGPSAutoRefreshing();
  42.  
  43.         }
Parsed in 0.032 seconds, using GeSHi 1.0.8.4


2. Lets dig deeper into the refreshFriendsList()-Method, which will grab our Contacts/Friends out of Androids Database using a query/Cursor.
As it is a pretty big function doing some advanced logic, we will split into pieces here.

The first thing we will do is to grab a so called Cursor. I'll try to explain that referring to relational databases and their SQL (Structured Query Language).
A Cursor is kinda like the result of a SQL-Query. In this case we make a query on 'People.CONTENT_URI' what would referr to sth. like 'SELECT * FROM 'Contacts'.
The next 3 arguments remain 'null', we could have filled them stuff like the equivalent to 'WHERE Name='Peter''...
The last field contains the Sort-Order we want to receive. Here the similarity to SQL is pretty obvious. We chose sorting the results by People.NAME in an ASCending order.
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.         /** List of friends in */
  2.  
  3.         protected ArrayList<Friend> allFriends = new ArrayList<Friend>();
  4.  
  5.         // ...
  6.  
  7.  
  8.  
  9.         private void refreshFriendsList(){
  10.  
  11.                 Cursor c = getContentResolver().query(People.CONTENT_URI,
  12.  
  13.                                 null, null, null, People.NAME + " ASC");
  14.  
  15.                 /* This method allows the activity to take
  16.  
  17.                  * care of managing the given Cursor's lifecycle
  18.  
  19.                  * for you based on the activity's lifecycle. */
  20.  
  21.                 this.startManagingCursor(c);
  22.  
  23.  
  24.  
  25.                 int notesColumn = c.getColumnIndex(People.NOTES);
  26.  
  27.                 int nameColumn = c.getColumnIndex(People.NAME);
  28.  
  29.  
  30.  
  31.                 /* This will take all our contacts transformed to Strings */
  32.  
  33.                 ArrayList<String> listItems = new ArrayList<String>();
Parsed in 0.037 seconds, using GeSHi 1.0.8.4

We are now going to loop through every contact that the Cursor found/'is providing' and check whether the Notes-String of that Contact somwhere contains a 'geo:XX...XX#' within.
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.                 // Moves the cursor to the first row
  2.  
  3.                 // and returns true if there is sth. to get
  4.  
  5.                 if (c.first()) {
  6.  
  7.                         do {           
  8.  
  9.                                 String notesString = c.getString(notesColumn);
  10.  
  11.                                
  12.  
  13.                                 Location friendLocation = null;
Parsed in 0.036 seconds, using GeSHi 1.0.8.4

If there was anything saved within the Notes of this contact, we now check whether there is a 'geo:XX...XX#' within the Notes-String. If we wound one, we extract the Latitude/Longitude and save it to 'friendLocation'.
[align=center]Learn about Regular Expressions in Java.[/align]
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.                                 if (notesString != null) {
  2.  
  3.                                         // Pattern for extracting geo-ContentURIs from the notes.
  4.  
  5.                                         final String geoPattern = "(geo:[\\-]?[0-9]{1,3}\\.[0-9]{1,6}\\,[\\-]?[0-9]{1,3}\\.[0-9]{1,6}\\#)";
  6.  
  7.                                         // Compile and use regular expression
  8.  
  9.                                         Pattern pattern = Pattern.compile(geoPattern);
  10.  
  11.  
  12.  
  13.                                         CharSequence inputStr = notesString;
  14.  
  15.                                         Matcher matcher = pattern.matcher(inputStr);
  16.  
  17.  
  18.  
  19.                                         boolean matchFound = matcher.find();
  20.  
  21.                                         if (matchFound) {
  22.  
  23.                                                 // We take the first match available
  24.  
  25.                                                 String groupStr = matcher.group(0);
  26.  
  27.                                                 // And parse the Lat/Long-GeoPos-Values from it
  28.  
  29.                                                 friendLocation = new Location();
  30.  
  31.                                                 String latid = groupStr.substring(groupStr.indexOf(":") + 1,
  32.  
  33.                                                                 groupStr.indexOf(","));
  34.  
  35.                                                 String longit = groupStr.substring(groupStr.indexOf(",") + 1,
  36.  
  37.                                                                 groupStr.indexOf("#"));
  38.  
  39.                                                 friendLocation.setLongitude(Float.parseFloat(longit));
  40.  
  41.                                                 friendLocation.setLatitude(Float.parseFloat(latid));
  42.  
  43.                                         }
  44.  
  45.                                 }
Parsed in 0.040 seconds, using GeSHi 1.0.8.4

So, if the friendLocation is passed can be not null (if we found a 'geo...# within the notes), but can also be still as we found nothing. Now we close the loop to iterate over every Contact.
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.                                 if(friendLocation != null){
  2.  
  3.                                         String friendName = c.getString(nameColumn);
  4.  
  5.                                         allFriends.add(new Friend(friendLocation, friendName));
  6.  
  7.                                 }
  8.  
  9.                         } while (c.next());
Parsed in 0.037 seconds, using GeSHi 1.0.8.4

:idea: All of our friends are now stored to the field 'this.allFriends'.

3. Now lets hop into the next function that had been called in onCreate(...):
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.                 /* Initiate the update of the contactList
  2.  
  3.                  * manually for the first time */
  4.  
  5.                 this.updateList();
Parsed in 0.036 seconds, using GeSHi 1.0.8.4

This method will display all contacts + their distance to ourself, as you've seen it in the animated GIF above.
The first thing we are doing with in is to get our own GPS-Location.
:?: Question: How will that work, we are in an emulator, with no gps-receiver connected :?:
:idea: Answer: Within the emulator there is running a pseudo-gps-receiver, that simulates a person driving/walking around in San Francisco, close to the Google-Headquarters! :roll:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.         private void updateList() {
  2.  
  3.                 // Refresh our location...
  4.  
  5.                 this.myLocation = myLocationManager.getCurrentLocation("gps");
Parsed in 0.037 seconds, using GeSHi 1.0.8.4

So, 'this.myLocation' is of the type "Location" which now contains mainly a "Longitude and Latitude"-Value, what together is our exact location on planet earth.

Now we loop over 'this.allFriends' and determine our distance towards each and save it in a ArrayList<String>, IF they are not farer away than the static field: NEARFRIEND_MAX_DISTANCE.
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.         private void updateList() {
  2.  
  3.                 // Refresh our location...
  4.  
  5.                 this.myLocation = myLocationManager.getCurrentLocation("gps");
  6.  
  7.                
  8.  
  9.                 ArrayList<String> listItems = new ArrayList<String>();
  10.  
  11.                
  12.  
  13.                 // For each Friend
  14.  
  15.                 for(Friend aNearFriend : this.allFriends){
  16.  
  17.                         /* Load the row-entry-format defined as a String
  18.  
  19.                          * and replace $name with the contact's name we
  20.  
  21.                          * get from the cursor */
  22.  
  23.                         String curLine = new String(getString(R.string.main_list_format));
  24.  
  25.                         curLine = curLine.replace("$name", aNearFriend.itsName);
  26.  
  27.                        
  28.  
  29.                         if(aNearFriend.itsLocation != null){
  30.  
  31.                                 if( this.myLocation.distanceTo(aNearFriend.itsLocation) <
  32.  
  33.                                                                         NEARFRIEND_MAX_DISTANCE){
  34.  
  35.                                         final DecimalFormat df = new DecimalFormat("####0.000");
  36.  
  37.                                         String formattedDistance =
  38.  
  39.                                                 df.format(this.myLocation.distanceTo(
  40.  
  41.                                                                                 aNearFriend.itsLocation) / 1000);
  42.  
  43.                                         curLine = curLine.replace("$distance", formattedDistance);
  44.  
  45.                                 }
  46.  
  47.                         }else{
  48.  
  49.                                 curLine = curLine.replace("$distance",
  50.  
  51.                                                 getString(R.string.main_list_geo_not_set));
  52.  
  53.                         }
  54.  
  55.                        
  56.  
  57.                         listItems.add(curLine);
  58.  
  59.                 }
  60.  
  61.  
  62.  
  63.                 ArrayAdapter<String> notes =  new ArrayAdapter<String>(this,
  64.  
  65.                                 android.R.layout.simple_list_item_1, listItems);
  66.  
  67.  
  68.  
  69.                 this.setListAdapter(notes);
  70.  
  71.         }
Parsed in 0.041 seconds, using GeSHi 1.0.8.4

Now we only have to apply that ArrayList<String> to be put into our ListView (what we can see actually) with the following lines:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.                 ArrayAdapter<String> notes =  new ArrayAdapter<String>(this,
  2.  
  3.                                 android.R.layout.simple_list_item_1, listItems);
  4.  
  5.  
  6.  
  7.           /* Save the index of the selected item
  8.  
  9.            * (if there has been an ListAdapter set before <img src="http://www.anddev.org/images/smilies/icon_exclaim.gif" alt=":!:" title="Exclamation" />) */
  10.  
  11.           long beforeIndex = 0;
  12.  
  13.           if(this.getListAdapter() != null)
  14.  
  15.                beforeIndex = this.getSelectionRowID();
  16.  
  17.                
  18.  
  19.           this.setListAdapter(notes);
  20.  
  21.  
  22.  
  23.           // Try to apply the saved selection-index
  24.  
  25.           try{
  26.  
  27.                this.setSelection((int)beforeIndex);
  28.  
  29.           }catch (Exception e){}
  30.  
  31.         }
Parsed in 0.038 seconds, using GeSHi 1.0.8.4


We could start our Application by now but the values would not yet update. But as we want them to update like this:
[align=center]Image[/align]
We need to do some more "System"-Stuff. Don't panic, there will be ~3x comments than Lines of real code.

4. Remembering the onCreate(..)-method from the very beginning, there was another method left within:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.                 /* Prepare the things, that will give
  2.  
  3.                  * us the ability, to receive Information
  4.  
  5.                  * about our GPS-Position. */
  6.  
  7.                 this.setupForGPSAutoRefreshing();
Parsed in 0.036 seconds, using GeSHi 1.0.8.4


So lets get into that heavily commented function:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2.         protected final long MINIMUM_DISTANCECHANGE_FOR_UPDATE = 25; // in Meters
  3.  
  4.         protected final long MINIMUM_TIME_BETWEEN_UPDATE = 2500; // in Milliseconds
  5.  
  6.         // ....
  7.  
  8.  
  9.  
  10.         /** Register with our LocationManager to send us
  11.  
  12.          * an intent (who's Action-String we defined above)
  13.  
  14.          * when  an intent to the location manager,
  15.  
  16.          * that we want to get informed on changes to our own position.
  17.  
  18.          * This is one of the hottest features in Android.
  19.  
  20.          */
  21.  
  22.         private void setupForGPSAutoRefreshing() {
  23.  
  24.  
  25.  
  26.                 // Get the first provider available
  27.  
  28.                 List<LocationProvider> providers = this.myLocationManager.getProviders();
  29.  
  30.                 LocationProvider provider = providers.get(0);
  31.  
  32.                
  33.  
  34.                 this.myLocationManager.requestUpdates(provider, MINIMUM_TIME_BETWEEN_UPDATE,
  35.  
  36.                                                                         MINIMUM_DISTANCECHANGE_FOR_UPDATE,
  37.  
  38.                                                                         new Intent(MY_LOCATION_CHANGED_ACTION));
  39.  
  40.                
  41.  
  42.                
  43.  
  44.                 /* Create an IntentReceiver, that will react on the
  45.  
  46.                  * Intents we said to our LocationManager to send to us. */
  47.  
  48.                 this.myIntentReceiver = new MyIntentReceiver();
  49.  
  50.  
  51.  
  52.                 /*
  53.  
  54.                  * In onResume() the following method will be called:
  55.  
  56.                  * registerReceiver(this.myIntentReceiver, this.myIntentFilter);
  57.  
  58.                  */
  59.  
  60.         }
Parsed in 0.039 seconds, using GeSHi 1.0.8.4

We used the following specialized IntentReceiver above, that will simply call our updateList()-method when it receives an Intent. (As we will apply our filter to it, it will only receive 'MY_LOCATION_CHANGED_ACTION'-Intents)
The following to Classes/Objects were used above and are working "as a team" :roll: :
"( registerReceiver(this.myIntentReceiver, this.myIntentFilter); ")

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.         protected final IntentFilter myIntentFilter =  new IntentFilter(MY_LOCATION_CHANGED_ACTION);
  2.  
  3.         //....
  4.  
  5.  
  6.  
  7.         /**
  8.  
  9.          * This tiny IntentReceiver updates
  10.  
  11.          * our stuff as we receive the intents
  12.  
  13.          * (LOCATION_CHANGED_ACTION) we told the
  14.  
  15.          * myLocationManager to send to us.
  16.  
  17.          */
  18.  
  19.         class MyIntentReceiver extends IntentReceiver {
  20.  
  21.                 @Override
  22.  
  23.                 public void onReceiveIntent(Context context, Intent intent) {
  24.  
  25.                         if(FriendFinder.this.doUpdates)
  26.  
  27.                                 // Will simply update our list, when receiving an intent
  28.  
  29.                                 FriendFinder.this.updateList();
  30.  
  31.                 }
  32.  
  33.         }
Parsed in 0.038 seconds, using GeSHi 1.0.8.4


5. The last thing we would have to do is to overwrite the onResume() and onFreeze(...)-Functions, as we want to (un)register as the Application is the top-most or not. (Prevents unnecessary CPU-Usage and therefor Battery-Consumption).
[align=center]:idea: For further information about those onWhatEver()-Methods have a look here: Lifecycle of an Activity.[/align]
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.         /**
  2.  
  3.          * Restart the receiving, when we are back on line.
  4.  
  5.          */
  6.  
  7.         @Override
  8.  
  9.         public void onResume() {
  10.  
  11.                 super.onResume();
  12.  
  13.                 this.doUpdates = true;
  14.  
  15.                
  16.  
  17.                 /* As we only want to react on the LOCATION_CHANGED
  18.  
  19.                  * intents we made the OS send out, we have to
  20.  
  21.                  * register it along with a filter, that will only
  22.  
  23.                  * "pass through" on LOCATION_CHANGED-Intents.
  24.  
  25.                  */
  26.  
  27.                 this.registerReceiver(this.myIntentReceiver, this.myIntentFilter);
  28.  
  29.         }
  30.  
  31.        
  32.  
  33.         /**
  34.  
  35.          * Make sure to stop the animation when we're no longer on screen,
  36.  
  37.          * failing to do so will cause a lot of unnecessary cpu-usage!
  38.  
  39.          */
  40.  
  41.         @Override
  42.  
  43.         public void onFreeze(Bundle icicle) {
  44.  
  45.                 this.doUpdates = false;
  46.  
  47.                 this.unregisterReceiver(this.myIntentReceiver);
  48.  
  49.                 super.onFreeze(icicle);
  50.  
  51.         }
Parsed in 0.038 seconds, using GeSHi 1.0.8.4


[align=center]You find the full code at the very very very end of this post.
:!: Congratulations, you've just created a auto-refreshing, gps-using Killer-Application :!:
[/align]

But this was just the first part! We want to do some Map-Stuff :!:

[align=center]GoTo: >> Part II / II <<[/align]

[align=center]View: >> Full Source for this Tutorial <<[/align]

Regards,
plusminus
Last edited by plusminus on Sat Jan 26, 2008 10:07 am, edited 1 time in total.
Image
Image | Android Development Community / Tutorials
User avatar
plusminus
Site Admin
Site Admin
 
Posts: 2688
Joined: Wed Nov 14, 2007 8:37 pm
Location: Schriesheim, Germany

Top

Postby aetmos » Sat Jan 26, 2008 4:27 am

As it is a pretty big function doing some advanced logic, we will split into peaces here.


s/peaces/pieces/g

We chosse sorting the results by People.NAME in an ASCending order.


s/chosse/chose/g

Excellent tutorial, by the way. Thanks.

Tom

P.S. Not trying to be picky, just pointing out some typos...
aetmos
Junior Developer
Junior Developer
 
Posts: 21
Joined: Fri Jan 18, 2008 12:33 am

Postby plusminus » Sat Jan 26, 2008 10:08 am

Hello aetmos,

awww no, those typos were really painful :P
Definitely a result of less sleep in that time ;)

Regards,
plusminus
Image
Image | Android Development Community / Tutorials
User avatar
plusminus
Site Admin
Site Admin
 
Posts: 2688
Joined: Wed Nov 14, 2007 8:37 pm
Location: Schriesheim, Germany

Postby russoue » Mon Jan 28, 2008 8:59 am

Thanks plusminus for the excellent tutorial! It was really helpful.
russoue
Freshman
Freshman
 
Posts: 7
Joined: Fri Nov 16, 2007 6:12 pm

Postby coding_android » Thu May 08, 2008 2:16 pm

I'd like to say thank you for that great app. I'm diving into the Android experience and you're tutorials are very helpful. :)
User avatar
coding_android
Moderator
Moderator
 
Posts: 76
Joined: Mon May 05, 2008 10:22 am
Location: Germany

Postby buster » Thu May 08, 2008 2:33 pm

As the Interface for the contacts seems to have changed (your screenshot which shows "Notes" looks completely different) i have a few questions:
1. when adding a contact, i noticed i can add custom fields to contacts. Obviously i could just create a "Gps" field with the location. But how do i access those custom fields within a cursor? Also, i'm wondering how this is working, as some contacts may have a gps field, others may not. From the SQL point of view there are two possibilites: Android adds a new column to the contacts which could result in many unused fields on other contacts. Or it could create a foreign table.. Anyways i'm wondering how i get this data.
The line of interest would be: "int notesColumn = c.getColumnIndex(People.NOTES); "
Obviously People.GPS doesnt work..

Thank you in advance (and for this great site!)

Sebastian
buster
Junior Developer
Junior Developer
 
Posts: 23
Joined: Tue Apr 22, 2008 4:14 pm
Location: Berlin, Germany

Top

Postby ca050306 » Wed May 21, 2008 6:16 pm

Hi Sebastian

I have the same issue, I created the custom fiel Notes but the function
int notesColumn = c.getColumnIndex(People.NOTES); return 1 and the string inside is null

It does not work as you wrote, if you find the solution let me know
BR
CA


buster wrote:As the Interface for the contacts seems to have changed (your screenshot which shows "Notes" looks completely different) i have a few questions:
1. when adding a contact, i noticed i can add custom fields to contacts. Obviously i could just create a "Gps" field with the location. But how do i access those custom fields within a cursor? Also, i'm wondering how this is working, as some contacts may have a gps field, others may not. From the SQL point of view there are two possibilites: Android adds a new column to the contacts which could result in many unused fields on other contacts. Or it could create a foreign table.. Anyways i'm wondering how i get this data.
The line of interest would be: "int notesColumn = c.getColumnIndex(People.NOTES); "
Obviously People.GPS doesnt work..

Thank you in advance (and for this great site!)

Sebastian
ca050306
Freshman
Freshman
 
Posts: 6
Joined: Sat May 17, 2008 3:31 pm

adding contacts for FriendFinder application

Postby tannadevang » Thu Jun 05, 2008 4:02 am

Can you tell me where should the contacts be added for the FriendFinder application to work, i see this error .

Application Error:
com.android.friendfinder

An error has occured in com.android.friendfinder.
Unable to start activity
ComponenetInfo{com.android.friendfinder/com.andorid.friendfinder.FriendFinder}:
java.lang.SecurityException:
Permission
android.permission.READ_CONTACTS required for provider contacts.

i tried adding contacts name as the geo location but doesn thelp :(


thanks,
devang
tannadevang
Freshman
Freshman
 
Posts: 2
Joined: Sun May 11, 2008 11:06 pm
Location: San Jose

Postby buster » Thu Jun 05, 2008 9:13 am

you need to add the proper permission in your manifest.xml as the error already says ;)
Open the Manifest.xml in Eclipse (you should see some good UI coming up, the Manifest Editor) and add that permission to your Activity.
buster
Junior Developer
Junior Developer
 
Posts: 23
Joined: Tue Apr 22, 2008 4:14 pm
Location: Berlin, Germany

Postby cbraun75 » Mon Jun 09, 2008 2:10 pm

Just have a Question for your Friend Nearby variable ...
I have the problem that i must now if this Locationpoint is the same location point i have in my Array. So I'm driving arround and if my position is in the radius of x meters or miles around a position which is in this array i'm doing something.

So I have
- me with a moving position and every x seconds i ask the array
- the array with a lot of saved positions
- a radius around the saved positions in the array (in this radius every request get true back)


Has anybody an idea in which way i can calculate this? How can i calculate the whole positions around the point which are accepted too?

Or some other ideas to solve it in a different way so that i don't need the array of fixed waypoints?
cbraun75
Junior Developer
Junior Developer
 
Posts: 23
Joined: Mon Jan 28, 2008 9:12 pm
Location: Munich

Postby zak » Fri Sep 26, 2008 9:49 am

hi, i have a question. In my android library there isn't com.google.android.maps.OverlayController class. where can i find it? thanks!
zak
Freshman
Freshman
 
Posts: 8
Joined: Tue Sep 23, 2008 8:42 am

Postby ninor » Fri Sep 26, 2008 12:02 pm

zak wrote:hi, i have a question. In my android library there isn't com.google.android.maps.OverlayController class. where can i find it? thanks!


This has changed to:
com.google.android.maps.Overlay
and
com.google.android.maps.OverlayItem

Search this site for some samples.

CU!
User avatar
ninor
Moderator
Moderator
 
Posts: 180
Joined: Thu Aug 14, 2008 6:30 pm
Location: Barcelona, Spain

Are the "friends" location static or dynamic?

Postby ppcinfo » Mon Nov 03, 2008 9:03 pm

I'm very new to Android app programming, so please forgive me if my question seems too basic.

I've found several apps that talk about mapping out friend locations, so I need to ask:

Is it possible for an app on my G1 phone to identify the current location of a friend who also has a G1 phone? In other words, is the "friend" location dynamic?

I think the answer is "no", since I don't know how an app on my phone would be able to detect the location of another person with a G phone. Just wanted to clarify that.

thanks.
ppcinfo
ppcinfo
Junior Developer
Junior Developer
 
Posts: 14
Joined: Mon Nov 03, 2008 7:47 pm

Postby lucius.fox08 » Mon Dec 15, 2008 3:09 am

How can I add the mock geo location of each contact in android sdk 1.0 rc2?

I don' see the 'Notes' Session in the contacts in the emulator.

Thank you.
lucius.fox08
Freshman
Freshman
 
Posts: 2
Joined: Mon Dec 15, 2008 3:05 am

Postby parimalraj2000 » Fri Jan 23, 2009 12:51 pm

Hello,
I am trying to access the phone book conatcts. It is working fine in SDK, but when ever I am trying to run it in actual phone the code is crashing.

Code:
private String CheckForNameFromContacts(String display_name)
{

try
{
Cursor c = getContentResolver().query(People.CONTENT_URI, null, null, null, null);

if (c.moveToFirst()) {

String name = null;

int phoneid = 0;

String phoneno = null;

do {

name = c.getString(c.getColumnIndex(People.NAME));

if(name.equals(display_name))

{

Toast.makeText(this,name,Toast.LENGTH_LONG);

phoneid = c.getInt(c.getColumnIndex(People.PRIMARY_PHONE_ID));

phoneno = c.getString(c.getColumnIndex(People.NUMBER));


return phoneno;
}

} while (c.moveToNext());

return null;
}
}
catch (Exception e) {
Toast.makeText(OpenSocial_Friends.this, "Exception: Couldn't access contacts"+ e.getMessage(), Toast.LENGTH_LONG).show();

return null;
}
return null;

}


Please help me getting it solved.

Regards,
Parimal.
parimalraj2000
Junior Developer
Junior Developer
 
Posts: 18
Joined: Tue Nov 04, 2008 10:01 am

Top
Next

Return to Map Tutorials

Who is online

Users browsing this forum: No registered users and 1 guest