Browse DevX
Sign up for e-mail newsletters from DevX


Location-Based Services Using CellID in Android

Though GPS functionality has become hugely popular, it doesn't work indoors and not all devices have built-in GPS receivers. How do you get around this? Learn how to find and use CellID to locate your Android device and use Google maps to display it.




Building the Right Environment to Support AI, Machine Learning and Deep Learning

n earlier article about Android showed you how to interface with GPS receivers to obtain the geographical position of a device. However, not all today's mobile devices have built-in GPS receivers. Moreover, GPS does not work indoors. This seriously limits the usefulness of applications built around GPS technologies. One good way to expand the utility of your LBS services is to use your device's cell ID. This article will show you how to obtain the cell ID of an Android device and then use the Google Maps API to translate this cell ID value into latitude and longitude.

Author's Note: Click here for a discussion on using Cell ID for location-based services using a Windows Mobile device.

Creating the Project

Using Eclipse, create a new Android project and name it CellID. In main.xml, add a <Button> and <TextView> view:

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android= "http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <Button android:id="@+id/displayMap" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Display map of current location" /> <TextView android:id="@+id/status" android:layout_width="fill_parent" android:layout_height="wrap_content" /> </LinearLayout>

Figure 1. How Your App Should Look: The UI of your LBS application.

This will create the layout you see Figure 1.

Because this application requires Internet connectivity along with access to cell location information, add the relevant permission to the AndroidManifest.xml file:

<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="net.learn2develop.CellID" android:versionCode="1" android:versionName="1.0.0"> <application android:icon="@drawable/icon" android:label="@string/app_name"> <activity android:name=".CellID" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> </manifest>

In the CellID.java file, import the following packages:

import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.net.HttpURLConnection; import java.net.URL; import java.net.URLConnection; import android.app.Activity; import android.content.Intent; import android.net.Uri; import android.os.Bundle; import android.telephony.gsm.GsmCellLocation; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.TextView;


Visit the Android Dev Center


Thanks for your registration, follow us on our social networks to keep up-to-date