DroidNet is an Android Networking Library listening for network connection state and Internet connectivity with assumption that active internet connection or not. Connecting to a network doesn’t necessarily mean that device has active internet connection
These instructions will help you set up this library easily on your current project and working in no time. You only need a few configurations to start working!
To be able to use the following library, you will need to add the following gradle dependency in your
repositories {
jcenter()
}
implementation 'com.job:droidnet:2.0.0'
That is the basic set up needed to be able to use the library in your applications!
First, we need to add these permission to our Android Manifest file :
<uses-permission android:name="android.permission.INTERNET" />
That's it, you have set up the required permissions and ready to go!
In your Main Application
import com.droidnet.DroidNet;
...
public class MyApp extends Application {
@Override
public void onCreate() {
super.onCreate();
DroidNet.init(this);
}
@Override
public void onLowMemory() {
super.onLowMemory();
DroidNet.getInstance().removeAllInternetConnectivityChangeListeners();
}
}
In your Activity
import com.droidnet.DroidListener;
import com.droidnet.DroidNet;
...
public class MainActivity extends AppCompatActivity implements DroidListener {
//...
private DroidNet mDroidNet;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//...
mDroidNet = DroidNet.getInstance();
mDroidNet.addInternetConnectivityListener(this);
}
@Override
protected void onDestroy() {
super.onDestroy();
mDroidNet.removeInternetConnectivityChangeListener(this);
}
@Override
public void onInternetConnectivityChanged(boolean isConnected) {
if (isConnected) {
//do Stuff with internet
netIsOn();
} else {
//no internet
netIsOff();
}
}
private void netIsOn(){...}
private void netIsOff(){...}
}
Take a look at this sample project which has a sample implementation of the library.
Please feel free to contribute or open issues, if any and I will be happy to help out!
You are all set.