2013-03-22 117 views
2

我想知道的方法或一種方法找到移動終端連接到小區的位置(GSM之一)獲得塔GSM位置經緯度LNG

這是我的課:

package com.example.signal; 

import java.io.File; 
import java.io.FileOutputStream; 
import java.io.IOException; 
import java.io.OutputStreamWriter; 
import java.sql.Date; 
import java.text.SimpleDateFormat; 
import java.util.Calendar; 
import java.util.LinkedList; 
import java.util.List; 

import android.location.Location; 
import android.location.LocationListener; 
import android.location.LocationManager; 
import android.net.ConnectivityManager; 
import android.os.Bundle; 
import android.app.Activity; 
import android.content.Context; 
import android.telephony.CellLocation; 
import android.telephony.NeighboringCellInfo; 
import android.telephony.PhoneStateListener; 
import android.telephony.SignalStrength; 
import android.telephony.TelephonyManager; 
import android.telephony.cdma.CdmaCellLocation; 
import android.telephony.gsm.GsmCellLocation; 
import android.text.format.Time; 
import android.util.Log; 
import android.view.Menu; 
import android.widget.LinearLayout; 
import android.widget.ScrollView; 
import android.widget.TextView; 
import android.widget.Toast; 

public class MainActivity extends Activity { 
    int counter = 0; 
    MainActivity mainn = this; 
    String SignalType = ""; 
    List<TextView> ListText = new LinkedList<TextView>(); 
    TelephonyManager  Tel; 
     MyPhoneStateListener MyListener; 
     int dBm; 
     int ASU; 
     Context context; 
     LinearLayout Linear; 
     ScrollView scroll; 
     File myFile; 
     FileOutputStream fOut; 
     OutputStreamWriter myOutWriter; 
     /** Called when the activity is first created. */ 
     @Override 
     public void onCreate(Bundle savedInstanceState) 
     { 
      super.onCreate(savedInstanceState); 
      // setContentView(R.layout.activity_main); 
      myFile = new File("/sdcard/mysdfile.txt"); 
      while (myFile.exists()) 
      { 
       counter++; 
       myFile = new File("/sdcard/mysdfile" + counter+".txt"); 
      } 
      try { 
       myFile.createNewFile(); 
       fOut = new FileOutputStream(myFile); 
        myOutWriter = new OutputStreamWriter(fOut); 
      } catch (IOException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 
      Linear = new LinearLayout(this); 
      Linear.setOrientation(LinearLayout.VERTICAL); 
      scroll = new ScrollView(this); 
      scroll.addView(Linear); 
      this.setContentView(scroll); 
      Tel  = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE); 
      if (Tel.getPhoneType() == TelephonyManager.PHONE_TYPE_CDMA) { 
       Toast.makeText(getApplicationContext(), "NO GSM", Toast.LENGTH_SHORT).show(); 
       return; 
      } else if (Tel.getPhoneType() == TelephonyManager.PHONE_TYPE_GSM) { 

      } 
      Tel.listen(MyListener ,PhoneStateListener.LISTEN_SIGNAL_STRENGTHS); 
      MyListener = new MyPhoneStateListener(); 

     } 

     /* Called when the application is minimized */ 
     @Override 
     protected void onPause() 
     { 
      super.onPause(); 
      Tel.listen(MyListener, PhoneStateListener.LISTEN_NONE); 
     } 

     /* Called when the application resumes */ 
     @Override 
     protected void onResume() 
     { 
      super.onResume(); 
      Tel.listen(MyListener,PhoneStateListener.LISTEN_SIGNAL_STRENGTHS); 
     } 

     /* —————————– */ 
     /* Start the PhoneState listener */ 
     /* —————————– */ 
     private class MyPhoneStateListener extends PhoneStateListener 
     { 
      /* Get the Signal strength from the provider, each tiome there is an update */ 
      @Override 
      public void onSignalStrengthsChanged(SignalStrength signalStrength) 
      { 

      super.onSignalStrengthsChanged(signalStrength); 
      //Toast.makeText(getApplicationContext(), "Go to Firstdroid!!! GSM Cinr = " 
       // + String.valueOf(signalStrength.getGsmSignalStrength()), Toast.LENGTH_SHORT).show(); 
      ASU = signalStrength.getGsmSignalStrength(); 
      if (ASU > 30) { 
       SignalType = "Good"; 
       // signalStrengthText.setTextColor(getResources().getColor(R.color.good)); 
      } else if (ASU > 20 && ASU < 30) { 
       SignalType = "Average"; 
       // signalStrengthText.setTextColor(getResources().getColor(R.color.average)); 
      } else if (ASU < 20) { 
       SignalType = "Weak"; 
       // signalStrengthText.setTextColor(getResources().getColor(R.color.weak)); 
      } 
      dBm= -113+(2*ASU); 
      TelephonyManager m = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE); 
      if (m.getSimState() != TelephonyManager.SIM_STATE_ABSENT){ 
       // SIM card 
      } else { 
       // No SIM card 
      } 
      TelephonyManager telephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE); 
      CellLocation location = telephonyManager.getCellLocation(); 
      GsmCellLocation gsmLocation = (GsmCellLocation) location; 
      String networkOperator = telephonyManager.getNetworkOperator(); 

