From a011c61bd29baa693c0021c2ac7999ccfe017abd Mon Sep 17 00:00:00 2001 From: debz-g Date: Sat, 10 Dec 2022 21:04:37 +0530 Subject: [PATCH] Handled No Internet Condition with dialog messages and Connectivity Manager. --- app/src/main/AndroidManifest.xml | 1 + .../java/dev/refox/anitrack/MainActivity.kt | 13 +++-- .../java/dev/refox/anitrack/SplashActivity.kt | 52 +++++++++++++++++-- 3 files changed, 59 insertions(+), 7 deletions(-) diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 892ba8b..5b55b22 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -4,6 +4,7 @@ package="dev.refox.anitrack"> + (R.id.navHostFragmentAnime) -// binding.bottomNav.setupWithNavController(navHost.findNavController()) - } + + private fun replaceFragment(fragment: Fragment){ val fragmentManager = supportFragmentManager diff --git a/app/src/main/java/dev/refox/anitrack/SplashActivity.kt b/app/src/main/java/dev/refox/anitrack/SplashActivity.kt index d88658c..43c0d5b 100644 --- a/app/src/main/java/dev/refox/anitrack/SplashActivity.kt +++ b/app/src/main/java/dev/refox/anitrack/SplashActivity.kt @@ -1,9 +1,15 @@ package dev.refox.anitrack +import android.app.AlertDialog +import android.content.Context import android.content.Intent +import android.net.ConnectivityManager +import android.net.NetworkCapabilities +import android.os.Build import androidx.appcompat.app.AppCompatActivity import android.os.Bundle import android.os.Handler +import android.widget.Toast import dev.refox.anitrack.databinding.ActivitySplashBinding class SplashActivity : AppCompatActivity() { @@ -14,10 +20,48 @@ class SplashActivity : AppCompatActivity() { window.setStatusBarColor(this.getResources().getColor(R.color.trans)) binding = ActivitySplashBinding.inflate(layoutInflater) val view = binding.root - Handler().postDelayed({ - startActivity(Intent(this, IntroActivity::class.java)) - finish() - }, 2000) + + if (!isNetworkAvailable() == true) { + AlertDialog.Builder(this) + .setIcon(android.R.drawable.ic_dialog_alert) + .setTitle("Internet Connection Alert") + .setMessage("Please Check Your Internet Connection!! ") + .setPositiveButton( + "Close" + ) { dialogInterface, i -> finish() }.show() + } else if (isNetworkAvailable() == true) { + + Handler().postDelayed({ + startActivity(Intent(this, IntroActivity::class.java)) + finish() + }, 2000) + + Toast.makeText(this, "Welcome", Toast.LENGTH_SHORT).show() + } + + setContentView(view) } + + private fun isNetworkAvailable(): Boolean + { + val connectivityManager = + getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager + if (connectivityManager != null) { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { + val capabilities = + connectivityManager.getNetworkCapabilities(connectivityManager.activeNetwork) + if (capabilities != null) { + if (capabilities.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR)) { + return true + } else if (capabilities.hasTransport(NetworkCapabilities.TRANSPORT_WIFI)) { + return true + } else if (capabilities.hasTransport(NetworkCapabilities.TRANSPORT_ETHERNET)) { + return true + } + } + } + } + return false + } } \ No newline at end of file