Ad
Is There Any Ways To Detect The Exact Time That User Turns Off WIFI Or Data?
I searched and it turns out that
Broadcast Receiver
is not recommended for the target over N.I found out
GCMNetworkManager
. And it is also depreciated.- I also checked
JobScheduler
. However, its behaviour is not something that I want to implement.
The only way that I can implement among the three is checking the internet status periodically.
However, I don't want it. What I want is detecting the event when user turns them off or on. And then, I'd like to show a dialog to make sure the user turn it on again.
My college who makes iOS says, he uses Observer
for his iOS app. And said maybe Android is the same.
Is there any way to make it possible?
Ad
Answer
You could consider those in Manifest.
<receiver android:name="receiver.NetworkChangeReceiver">
<intent-filter>
<action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
<action android:name="android.net.wifi.WIFI_STATE_CHANGED" />
</intent-filter>
</receiver>
However, those are deprecated.
Instead, you can declare it programmatically.
- Create a receiver class.
class NetworkChangeReceiver : BroadcastReceiver() {
var dialog: Dialog? = null
override fun onReceive(context: Context, intent: Intent) {
val isConnected = NetworkUtil.getConnectivityStatusString(context)
Toast.makeText(context, "Intere State Changed: $isConnected", Toast.LENGTH_SHORT).show()
}
}
- In your
Activity
.
// Internet Check Receiver
val intentFilter = IntentFilter("android.net.conn.CONNECTIVITY_CHANGE")
this.registerReceiver(NetworkChangeReceiver(), intentFilter)
Ad
source: stackoverflow.com
Related Questions
- → should I choose reactjs+f7 or f7+vue.js?
- → Phonegap Android write to sd card
- → Local reference jquery script in nanohttpd (Android)
- → Click to navigate on mobile devices
- → How to allow api access to android or ios app only(laravel)?
- → Access the Camera and CameraRoll on Android using React Native?
- → React native change listening port
- → What is the default unit of style in React Native?
- → Google play market autocomplete icon
- → Warning: Each child in an array or iterator should have a unique "key" prop. Check the render method of `ListView`
- → Using Laravel with Genymotion
- → react native using like web-based ajax function
- → react native pdf View
Ad