
com.path.android.jobqueue.network.NetworkUtilImpl Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of android-priority-jobqueue Show documentation
Show all versions of android-priority-jobqueue Show documentation
a Job Queue specifically written for Android to easily schedule jobs (tasks) that run in the background, improving UX and application stability.
package com.path.android.jobqueue.network;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
/**
* default implementation for network Utility to observe network events
*/
public class NetworkUtilImpl implements NetworkUtil, NetworkEventProvider {
private Listener listener;
public NetworkUtilImpl(Context context) {
context.getApplicationContext().registerReceiver(new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
if(listener == null) {//shall not be but just be safe
return;
}
//http://developer.android.com/reference/android/net/ConnectivityManager.html#EXTRA_NETWORK_INFO
//Since NetworkInfo can vary based on UID, applications should always obtain network information
// through getActiveNetworkInfo() or getAllNetworkInfo().
listener.onNetworkChange(isConnected(context));
}
}, new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION));
}
@Override
public boolean isConnected(Context context) {
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo = cm.getActiveNetworkInfo();
return netInfo != null && netInfo.isConnectedOrConnecting();
}
@Override
public void setListener(Listener listener) {
this.listener = listener;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy