title: Android get location from cell tower
body: <p>Hi I want to get location from cell tower(Sim card) without internet. I had used below mentioned code it's give me only last location when network available.</p>
  
  <p>my requirement is to get location without WiFi or internet. can it's possible?</p>
  
  <pre><code>package com.example.gpscelltower;
  import java.text.DateFormat;
  import java.text.SimpleDateFormat;
  import java.util.Calendar;
  import java.util.Timer;
  import java.util.TimerTask;
  import android.app.Activity;
  import android.content.Context;
  import android.location.Location;
  import android.location.LocationListener;
  import android.location.LocationManager;
  import android.os.Bundle;
  import android.util.Log;
  import android.widget.Toast;
  
  public class CellTowerActivity1 extends Activity {
  private static final String TAG = "Cell";
  private static LocationManager locationManager;
  private static LocationListener locationListener;
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_cell_tower);
  
      locationManager=(LocationManager)getSystemService(Context.LOCATION_SERVICE);  
      String providerName=LocationManager.NETWORK_PROVIDER;  
      Location location=locationManager.getLastKnownLocation(providerName);  
      updateWithLocation(location);       
  
      //Cell-ID and WiFi location updates.
      locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 1000, 0, locationListener);
      locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 0, locationListener);
  
      locationListener = new LocationListener() {
        public void onLocationChanged(Location location) {
          Log.d("TAG", "locationListner");
          updateWithLocation(location);
          String Text = "My current location is: " + "Latitude = "+ location.getLatitude() + "Longitude = " + location.getLongitude();
          Toast.makeText(getApplicationContext(), Text, Toast.LENGTH_LONG).show();
          Log.d("TAG", "Starting..");
        }
        public void onStatusChanged(String provider, int status, Bundle extras) {
            Log.d("TAG", "onSatatus");
        }
        public void onProviderEnabled(String provider) {
            Log.d("TAG", "onProviderEnable");
        }
        public void onProviderDisabled(String provider) {
            Log.d("TAG", "onProviderDisble");
        }
      };
  }  
  private void updateWithLocation(Location location) {  
          Log.d("TAG", "UpdateWithLocation");
          String displayString;  
          if(location!=null){  
              Double lat=location.getLatitude();  
              Double lng=location.getLongitude();  
              Long calTime=location.getTime();  
              DateFormat formatter = new SimpleDateFormat("dd/MM/yyyy hh:mm:ss.SSS");  
              Calendar calendar = Calendar.getInstance();  
              calendar.setTimeInMillis(calTime);  
              String time=formatter.format(calendar.getTime()).toString();  
              displayString="The Lat: "+lat.toString()+" \nAnd Long: "+lng.toString()+"\nThe time of Calculation: "+time;  
          }else{  
              displayString="No details Found";  
          }  
          Toast.makeText(CellTowerActivity1.this, ""+displayString, Toast.LENGTH_LONG).show();  
      }  
  }  
  </code></pre>