不能使用处理程序延迟启动片段
cant use handler to start fragment with delay
我想在我的应用程序中设置 SplashScreen(fragment),因为它是一个在线购物应用程序,我应该检查互联网连接。
我在这个应用程序中有 4 个主要片段和一个主要 activity。
我在启动片段中使用了进度条、文本视图和刷新按钮。
让我们看看部分代码!...
val networkConnection = NetworkConnection(requireContext())
networkConnection.observe(requireActivity()) { isConnected ->
if (isConnected) {
binding.refresh.setOnClickListener {
binding.apply {
progress.visibility = View.VISIBLE
progress.animate().start()
connectivityText.visibility = View.INVISIBLE
refresh.visibility = View.INVISIBLE
go()
}
}
} else {
binding.apply {
progress.visibility = View.GONE
connectivityText.visibility = View.VISIBLE
refresh.visibility = View.VISIBLE
}
}
}
这部分适用于当我未连接到互联网并启动应用程序时。
textview 出现此文本“您未连接到 Internet”,并且出现刷新按钮以在连接后刷新片段。它工作正常。
问题是当我连接到互联网并启动应用程序时。让我们看看代码。
val s = S(requireContext())
when (s.isOnline()) {
true -> {
binding.apply {
progress.visibility = android.view.View.VISIBLE
progress.animate().start()
connectivityText.visibility = android.view.View.INVISIBLE
refresh.visibility = android.view.View.INVISIBLE
}
}
false -> {
binding.apply {
progress.visibility = View.GONE
connectivityText.visibility = View.VISIBLE
refresh.visibility = View.VISIBLE
}
}
}
“S”是一个具有 isOnline() 函数的文件。我想在“if(isOnline)”语句中使用以下代码
Handler(Looper.getMainLooper()).postDelayed({
val action =
SplashFragmentDirections.actionSplashFragmentToHomeFragment2()
findNavController().navigate(action)
CoroutineScope(Dispatchers.Main).launch {
delay(4)
}
}, 4000)
当我在“if(isonline)”语句中设置此代码时,我看到此错误:
无法从 /::(端口 0)连接到 /192.168.1.36(端口 80),当我从那里删除它时
应用程序运行正常,没有错误。
网络连接类:
class NetworkConnection( private val context: Context) : LiveData<Boolean>() {
private var connectivityManager: ConnectivityManager =
context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
private lateinit var networkCallback: ConnectivityManager.NetworkCallback
override fun onActive() {
super.onActive()
updateConnection()
when {
Build.VERSION.SDK_INT >= Build.VERSION_CODES.N -> {
connectivityManager.registerDefaultNetworkCallback(ConnectivityManagerCallback())
}
Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP -> {
lollipopNetworkRequest()
}
else -> {
context.registerReceiver(
networkReciever,
IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION)
)
}
}
}
@RequiresApi(Build.VERSION_CODES.N)
override fun onInactive() {
super.onInactive()
}
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
private fun lollipopNetworkRequest() {
val requestBuilder = NetworkRequest.Builder()
.addTransportType(NetworkCapabilities.TRANSPORT_ETHERNET)
.addTransportType(NetworkCapabilities.TRANSPORT_CELLULAR)
.addTransportType(NetworkCapabilities.TRANSPORT_WIFI)
connectivityManager.registerNetworkCallback(
requestBuilder.build(),
ConnectivityManagerCallback()
)
}
private fun ConnectivityManagerCallback(): ConnectivityManager.NetworkCallback {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
networkCallback = object : ConnectivityManager.NetworkCallback() {
override fun onLost(network: Network) {
super.onLost(network)
postValue(false)
}
override fun onAvailable(network: Network) {
super.onAvailable(network)
postValue(true)
}
}
} else {
throw IllegalAccessError("ERROR")
}
return networkCallback
}
private var networkReciever = object : BroadcastReceiver() {
override fun onReceive(context: Context?, intent: Intent?) {
updateConnection()
}
}
private fun updateConnection() {
val activeNetwork: NetworkInfo? = connectivityManager.activeNetworkInfo
if (activeNetwork?.isConnected == true) {
postValue(true)
} else {
postValue(false)
}
}
}
我花了一个星期,但我仍然不知道。请帮助我
任何帮助将不胜感激。
我将我的初始片段更改为 activity 并且我也将其从图表中删除,然后我使用 Thread 而不是像这样的处理程序来处理连接到互联网的部分
var ms: Long = 0
val splashTime = 2000
val splashActive = true
val paused = false
//use go() method here instead of this thread.check below to see go() method
Thread {
try {
while (splashActive && ms < splashTime) {
if (!paused)
ms += 100
Thread.sleep(100)
}
} catch (e: Exception) {
e.fillInStackTrace()
} finally {
if (!s.isOnline()) {
} else {
startActivity(Intent(this, MainActivity::class.java))
}
}
}.start()
我改变了我没有连接到互联网的部分:
val networkConnection = NetworkConnection(this)
networkConnection.observe(this) { isConnected ->
if (isConnected) {
binding.refresh.setOnClickListener {
binding.apply {
progress.visibility = View.VISIBLE
progress.animate().start()
connectivityText.visibility = View.INVISIBLE
refresh.visibility = View.INVISIBLE
}
//use go() method here instead of this thread.check below to see go() method
Thread {
Thread.sleep(2000)
startActivity(Intent(this, MainActivity::class.java))
}.start()
}
} else {
binding.apply {
progress.visibility = View.GONE
connectivityText.visibility = View.VISIBLE
refresh.visibility = View.VISIBLE
}
}
}
更新
将我连接到互联网的第一部分代码放到一个方法中,然后在任何你想要的地方使用该方法,如下所示:
fun go() {
val s = S(this)
Thread {
try {
while (splashActive && ms < splashTime) {
if (!paused)
ms += 100
Thread.sleep(100)
}
} catch (e: Exception) {
e.fillInStackTrace()
} finally {
if (!s.isOnline()) {
} else {
startActivity(Intent(this, MainActivity::class.java))
}
}
}.start()
}
如果你有更好的解决方案请告诉我
我想在我的应用程序中设置 SplashScreen(fragment),因为它是一个在线购物应用程序,我应该检查互联网连接。
我在这个应用程序中有 4 个主要片段和一个主要 activity。
我在启动片段中使用了进度条、文本视图和刷新按钮。
让我们看看部分代码!...
val networkConnection = NetworkConnection(requireContext())
networkConnection.observe(requireActivity()) { isConnected ->
if (isConnected) {
binding.refresh.setOnClickListener {
binding.apply {
progress.visibility = View.VISIBLE
progress.animate().start()
connectivityText.visibility = View.INVISIBLE
refresh.visibility = View.INVISIBLE
go()
}
}
} else {
binding.apply {
progress.visibility = View.GONE
connectivityText.visibility = View.VISIBLE
refresh.visibility = View.VISIBLE
}
}
}
这部分适用于当我未连接到互联网并启动应用程序时。
textview 出现此文本“您未连接到 Internet”,并且出现刷新按钮以在连接后刷新片段。它工作正常。
问题是当我连接到互联网并启动应用程序时。让我们看看代码。
val s = S(requireContext())
when (s.isOnline()) {
true -> {
binding.apply {
progress.visibility = android.view.View.VISIBLE
progress.animate().start()
connectivityText.visibility = android.view.View.INVISIBLE
refresh.visibility = android.view.View.INVISIBLE
}
}
false -> {
binding.apply {
progress.visibility = View.GONE
connectivityText.visibility = View.VISIBLE
refresh.visibility = View.VISIBLE
}
}
}
“S”是一个具有 isOnline() 函数的文件。我想在“if(isOnline)”语句中使用以下代码
Handler(Looper.getMainLooper()).postDelayed({
val action =
SplashFragmentDirections.actionSplashFragmentToHomeFragment2()
findNavController().navigate(action)
CoroutineScope(Dispatchers.Main).launch {
delay(4)
}
}, 4000)
当我在“if(isonline)”语句中设置此代码时,我看到此错误:
无法从 /::(端口 0)连接到 /192.168.1.36(端口 80),当我从那里删除它时
应用程序运行正常,没有错误。
网络连接类:
class NetworkConnection( private val context: Context) : LiveData<Boolean>() {
private var connectivityManager: ConnectivityManager =
context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
private lateinit var networkCallback: ConnectivityManager.NetworkCallback
override fun onActive() {
super.onActive()
updateConnection()
when {
Build.VERSION.SDK_INT >= Build.VERSION_CODES.N -> {
connectivityManager.registerDefaultNetworkCallback(ConnectivityManagerCallback())
}
Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP -> {
lollipopNetworkRequest()
}
else -> {
context.registerReceiver(
networkReciever,
IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION)
)
}
}
}
@RequiresApi(Build.VERSION_CODES.N)
override fun onInactive() {
super.onInactive()
}
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
private fun lollipopNetworkRequest() {
val requestBuilder = NetworkRequest.Builder()
.addTransportType(NetworkCapabilities.TRANSPORT_ETHERNET)
.addTransportType(NetworkCapabilities.TRANSPORT_CELLULAR)
.addTransportType(NetworkCapabilities.TRANSPORT_WIFI)
connectivityManager.registerNetworkCallback(
requestBuilder.build(),
ConnectivityManagerCallback()
)
}
private fun ConnectivityManagerCallback(): ConnectivityManager.NetworkCallback {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
networkCallback = object : ConnectivityManager.NetworkCallback() {
override fun onLost(network: Network) {
super.onLost(network)
postValue(false)
}
override fun onAvailable(network: Network) {
super.onAvailable(network)
postValue(true)
}
}
} else {
throw IllegalAccessError("ERROR")
}
return networkCallback
}
private var networkReciever = object : BroadcastReceiver() {
override fun onReceive(context: Context?, intent: Intent?) {
updateConnection()
}
}
private fun updateConnection() {
val activeNetwork: NetworkInfo? = connectivityManager.activeNetworkInfo
if (activeNetwork?.isConnected == true) {
postValue(true)
} else {
postValue(false)
}
}
}
我花了一个星期,但我仍然不知道。请帮助我
任何帮助将不胜感激。
我将我的初始片段更改为 activity 并且我也将其从图表中删除,然后我使用 Thread 而不是像这样的处理程序来处理连接到互联网的部分
var ms: Long = 0
val splashTime = 2000
val splashActive = true
val paused = false
//use go() method here instead of this thread.check below to see go() method
Thread {
try {
while (splashActive && ms < splashTime) {
if (!paused)
ms += 100
Thread.sleep(100)
}
} catch (e: Exception) {
e.fillInStackTrace()
} finally {
if (!s.isOnline()) {
} else {
startActivity(Intent(this, MainActivity::class.java))
}
}
}.start()
我改变了我没有连接到互联网的部分:
val networkConnection = NetworkConnection(this)
networkConnection.observe(this) { isConnected ->
if (isConnected) {
binding.refresh.setOnClickListener {
binding.apply {
progress.visibility = View.VISIBLE
progress.animate().start()
connectivityText.visibility = View.INVISIBLE
refresh.visibility = View.INVISIBLE
}
//use go() method here instead of this thread.check below to see go() method
Thread {
Thread.sleep(2000)
startActivity(Intent(this, MainActivity::class.java))
}.start()
}
} else {
binding.apply {
progress.visibility = View.GONE
connectivityText.visibility = View.VISIBLE
refresh.visibility = View.VISIBLE
}
}
}
更新
将我连接到互联网的第一部分代码放到一个方法中,然后在任何你想要的地方使用该方法,如下所示:
fun go() {
val s = S(this)
Thread {
try {
while (splashActive && ms < splashTime) {
if (!paused)
ms += 100
Thread.sleep(100)
}
} catch (e: Exception) {
e.fillInStackTrace()
} finally {
if (!s.isOnline()) {
} else {
startActivity(Intent(this, MainActivity::class.java))
}
}
}.start()
}
如果你有更好的解决方案请告诉我