From e652187a2f24b89806e60e3e581ed98baff41f32 Mon Sep 17 00:00:00 2001 From: Vishwajith-Shettigar Date: Fri, 2 Aug 2024 10:59:26 +0530 Subject: [PATCH] Examples for ui and non ui --- app/src/main/AndroidManifest.xml | 2 +- .../network_monitoring/MainActivity.kt | 21 --------- .../NetworkMonitorWithUi.kt | 46 +++++++++++++++++++ .../NetworkMonitorWithoutUi.kt | 38 +++++++++++++++ app/src/main/res/layout/activity_main.xml | 2 +- .../example/networkmonitor/NetworkMonitor.kt | 1 - .../networkmonitor/NetworkStatusView.kt | 6 +-- 7 files changed, 89 insertions(+), 27 deletions(-) delete mode 100644 app/src/main/java/com/example/network_monitoring/MainActivity.kt create mode 100644 app/src/main/java/com/example/network_monitoring/NetworkMonitorWithUi.kt create mode 100644 app/src/main/java/com/example/network_monitoring/NetworkMonitorWithoutUi.kt diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 4d5bf01..de06b32 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -13,7 +13,7 @@ android:theme="@style/Theme.Network_Monitoring" tools:targetApi="31"> diff --git a/app/src/main/java/com/example/network_monitoring/MainActivity.kt b/app/src/main/java/com/example/network_monitoring/MainActivity.kt deleted file mode 100644 index 098bd4a..0000000 --- a/app/src/main/java/com/example/network_monitoring/MainActivity.kt +++ /dev/null @@ -1,21 +0,0 @@ -package com.example.network_monitoring - -import androidx.appcompat.app.AppCompatActivity -import android.os.Bundle -import com.example.networkmonitor.NetworkStatusView - -class MainActivity : AppCompatActivity() { - private lateinit var networkStatusView: NetworkStatusView - - override fun onCreate(savedInstanceState: Bundle?) { - super.onCreate(savedInstanceState) - setContentView(R.layout.activity_main) - networkStatusView = findViewById(R.id.network_status_view) - networkStatusView.startMonitoring(this) - } - - override fun onDestroy() { - super.onDestroy() - networkStatusView.unregisterNetworkMonitor() - } -} diff --git a/app/src/main/java/com/example/network_monitoring/NetworkMonitorWithUi.kt b/app/src/main/java/com/example/network_monitoring/NetworkMonitorWithUi.kt new file mode 100644 index 0000000..3242d45 --- /dev/null +++ b/app/src/main/java/com/example/network_monitoring/NetworkMonitorWithUi.kt @@ -0,0 +1,46 @@ +package com.example.network_monitoring + +import androidx.appcompat.app.AppCompatActivity +import android.os.Bundle +import android.util.Log +import com.example.networkmonitor.NetworkMonitor +import com.example.networkmonitor.NetworkStatusView + +class NetworkMonitorWithUi : AppCompatActivity() { + private lateinit var networkStatusView: NetworkStatusView + private lateinit var networkMonitor:NetworkMonitor + + override fun onCreate(savedInstanceState: Bundle?) { + super.onCreate(savedInstanceState) + setContentView(R.layout.activity_main) + networkStatusView = findViewById(R.id.network_status_view) + + networkStatusView.startMonitoring(this) + + + // Network monitoring api. + networkMonitor = NetworkMonitor(this) + networkMonitor.networkStatus.observe(this) { + if (it) { + // Connection on + Log.e("#", it.toString() + " " + networkMonitor.isNetworkAvailable()) + } else { + // Connection off + Log.e("#", it.toString() + " " + networkMonitor.isNetworkAvailable()) + } + } + + // If you dont want to observe. + val isNetworkAvailable=networkMonitor.isNetworkAvailable() + + } + + override fun onDestroy() { + super.onDestroy() + // Stop monitoring. + networkStatusView.stopMonitoring() + + // Stop obeserving. + networkMonitor.unregisterCallback() + } +} diff --git a/app/src/main/java/com/example/network_monitoring/NetworkMonitorWithoutUi.kt b/app/src/main/java/com/example/network_monitoring/NetworkMonitorWithoutUi.kt new file mode 100644 index 0000000..f7b9bd3 --- /dev/null +++ b/app/src/main/java/com/example/network_monitoring/NetworkMonitorWithoutUi.kt @@ -0,0 +1,38 @@ +package com.example.network_monitoring + +import android.os.Bundle +import android.util.Log +import androidx.appcompat.app.AppCompatActivity +import com.example.networkmonitor.NetworkMonitor + +class NetworkMonitorWithoutUi : AppCompatActivity() { + private lateinit var networkMonitor: NetworkMonitor + + override fun onCreate(savedInstanceState: Bundle?) { + super.onCreate(savedInstanceState) + setContentView(R.layout.activity_main) + + // Network monitoring api. + networkMonitor = NetworkMonitor(this) + + networkMonitor.networkStatus.observe(this) { + if (it) { + // Connection on + Log.e("#", it.toString() + " " + networkMonitor.isNetworkAvailable()) + } else { + // Connection off + Log.e("#", it.toString() + " " + networkMonitor.isNetworkAvailable()) + } + } + + // If you dont want to observe. + val isNetworkAvailable=networkMonitor.isNetworkAvailable() + + } + + override fun onDestroy() { + super.onDestroy() + // Stop obeserving. + networkMonitor.unregisterCallback() + } +} diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index 3f55786..8f94683 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -5,7 +5,7 @@ android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" - tools:context=".MainActivity"> + tools:context=".NetworkMonitorWithUi">