Jun 4, 2011

Get cell location on a GSM phone, getCellLocation()

TelephonyManager.getCellLocation() return the current location of the device.

We need the following permission in this example:
  • android.permission.ACCESS_COARSE_LOCATION
  • android.permission.ACCESS_FINE_LOCATION
  • android.permission.READ_PHONE_STATE


Get cell location on a GSM phone, getCellLocation()

package com.AndroidTelephonyManager;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.telephony.TelephonyManager;
import android.telephony.gsm.GsmCellLocation;
import android.widget.TextView;

public class AndroidTelephonyManager extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TextView textGsmCellLocation = (TextView)findViewById(R.id.gsmcelllocation);
TextView textCID = (TextView)findViewById(R.id.cid);
TextView textLAC = (TextView)findViewById(R.id.lac);

//retrieve a reference to an instance of TelephonyManager
TelephonyManager telephonyManager = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
GsmCellLocation cellLocation = (GsmCellLocation)telephonyManager.getCellLocation();

int cid = cellLocation.getCid();
int lac = cellLocation.getLac();
textGsmCellLocation.setText(cellLocation.toString());
textCID.setText("gsm cell id: " + String.valueOf(cid));
textLAC.setText("gsm location area code: " + String.valueOf(lac));
}
}


<?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"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<TextView
android:id="@+id/gsmcelllocation"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<TextView
android:id="@+id/cid"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<TextView
android:id="@+id/lac"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>


next:
- Convert cellLocation to real location (Latitude and Longitude)

6 comments:

  1. Hi There, I have used this code but, when I m trying to run it on handset, its its closing unexpectedly.

    M not getting the reason of this. Kindly give solution.

    ReplyDelete
  2. This comment has been removed by the author.

    ReplyDelete
  3. Register AndroidTelephonyManager in Manifest

    ReplyDelete
  4. I got this :
    10-30 04:00:17.523: E/AndroidRuntime(1553): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.caller/com.caller.CallerActivity}: java.lang.SecurityException: Neither user 10071 nor current process has android.permission.ACCESS_COARSE_LOCATION.

    What do I miss here ? I have put





    in the manifest.....

    Any ideas ? thanks

    ReplyDelete
    Replies
    1. e need the following permission in this example:
      android.permission.ACCESS_COARSE_LOCATION
      android.permission.ACCESS_FINE_LOCATION
      android.permission.READ_PHONE_STATE

      Delete
  5. hi i face same problem and already use provided permission in manifest but facing same problem please suggest me..

    ReplyDelete

Infolinks In Text Ads