Show me the code! – By Davanum Srinivas

November 30, 2007

Android – Poor man’s “My Location” (Show current cell location)

Filed under: Uncategorized — Davanum Srinivas @ 12:28 pm

Many Thanks to Nicholas and bloid for comments on my previous blog post. Here’s an update with some commentary

Step #1: Get the Cell-Id, LAC, MCC and MNC from the cell phone


Set up a PhoneIntentReceiver as shown below:

// Set up the handler for receiving events for service state etc.
mPhoneStateReceiver = new PhoneStateIntentReceiver(this, new ServiceStateHandler());
mPhoneStateReceiver.notifyServiceState(MY_NOTIFICATION_ID);
mPhoneStateReceiver.notifyPhoneCallState(MY_NOTIFICATION_ID);
mPhoneStateReceiver.notifySignalStrength(MY_NOTIFICATION_ID);
mPhoneStateReceiver.registerIntent();

And the handler snippet is below. Note that he MNC/MCC information is returned by the getOperatorNumeric method.

    /**
     * Notification Handler for Phone events
     */
    private class ServiceStateHandler extends Handler {
        private boolean updateOnce = false;

        public void handleMessage(Message msg) {
            Log.i("LocateMe", "In handleMessage : " + msg.what);
            switch (msg.what) {
                case MY_NOTIFICATION_ID:
                    ServiceState state = mPhoneStateReceiver.getServiceState();
                    notification_cid = state.getCid();
                    notification_lac = state.getLac();
                    notification_signal = mPhoneStateReceiver.getSignalStrength();
                    notifiction_operator_numeric = "" + state.getOperatorNumeric();

                    if (updateOnce == false) {
                        updateOnce = true;
                        updateTextFields();
                    }
                    break;
            }
        }
    }

Here’s a screen shot with the default values that you get from the emulator:
001

Since the default information from emulator is unusable, let’s find something we can use say from this site -
“groovy mother…”
Here’s a screen shot:
002

Step #2: Convert the Cell information into an Address


Now the question is how do we get an address information from the cellid/lac/mnc/mcc. The GNUCITIZEN site has a nice tip on how to use the ZoneTag API from Yahoo. So we construct a nice URL with the information entered on screen.
http://zonetag.research.yahooapis.com/services/rest/V1/cellLookup.php?apptoken=ZoneTagDemoToken&cellid=20442&lac=6015&mnc=410&mcc=310

Just Click on the URL above to see the XML that it returns.

Step #3: Convert Address into a Lat/Long


Just use the Yahoo GeoCode API. We parse the XML from the previous step, extract the city, state, zip and construct a new URL.
http://local.yahooapis.com/MapsService/V1/geocode?appid=YahooDemo&city=Boston&state=Massachusetts&zip=02215

Again just Click on the URL above to see the XML that it returns.

Step #4: Launch the Maps

ContentURI uri = ContentURI.create("geo:" + myContentHandler2.getLatitude()
     + "," + myContentHandler2.getLongitude());
Intent intent = new Intent("android.intent.action.VIEW", uri);
startActivity(intent);

003

Piece of cake :)

Download the sources and Application – LocateMe-2.0.zip

UPDATE: December 1, 2007 8 AM EST – Latest version is here

About these ads

9 Comments »

  1. [...] UPDATE: Nov 30, 12:37 EST – Version 2.0 is here [...]

    Pingback by Android - Access Cell Phone details (Cell-Id, LAC, Signal Strength) « Show me the code! — November 30, 2007 @ 12:38 pm

  2. Hi
    I succeed to compile it but I did’t found how to run it…

    I try to put it under Eclipse but import failed.

    I am working under linux.

    Any help welcome.
    Your appli looks very nice and I would like to run it.

    Thank’s

    Comment by Andre — December 1, 2007 @ 11:33 am

  3. When I run the app the just click on the “Locate Me!” button, I get an error message telling “Application error: com.google.android.maps An error has occured in process com.google.android.maps. Unable to start activity [.....]“. Any ideas ?

    Comment by l3v1 — January 3, 2008 @ 5:04 am

  4. [...] Then I build the XML SAX parsing part by referenced from Davanum Srinivas’ LocateMe at: http://davanum.wordpress.com/2007/11/30/android-poor-mans-my-location-show-current-cell-location/ [...]

    Pingback by Mobile Application Code Shogun » Blog Archive » Android GoogleMaps Mini — March 7, 2008 @ 11:43 pm

  5. you can send the locateme for my email?? your link is broken.

    olucio@gmail.com

    thanks!!

    Comment by lucio flavio — October 30, 2009 @ 10:40 am

  6. can you send me the source code? your link is broken

    abouddi@hotmail.com

    thanks

    Comment by boudi — March 24, 2010 @ 3:47 am

  7. can you send me the source code? your link is broken
    thank you

    Comment by wangpeng — May 20, 2010 @ 4:23 am

  8. could you send me the source code?

    Comment by lance — August 23, 2010 @ 7:53 am


RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

The Shocking Blue Green Theme. Blog at WordPress.com.

Follow

Get every new post delivered to your Inbox.

Join 132 other followers

%d bloggers like this: