summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorbt <bt@rctt.net>2026-05-03 22:51:03 +0200
committerbt <bt@rctt.net>2026-05-03 22:51:03 +0200
commit5c282707a9c8183f165adabb478d4315afaf3676 (patch)
treed578942da167b99f76d9ea5aae8d2e4cc7cb398a
parent2937bc975848e7979287e7067f4fff10bbfe4988 (diff)
downloadnetmon-5c282707a9c8183f165adabb478d4315afaf3676.tar.gz
netmon-5c282707a9c8183f165adabb478d4315afaf3676.zip
Use "requestCellInfoUpdate' to get cell info faster
-rw-r--r--.idea/gradle.xml1
-rw-r--r--.idea/misc.xml1
-rw-r--r--app/src/main/AndroidManifest.xml2
-rw-r--r--app/src/main/java/net/rctt/netmon/MainActivity.kt110
4 files changed, 67 insertions, 47 deletions
diff --git a/.idea/gradle.xml b/.idea/gradle.xml
index cdbc250..02c4aa5 100644
--- a/.idea/gradle.xml
+++ b/.idea/gradle.xml
@@ -1,5 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
+ <component name="GradleMigrationSettings" migrationVersion="1" />
<component name="GradleSettings">
<option name="linkedExternalProjectsSettings">
<GradleProjectSettings>
diff --git a/.idea/misc.xml b/.idea/misc.xml
index 74dd639..b2c751a 100644
--- a/.idea/misc.xml
+++ b/.idea/misc.xml
@@ -1,4 +1,3 @@
-<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ExternalStorageConfigurationManager" enabled="true" />
<component name="ProjectRootManager" version="2" languageLevel="JDK_21" default="true" project-jdk-name="jbr-21" project-jdk-type="JavaSDK">
diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml
index d036883..f8bbdd0 100644
--- a/app/src/main/AndroidManifest.xml
+++ b/app/src/main/AndroidManifest.xml
@@ -3,6 +3,8 @@
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
+ <uses-permission android:name="android.permission.READ_PHONE_STATE" />
+ <uses-permission android:name="android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS" />
<application
android:allowBackup="true"
diff --git a/app/src/main/java/net/rctt/netmon/MainActivity.kt b/app/src/main/java/net/rctt/netmon/MainActivity.kt
index 7111922..b3a6d37 100644
--- a/app/src/main/java/net/rctt/netmon/MainActivity.kt
+++ b/app/src/main/java/net/rctt/netmon/MainActivity.kt
@@ -5,12 +5,15 @@ import android.annotation.SuppressLint
import android.os.Bundle
import android.os.Handler
import android.os.Looper
+import android.telephony.CellInfo
import android.telephony.CellInfoGsm
import android.telephony.CellInfoLte
import android.telephony.CellInfoNr
import android.telephony.CellInfoTdscdma
import android.telephony.CellInfoWcdma
+import android.telephony.PhoneStateListener
import android.telephony.TelephonyManager
+import android.telephony.TelephonyManager.CellInfoCallback
import android.util.Log
import android.widget.LinearLayout
import android.widget.TextView
@@ -22,6 +25,9 @@ import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import java.text.SimpleDateFormat
import java.util.Date
+import java.util.Timer
+import java.util.TimerTask
+
class MainActivity : AppCompatActivity() {
lateinit var statusView: LinearLayout
@@ -42,64 +48,76 @@ class MainActivity : AppCompatActivity() {
statusView = findViewById(R.id.statusView)
cellView = findViewById(R.id.cellView)
- val pLoc = ContextCompat.checkSelfPermission(applicationContext, Manifest.permission.ACCESS_FINE_LOCATION)
+ val pLoc = ContextCompat.checkSelfPermission(
+ applicationContext,
+ Manifest.permission.ACCESS_FINE_LOCATION
+ )
if (pLoc == -1) {
log("Location permission required")
- } else {
- log("Ready")
- tel = getSystemService(TELEPHONY_SERVICE) as TelephonyManager
+ return
+ }
+ log("Ready")
- val mainHandler = Handler(Looper.getMainLooper())
+ tel = getSystemService(TELEPHONY_SERVICE) as TelephonyManager
- mainHandler.post(object : Runnable {
- @RequiresPermission(Manifest.permission.ACCESS_FINE_LOCATION)
- override fun run() {
- refresh()
- mainHandler.postDelayed(this, 1000)
- }
- })
- }
+ val mainHandler = Handler(Looper.getMainLooper())
+
+ mainHandler.post(object : Runnable {
+ @RequiresPermission(Manifest.permission.ACCESS_FINE_LOCATION)
+ override fun run() {
+ refresh()
+ mainHandler.postDelayed(this, 100)
+ }
+ })
}
@RequiresPermission(Manifest.permission.ACCESS_FINE_LOCATION)
fun refresh() {
log("refreshing")
- val cellList = tel.allCellInfo
- cellView.removeAllViews()
- for (cell in cellList) {
- when (cell) {
- is CellInfoGsm -> {
- logDetected("GSM")
- }
- is CellInfoLte -> {
- val id = cell.cellIdentity
- if (id.ci == 2147483647) {
- continue
- }
+ tel.requestCellInfoUpdate(mainExecutor, object : CellInfoCallback() {
+ @RequiresPermission(Manifest.permission.ACCESS_FINE_LOCATION)
+ override fun onCellInfo(cellList: List<CellInfo?>) {
+ cellView.removeAllViews()
+ for (cell in cellList) {
+ when (cell) {
+ is CellInfoGsm -> {
+ logDetected("GSM")
+ }
- logDetected("LTE")
- logCell("CID: ${id.ci}")
- logCell("PCI: ${id.pci}")
- logCell("TAC: ${id.tac}")
- logCell("EARFCN ${id.earfcn}")
- logCell("BANDWIDTH: ${id.bandwidth}")
- logCell("RSRP: ${cell.cellSignalStrength.rsrp}")
- logCell("DBM: ${cell.cellSignalStrength.dbm}")
- logCell("STATUS: ${cell.cellConnectionStatus}")
- logCell("")
- }
- is CellInfoNr -> {
- logDetected("NR")
- }
- is CellInfoTdscdma -> {
- logDetected("TDSCDMA")
- }
- is CellInfoWcdma -> {
- logDetected("WCDMA")
+ is CellInfoLte -> {
+ val id = cell.cellIdentity
+ if (id.ci == 2147483647) {
+ continue
+ }
+
+ logDetected("LTE")
+ logCell("CID: ${id.ci}")
+ logCell("PCI: ${id.pci}")
+ logCell("TAC: ${id.tac}")
+ logCell("EARFCN ${id.earfcn}")
+ logCell("BANDWIDTH: ${id.bandwidth}")
+ logCell("RSRP: ${cell.cellSignalStrength.rsrp}")
+ logCell("DBM: ${cell.cellSignalStrength.dbm}")
+ logCell("STATUS: ${cell.cellConnectionStatus}")
+ logCell("")
+ }
+
+ is CellInfoNr -> {
+ logDetected("NR")
+ }
+
+ is CellInfoTdscdma -> {
+ logDetected("TDSCDMA")
+ }
+
+ is CellInfoWcdma -> {
+ logDetected("WCDMA")
+ }
+ }
}
}
- }
+ })
}
@SuppressLint("SimpleDateFormat", "SetTextI18n")
@@ -121,4 +139,4 @@ class MainActivity : AppCompatActivity() {
cellView.addView(msg)
Log.d("NETMON", text)
}
-} \ No newline at end of file
+}