Ser*_*nko 16
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());
}
}
Run Code Online (Sandbox Code Playgroud)
不要忘记设置ACCESS_COARSE_LOCATION或ACCESS_FINE_LOCATION,否则您将获得SecurityExceptions.
例如,将以下内容添加到应用清单中的<manifest>元素:
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
Run Code Online (Sandbox Code Playgroud)
小智 7
这并不容易,因为你必须知道你在处理什么。首先你必须知道这是什么。就像android文档说的..
我将一些 KOTLIN 脚本翻译成 Java,但不是全部。别担心,Java 很容易翻译成 Kotlin 并进行逆向转换。
TelephonyManager 提供对有关设备上电话服务的信息的访问。应用程序可以使用此类中的方法来确定电话服务和状态,以及访问某些类型的订户信息。应用程序还可以注册一个监听器来接收电话状态变化的通知。
这将带您到 TELEPHONY_SERVICE
与 getSystemService(String) 一起使用来检索 TelephonyManager 以处理管理设备的电话功能。*
科特林
val telephonyManager = getSystemService(Context.TELEPHONY_SERVICE) as TelephonyManager
Run Code Online (Sandbox Code Playgroud)
爪哇
final TelephonyManager telephony = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
Run Code Online (Sandbox Code Playgroud)
之后你会发现这个
科特林
val cellLocation = telephonyManager.allCellInfo
Run Code Online (Sandbox Code Playgroud)
爪哇
List<CellInfo> cellLocation = telephonyManager.getAllCellInfo();
Run Code Online (Sandbox Code Playgroud)
从设备上的所有无线电返回所有观察到的小区信息,包括主小区和相邻小区。调用此方法不会触发对 onCellInfoChanged() 的调用,也不会更改调用 onCellInfoChanged() 的速率。
该列表可以包括任意组合的一个或多个 CellInfoGsm、CellInfoCdma、CellInfoLte 和 CellInfoWcdma 对象。
该信息表明您可以获得您的设备和/或 SIM 卡所属的相应网络类型
科特林
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
}
}
}
Run Code Online (Sandbox Code Playgroud)
如您所见,您可以获得相应网络类型的不同属性,例如 GSM
爪哇
getCid(), getLac(), getMccString(), getBsic(), getMncString()
Run Code Online (Sandbox Code Playgroud)
或 LTE
爪哇
getEarfcn(), getMccString(), getMncString(), getCi(), getPci(), getTac()
Run Code Online (Sandbox Code Playgroud)
现在,没错!问题是当您在 LOG 中开始看到 MAX_VALUES Int 数据类型时,在这种情况下2147483647。在文档中说“16-bit Tracking Area Code, Integer.MAX_VALUE if unknown ”然后应用程序说 2147483647 我们跳出窗口。
但是,如果我们认为深入,我们只会这样做一次。我们得到了财产网络一次。我们应该在状态网络专有更改时更新,这被命名为Listener
科特林
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
Run Code Online (Sandbox Code Playgroud)
创建类并扩展 PhoneStateListener 类
科特林
private inner class SignalStrengthListener() : PhoneStateListener() {
override fun onSignalStrengthsChanged(signalStrength: android.telephony.SignalStrength) {
**//to do in listener... in the below link**
}
}
Run Code Online (Sandbox Code Playgroud)
接下来的代码有些简单和愚蠢,因为侦听器正在工作,Listen。并且网络专有数据类型变化如此之快,以至于我们无法看到。但唯一要做的就是下一个脚本
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**
}
Run Code Online (Sandbox Code Playgroud)
所以,只有当你得到一个已知值时它才会改变
我留下了一些链接以了解它
我不知道您是否可以访问该信息。我知道您可以访问的位置数据是这样的
来自 d.android.com 上的 Android 开发人员指南
“您可以使用 Android 提供的 Context 对象查找区域设置:
String locale = context.getResources().getConfiguration().locale.getDisplayName();
Run Code Online (Sandbox Code Playgroud)
”
编辑:哦,你需要它做什么?因为如果您只想为其创建不同的资源,则无需访问这些信息,因为 Android 会为您做到这一点。请在 d.android.com 开发指南的本地化章节中阅读相关内容。
| 归档时间: |
|
| 查看次数: |
35447 次 |
| 最近记录: |