Android从手机信号塔获取位置

时间:2013-09-19 07:01:32

标签: android android-location android-loadermanager

嗨我想从没有互联网的手机信号塔(Sim卡)获取位置。我曾使用下面提到的代码,它只在网络可用时给我最后一个位置。

我的要求是在没有WiFi或互联网的情况下获取位置。可以吗?

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();  
    }  
}  

1 个答案:

答案 0 :(得分:1)

单元格地理定位通过将Cell Tower ID发送到服务器来实现,该服务器将其映射到坐标。所以,遗憾的是,无法通过标准API获得与数据连接的坐标。

相关问题