如何在安卓手机中获取位置区域码和小区标识码

10
我想知道存储在SIM卡中的位置区域码和小区ID。
如何在Android手机上获取位置区域码和小区ID。
最好的祝福。
5个回答

17
final TelephonyManager telephony = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
if (telephony.getPhoneType() == TelephonyManager.PHONE_TYPE_GSM) {
    final GsmCellLocation location = (GsmCellLocation) telephony.getCellLocation();
    if (location != null) {
        msg.setText("LAC: " + location.getLac() + " CID: " + location.getCid());
    }
}

请勿忘记设置ACCESS_COARSE_LOCATION或ACCESS_FINE_LOCATION,否则可能会收到SecurityExceptions异常。

例如,在应用程序清单中的<manifest>元素中添加以下内容:

<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>

16

在拨号键盘中输入*#*#4636#*#*或许是你想要的。


8

这并不容易,因为您必须知道自己在处理什么。首先,您必须知道这是什么。就像Android文档所说...

我将一些Kotlin脚本翻译成了Java,但不是全部。不用担心,Java很容易翻译成Kotlin并进行反向操作。

TelephonyManager 提供有关设备上电话服务的信息访问。应用程序可以使用此类中的方法确定电话服务和状态,以及访问某些类型的用户信息。应用程序还可以注册侦听器以接收电话状态更改的通知。

这将带您前往 TELEPHONY_SERVICE

与 getSystemService(String)一起使用,以检索用于处理设备电话功能的TelephonyManager。

Kotlin

val telephonyManager = getSystemService(Context.TELEPHONY_SERVICE) as TelephonyManager

java

   final TelephonyManager telephony = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);

接着你会找到这个:

Kotlin

val cellLocation = telephonyManager.allCellInfo

java

   List<CellInfo> cellLocation = telephonyManager.getAllCellInfo();

getAllCellInfo

获取设备上所有无线电台(包括主要和邻近的基站)观察到的所有基站信息。调用此方法不会触发 onCellInfoChanged() 的调用,也不会更改 onCellInfoChanged() 调用的速率。

列表中可以包含一个或多个 CellInfoGsm、CellInfoCdma、CellInfoLte 和 CellInfoWcdma 对象,以任意组合的形式。

这些信息可以告诉您设备和/或 SIM 卡所属的网络类型。

Kotlin

   if (cellLocation != null) {  //verify if is'nt null
        for (info in cellLocation) {    // Loop for go through Muteablelist
            if (info is CellInfoGsm) {  //verify if Network is Gsm type

                // val gsm = (info as CellInfoGsm).cellSignalStrength  //get the cell Signal strength  
                val identityGsm = (info as CellInfoGsm).cellIdentity   //get the cellIdentity data 
                val defineTextLAC = getString(R.string.lac, "lola"+identityGsm.lac)  //get the LAC(LOCATION AREA CODE) string
                lacLabel.text = defineTextLAC   //set the xml text in element textview

            }else if(info is CellInfoCdma){     //verify if Network is Cdma type

                val identityCdma = info.cellIdentity    //get the cellIdentity data 
                val defineTextLAC = getString(R.string.lac, "lola"+identityCdma.basestationId)   //get the (basestationId) string
                lacLabel.text = defineTextLAC   //set the xml text in element textview      //get the LAC(LOCATION AREA CODE) string

            }else if(info is CellInfoLte){       //verify if Network is LTE type

                val identityLte = info.cellIdentity     //get the cellIdentity data
                val defineTextLAC = getString(R.string.lac, "pop"+identityLte.ci)       //get the CI(CellIdentity) string
                lacLabel.text = defineTextLAC  //set the xml text in element textview

            }else if  (lCurrentApiVersion >= Build.VERSION_CODES.JELLY_BEAN_MR2 && info is CellInfoWcdma) { //verify if Network is Wcdma and version SFK match with te network type

                val identityWcdma = info.cellIdentity       //get the cellIdentity data
                val defineTextLAC = getString(R.string.lac, "lola"+identityWcdma.cid) //  get the Cid(CellIdentity) string  
                lacLabel.text = defineTextLAC  //set the xml text in element textview

            }
        }
    }

如您所见,您可以获取不同网络类型的属性,例如 GSM

Java

   getCid(), getLac(), getMccString(), getBsic(), getMncString()

或适用于LTE

Java

 getEarfcn(), getMccString(), getMncString(), getCi(), getPci(), getTac() 

现在没错了!问题是当您在LOG中开始看到MAX_VALUES Int数据类型时,在这种情况下为2147483647。文档中说:“16位Tracking Area Code,如果未知,则为Integer.MAX_VALUE。”然后应用程序显示2147483647,我们就跳出窗口了。

但是,如果我们深入思考一下,我们只做了一次这样的操作。我们应该在网络状态属性更改时更新,这称为Listener

Kotlin

private var signalStrengthListener: SignalStrengthListener? = null

       signalStrengthListener = SignalStrengthListener()

   (getSystemService(Context.TELEPHONY_SERVICE) as TelephonyManager).listen(signalStrengthListener, 
  PhoneStateListener.LISTEN_SIGNAL_STRENGTHS)

    tm = getSystemService(Context.TELEPHONY_SERVICE) as TelephonyManager

创建类并扩展PhoneStateListener类

Kotlin

   private inner class SignalStrengthListener() : PhoneStateListener() {



    override fun onSignalStrengthsChanged(signalStrength: android.telephony.SignalStrength) {

   **//to do in listener... in the below link**

   }
 }

这是一个Java示例,用于使监听器内容和其他功能更加易懂

下面的代码非常简单且愚蠢,因为监听器正在工作。网络专有数据类型变化如此之快,以至于我们无法看到它。但唯一需要做的事情就是执行下一个脚本

  if(cellInfo.cellIdentity.tac != 2147483647){   //2147483647 is the MAX_VALUE INT DATA TYPE
            cellTac =  cellInfo.cellIdentity.tac  //tac is a propiety network (TRACKING AREA CODE**strong text**
   }

因此,只有在获取已知值时才会发生更改

我提供一些链接以了解更多相关信息

应用程序示例

Android Java 示例

Android Java 示例


1
数值采用16进制。 使用方法如下:

String cellId = Integer.toHexString(location.getCid());
String cellLac = Integer.toHexString(location.getLac());

0

我不知道你是否可以访问那些信息。 我知道你可以访问的位置数据是:

来自 d.android.com 的 Android 开发者指南

“您可以使用 Android 可用的 Context 对象查找语言环境:

String locale = context.getResources().getConfiguration().locale.getDisplayName();

"

编辑:哦,你需要它做什么?因为如果你只想为其创建不同的资源,你不需要访问信息,因为Android会为你完成。在d.android.com开发指南的本地化章节中阅读相关内容。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接