I would like to know Location Area Code and Cell ID saved in sim card.
How to get Location Area Code and Cell ID in android phone.
best开发者_JS百科 regards.
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());
}
}
Don't forget to set ACCESS_COARSE_LOCATION or ACCESS_FINE_LOCATION or you'll get SecurityExceptions.
For example add the following to your <manifest> element in the app manifest:
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
Type in *#*#4636#*#*
into the dialer and that might be what you're looking for.
This Is not easier, because you must know what you are dealing with. First u must be know what is this. Like android documentation say..
I TRANSLATED SOME KOTLIN SCRIPTS TO JAVA BUT NOT ALL. DON'T WORRY, JAVA IS EASY TO TRANSLATE TO KOTLIN AND MAKE REVERSE.
TelephonyManager Provides access to information about the telephony services on the device. Applications can use the methods in this class to determine telephony services and states, as well as to access some types of subscriber information. Applications can also register a listener to receive notification of telephony state changes.
This will take you to TELEPHONY_SERVICE
Use with getSystemService(String) to retrieve a TelephonyManager for handling management the telephony features of the device.*
Kotlin
val telephonyManager = getSystemService(Context.TELEPHONY_SERVICE) as TelephonyManager
java
final TelephonyManager telephony = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
After that you find this
Kotlin
val cellLocation = telephonyManager.allCellInfo
java
List<CellInfo> cellLocation = telephonyManager.getAllCellInfo();
getAllCellInfo
Returns all observed cell information from all radios on the device including the primary and neighboring cells. Calling this method does not trigger a call to onCellInfoChanged(), or change the rate at which onCellInfoChanged() is called.
The list can include one or more CellInfoGsm, CellInfoCdma, CellInfoLte, and CellInfoWcdma objects, in any combination.
The information says that you can obtain the respective type of network in which your device and / or sim card belong
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
}
}
}
As you can see you can obtain diferent propieties for respective network type for example GSM
java
getCid(), getLac(), getMccString(), getBsic(), getMncString()
or for LTE
java
getEarfcn(), getMccString(), getMncString(), getCi(), getPci(), getTac()
Now, that's right! The problem is when in the LOG you begin to see MAX_VALUES Int data type in this case 2147483647. In the documentation say "16-bit Tracking Area Code, Integer.MAX_VALUE if unknown" Then the app say 2147483647 and we jump out of the window.
But, if we think deeping we're doing this just one time. we're get the property network one time. We should update when the status network proprietary change, and this is named 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
to make class and extend PhoneStateListener class
Kotlin
private inner class SignalStrengthListener() : PhoneStateListener() {
override fun onSignalStrengthsChanged(signalStrength: android.telephony.SignalStrength) {
**//to do in listener... in the below link**
}
}
This is the java example for make the listener content and other function to help our
The next code is some simple and silly because the listener is working, Listen. And the network proprietary data type change so fast that we can't see that. But the only thing to do is the next script
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**
}
so, it only changes when you get a known value
I leave some links to know most about it
Example of app in prod
Java examples android
Java example android
Values are base 16. Use it like:
String cellId = Integer.toHexString(location.getCid());
String cellLac = Integer.toHexString(location.getLac());
I don't know if you can acces that information. The location data of which I know you can acces it is this
From the Android Developers guide on d.android.com
"You can look up the locale using the Context object that Android makes available:
String locale = context.getResources().getConfiguration().locale.getDisplayName();
"
EDIT: Oh what for do you need it? Because if you only want to make different resources for it, you don't need to acces the information, as Android does that for you. Read about it on the d.android.com dev guide in the chapter localization.
精彩评论