package com.umer.cellid;
import java.util.Locale;
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.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.database.Cursor;
import android.location.Criteria;
import android.location.Geocoder;
import android.location.Location;
import android.location.LocationManager;
import android.net.Uri;
import android.os.Bundle;
import android.telephony.CellLocation;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.telephony.gsm.GsmCellLocation;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class OpencellidActivity extends Activity {
String latitude;
String longitude;
public class OpenCellID {
String mcc; //Mobile Country Code
String mnc; //mobile network code
String cellid; //Cell ID
String lac; //Location Area Code
Boolean error;
String strURLSent;
String GetOpenCellID_fullresult;
public Boolean isError(){
return error;
}
public void setMcc(String value){
mcc = value;
}
public void setMnc(String value){
mnc = value;
}
public void setCallID(int value){
cellid = String.valueOf(value);
}
public void setCallLac(int value){
lac = String.valueOf(value);
}
public String getLocation(){
return("latitude: "+latitude + " longitude: " + longitude);
}
public void groupURLSent(){
strURLSent =
"http://www.opencellid.org/cell/get?mcc=" + mcc
+"&mnc=" + mnc
+"&cellid=" + cellid
+"&lac=" + lac
+"&fmt=txt";
}
public String getstrURLSent(){
return strURLSent;
}
public String getGetOpenCellID_fullresult(){
return GetOpenCellID_fullresult;
}
public void GetOpenCellID() throws Exception {
groupURLSent();
HttpClient client = new DefaultHttpClient();
HttpGet request = new HttpGet(strURLSent);
HttpResponse response = client.execute(request);
GetOpenCellID_fullresult = EntityUtils.toString(response.getEntity());
spliteResult();
}
private void spliteResult(){
if(GetOpenCellID_fullresult.equalsIgnoreCase("err")){
error = true;
}else{
error = false;
String[] tResult = GetOpenCellID_fullresult.split(",");
latitude = tResult[0];
longitude = tResult[1];
}
}
}
int myLatitude, myLongitude;
OpenCellID openCellID;
TextView textSignal,batteryLevel;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
SignalStrengthListener signalStrengthListener;
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);
TextView textLAC = (TextView)findViewById(R.id.lac);
TextView textGeo = (TextView)findViewById(R.id.geo);
TextView textRemark = (TextView)findViewById(R.id.remark);
TextView textImei = (TextView)findViewById(R.id.imei);
TextView textImsi = (TextView)findViewById(R.id.imsi);
TextView textGps = (TextView)findViewById(R.id.gps_langlat);
textSignal = (TextView)findViewById(R.id.signal);
batteryLevel=(TextView) findViewById(R.id.betry);
Button mapButton=(Button) findViewById(R.id.button1);
//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);
textImei.setText("Imei: " + getimeinum().toString());
textImsi.setText("imsi: " + getimsinum().toString());
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));
signalStrengthListener = new SignalStrengthListener();
((TelephonyManager)getSystemService(TELEPHONY_SERVICE)).listen(signalStrengthListener,SignalStrengthListener.LISTEN_SIGNAL_STRENGTHS);
LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
Criteria criteria = new Criteria();
criteria.setPowerRequirement(Criteria.POWER_LOW);
criteria.setAccuracy(Criteria.ACCURACY_COARSE);
String bestprovider = locationManager.getBestProvider(criteria, false);
Location lastknownlocation = locationManager.getLastKnownLocation(bestprovider);
if(lastknownlocation!=null)
{
textGps.setText("GPSLocation: Latitude: " + lastknownlocation.getLatitude()+", Longitude: "+ lastknownlocation.getLongitude());
}
textGeo.setText("APN: " + getapn().toString());
this.registerReceiver(this.myBatteryReceiver,
new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
openCellID = new OpenCellID();
openCellID.setMcc(mcc);
openCellID.setMnc(mnc);
openCellID.setCallID(cid);
openCellID.setCallLac(lac);
try {
openCellID.GetOpenCellID();
if(!openCellID.isError()){
// textGeo.setText(openCellID.getLocation());
textRemark.setText( "\n\n"
+ "URL sent: \n" + openCellID.getstrURLSent() + "\n\n"
+ "response: \n" + openCellID.getLocation()+ "\n\n");
}else{
textGeo.setText("Error");
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
textGeo.setText("Exception: " + e.toString());
}
mapButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent in=new Intent(OpencellidActivity.this, Map.class);
Bundle b=new Bundle();
b.putString("latitude", latitude);
b.putString("longitude", longitude);
in.putExtras(b);
startActivity(in);
}
});
}
private BroadcastReceiver myBatteryReceiver
= new BroadcastReceiver(){
@Override
public void onReceive(Context arg0, Intent arg1) {
// TODO Auto-generated method stub
if (arg1.getAction().equals(Intent.ACTION_BATTERY_CHANGED)){
batteryLevel.setText("Battery Level: "
+ String.valueOf(arg1.getIntExtra("level", 0)) + "%");
}
}
};
public String getimeinum() {
String deviceID = null;
String serviceName = Context.TELEPHONY_SERVICE;
TelephonyManager m_telephonyManager = (TelephonyManager) getSystemService(serviceName);
CellLocation re= m_telephonyManager.getCellLocation();
GsmCellLocation loc = (GsmCellLocation)m_telephonyManager.getCellLocation();
String Cell_ID = String.format("%08x ", loc.getCid());
int deviceType = m_telephonyManager.getPhoneType();
switch (deviceType) {
case (TelephonyManager.PHONE_TYPE_GSM):
break;
case (TelephonyManager.PHONE_TYPE_CDMA):
break;
case (TelephonyManager.PHONE_TYPE_NONE):
break;
default:
break;
}
// String imsi = m_telephonyManager.getSubscriberId();
deviceID = m_telephonyManager.getDeviceId();
return deviceID ;
}
public String getimsinum()
{
String serviceName = Context.TELEPHONY_SERVICE;
TelephonyManager m_telephonyManager = (TelephonyManager) getSystemService(serviceName);
String imsi = m_telephonyManager.getSubscriberId();
return imsi;
}
private class SignalStrengthListener extends PhoneStateListener
{
@Override
public void onSignalStrengthsChanged(android.telephony.SignalStrength signalStrength) {
// get the signal strength (a value between 0 and 31)
int strengthAmplitude = signalStrength.getGsmSignalStrength();
//do something with it (in this case we update a text view)
textSignal.setText(" signal strength: "+String.valueOf(strengthAmplitude));
// signal=String.valueOf(strengthAmplitude);
super.onSignalStrengthsChanged(signalStrength);
}
}
public String getLocation()
{
String name = null;
LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
Criteria criteria = new Criteria();
criteria.setPowerRequirement(Criteria.POWER_LOW);
criteria.setAccuracy(Criteria.ACCURACY_COARSE);
String bestprovider = locationManager.getBestProvider(criteria, false);
Location lastknownlocation = locationManager.getLastKnownLocation(bestprovider);
Geocoder geocoder = new Geocoder(OpencellidActivity.this, Locale.getDefault());
if(lastknownlocation!=null)
{
lastknownlocation.getLatitude();
lastknownlocation.getLongitude();
}
return name;
}
public String getapn()
{
final Uri APN_TABLE_URI = Uri.parse("content://telephony/carriers");
final Uri PREFERRED_APN_URI = Uri.parse("content://telephony/carriers/preferapn");
//receiving cursor to preffered APN table
Cursor c = getContentResolver().query(PREFERRED_APN_URI, null, null, null, null);
//moving the cursor to beggining of the table
c.moveToFirst();
//now the cursor points to the first preffered APN and we can get some
//information about it
//for example first preffered APN id
int index = c.getColumnIndex("_id"); //getting index of required column
Short id = c.getShort(index); //getting APN's id from
//we can get APN name by the same way
index = c.getColumnIndex("name");
String name = c.getString(index);
return name;
}
}
Manifest
<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"/>