Breaking News


Saturday 15 February 2014

Android Neighboring Cell Information from GSM Cell ID

This example shows how you can get neighboring cell info from GSM Cell ID. TelephonyManager.getNeighboringCellInfo() return a list of neighboring cell information(NeighboringCellInfo), including Received Signal Strength and Cell ID location.
Algorithm:
1.) Create a new project by File-> New -> Android Project name it NeighboringCellInformation.
2.) Write following into main.xml:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
<?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="NeighboringCell Information"
   />
<TextView
   android:id="@+id/gsmcelllocation"
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   />
<TextView
   android:id="@+id/mcc"
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   />
<TextView
   android:id="@+id/mnc"
   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/neighboring"
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   />
</LinearLayout>
3.) Write following permissions into your manifest file:
1
2
3
4
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
<uses-permission android:name="android.permission.INTERNET"/>
4.) Run for output.
Steps:
1.) Create a project named NeighboringCellInformation and set the information as stated in the image.
Build Target: Android 4.4
Application Name: NeighboringCellInformation
Package Name: com.NeighboringCellInformation
Activity Name: NeighboringCellInformation
neighbor1
2.) Open NeighboringCellInformation.java file and write following code there:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package com.neighboringcellinformation;
 
import java.util.List;
  
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
  
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.telephony.NeighboringCellInfo;
import android.telephony.TelephonyManager;
import android.telephony.gsm.GsmCellLocation;
import android.view.View;
import android.widget.Button;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.Toast;
  
public class NeighboringCellInformation 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 textMCC = (TextView)findViewById(R.id.mcc);
       TextView textMNC = (TextView)findViewById(R.id.mnc);
       TextView textCID = (TextView)findViewById(R.id.cid);
  
        
       //retrieve a reference to an instance of TelephonyManager
       TelephonyManager telephonyManager = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
       GsmCellLocation cellLocation = (GsmCellLocation)telephonyManager.getCellLocation();
        
       String networkOperator = telephonyManager.getNetworkOperator();
       String mcc = networkOperator.substring(0, 3);
       String mnc = networkOperator.substring(3);
       textMCC.setText("mcc: " + mcc);
       textMNC.setText("mnc: " + mnc);
        
       int cid = cellLocation.getCid();
       int lac = cellLocation.getLac();
       textGsmCellLocation.setText(cellLocation.toString());
       textCID.setText("gsm cell id: " + String.valueOf(cid));
        
       TextView Neighboring = (TextView)findViewById(R.id.neighboring);
       List<NeighboringCellInfo> NeighboringList = telephonyManager.getNeighboringCellInfo();
        
       String stringNeighboring = "Neighboring List- Lac : Cid : RSSI\n";
       for(int i=0; i < NeighboringList.size(); i++){
          
        String dBm;
        int rssi = NeighboringList.get(i).getRssi();
        if(rssi == NeighboringCellInfo.UNKNOWN_RSSI){
         dBm = "Unknown RSSI";
        }else{
         dBm = String.valueOf(-113 + 2 * rssi) + " dBm";
        }
  
        stringNeighboring = stringNeighboring
         + String.valueOf(NeighboringList.get(i).getLac()) +" : "
         + String.valueOf(NeighboringList.get(i).getCid()) +" : "
         + dBm +"\n";
       }
        
       Neighboring.setText(stringNeighboring);
   }  
}
3.) Compile and build the project.
Output
Run on Actual device instead of simulator to get correct output.
Share This
Blogger
Facebook
Disqus

comments powered by Disqus

No comments :

Post a Comment

Subscribe
Labels
Popular Posts

Subscribe Via Email

About Us

THIS IS ANDROID AND JAVA FREE TUTORIAL AND ALL PROVIDE SOLUTION IN ANDROID AND JAVA INTERVIEW QUE AND ANS IS AVAILABLE IN MY BLOG AND ANY QUERY PLEASE CONTACT ME GO NOW CONTACT US PAGE.

Total Pageviews

© Android and Java solution All rights reserved | Designed By Fireandroids