      if (networkOperator != null) { 
       int mcc = Integer.parseInt(networkOperator.substring(0, 3)); 
       int mnc = Integer.parseInt(networkOperator.substring(3)); 
       Log.d("mcc " + mcc, "mnc " + mnc); 
      } 
      //CdmaCellLocation gsmLocation1 = (CdmaCellLocation) location; 
      int cellId = gsmLocation.getCid(); 
      int lac = gsmLocation.getLac(); 
      // Log.d("al latitude ", gsmLocation1.getBaseStationLatitude()+""); 
      //Time today = new Time(Time.getCurrentTimezone()); 
      //today.setToNow(); 
      String mydate = java.text.DateFormat.getDateTimeInstance().format(Calendar.getInstance().getTime()); 
      TextView tvv = new TextView(mainn); 
      tvv.setText("GSM dBm " + String.valueOf(dBm) + " , Cell ID " + String.valueOf(cellId) + " , Cell LAC " + String.valueOf(lac)+ " , ASU " + ASU + " , Time now " + mydate + " , Signal Type " + SignalType); 
      // Toast.makeText(getApplicationContext(), "Go to Firstdroid!!! GSM dBm = " 
      //  + String.valueOf(dBm) + " , Cell ID = " + String.valueOf(cellId) +" , Cell Lac = " + String.valueOf(lac), Toast.LENGTH_SHORT).show(); 
      /* LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE); 

      // Define a listener that responds to location updates 
      LocationListener locationListener = new LocationListener() { 
       public void onLocationChanged(Location location) { 
       // Called when a new location is found by the network location provider. 
       // makeUseOfNewLocation(location); 
       } 

       public void onStatusChanged(String provider, int status, Bundle extras) {} 

       public void onProviderEnabled(String provider) {} 

       public void onProviderDisabled(String provider) {} 
      }; 

      // Register the listener with the Location Manager to receive location updates 
      locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener); 
      String locationProvider = LocationManager.NETWORK_PROVIDER; 
     // Or use LocationManager.GPS_PROVIDER 

      Location lastKnownLocation = locationManager.getLastKnownLocation(locationProvider); 
     double lat = lastKnownLocation.getLatitude(); 
     double longg = lastKnownLocation.getLongitude(); 
     Log.d("Lat is " + lat, "Long is " + longg); 
     int psc = gsmLocation.getPsc(); 
     Log.d("TAG", "PSC = " + psc); */ 

     List<NeighboringCellInfo> neighCell = null; 
     neighCell = telephonyManager.getNeighboringCellInfo(); 
     for (int i = 0; i < neighCell.size(); i++) 
     { 
      NeighboringCellInfo thisCell = neighCell.get(i); 
      int CID = thisCell.getCid(); 
      int RSSI = thisCell.getRssi(); 
      int PSC = thisCell.getPsc(); 
      Log.d("TAG", " "+CID+" - "+RSSI + " - " + PSC); 
     } 
     try 
      { 

        myOutWriter.append(tvv.getText()); 
        myOutWriter.append("\n"); 
      } 
      catch (Exception e) 
      { 
       e.printStackTrace(); 
      } 
      Linear.addView(tvv); 
      Linear.invalidate(); 
      scroll.invalidate(); 
      } 

     };/* End of private Class */ 

    /* GetGsmSignalStrength */ 
    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 

     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.main, menu); 
     return true; 
    } 
    @Override 
    public void onBackPressed() 
    { 
     try 
     { 
      myOutWriter.close(); 
      fOut.close(); 
      this.finish(); 
     } 
     catch (Exception e) 
     { 
      e.printStackTrace(); 
     } 
    } 

} 

有什麼我可以說gsmLocation.getLatitude()gsmLocation.getLongitude()

回答

2

要知道當前GSM小區的位置,需要一個單元格ID及其位置的數據庫。如果您不是網絡運營商,那麼這些信息就沒有真正的權威來源,因爲它們不會發布。

如果你只是想對設備進行定位,嘗試this SO question.

+0

我已經使用http://ayauto.in和http://location-api.com,它們是包含小區位置數據庫,給他們的MCC,MNC, Cell ID和LAC,他們給我的單元格的經度,但具有相同的輸入給兩個網站的單元格位置是不同的,所以我認爲他們不夠準確,依靠 – 2013-03-22 11:23:45

+0

我認爲這是目前的情況。它可能在未來發生變化。 – 2013-03-22 12:19:45

+0

在擁擠的地區,精度一般在200mts以內。維基頁面列出了http://locationapi.org作爲覆蓋面最大的數據庫。 – kouton 2013-08-16 07:54:40

2

擴展什麼@S名單說,你應該登錄了以下API提供商&看到自己的令牌如何準確度/成本解決。