Last active
November 10, 2017 12:36
-
-
Save alfianyusufabdullah/07631f4484d68b1bcb07fb28398197ae to your computer and use it in GitHub Desktop.
Cara Barbar Cek Realtime Network Status *cara barbar jangan baper
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package jonesrandom.kamusobat.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; | |
import android.os.Build; | |
import android.support.annotation.NonNull; | |
import android.support.annotation.RequiresApi; | |
import android.support.v4.net.ConnectivityManagerCompat; | |
import android.util.Log; | |
import android.widget.Toast; | |
import java.io.IOException; | |
import okhttp3.Call; | |
import okhttp3.Callback; | |
import okhttp3.OkHttpClient; | |
import okhttp3.Request; | |
import okhttp3.Response; | |
/** | |
* Created by jonesrandom on 11/8/17. | |
* <p> | |
* #JanganLupaBahagia | |
*/ | |
public class AppConnections { | |
private NetworkListener listener; | |
private int STATUS_CHECKING = 0; | |
public static final int NET_CONNECTED = 1; | |
public static final int NET_CONNECTING = 2; | |
public static final int NET_DISCONNECTED = 0; | |
public AppConnections( NetworkListener listener) { | |
this.listener = listener; | |
} | |
private BroadcastReceiver networkReceiver = new BroadcastReceiver() { | |
@Override | |
public void onReceive(Context context, Intent intent) { | |
Log.d("Connection_status", "onReceive: Connection Change"); | |
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); | |
if (cm != null) { | |
NetworkInfo netInfo = cm.getActiveNetworkInfo(); | |
if (netInfo != null && netInfo.isConnected()) { | |
fireNetworkCall(); | |
} else { | |
listener.onConnections(NET_DISCONNECTED); | |
STATUS_CHECKING = 0; | |
} | |
} | |
} | |
}; | |
private void fireNetworkCall() { | |
Log.d("Connection_status", "start call server"); | |
if (STATUS_CHECKING == 1) { | |
OkHttpClient client = new OkHttpClient(); | |
Request request = new Request.Builder().url("https://firebase.google.com").build(); | |
client.newCall(request).enqueue(new Callback() { | |
@Override | |
public void onFailure(@NonNull Call call, @NonNull IOException e) { | |
listener.onConnections(NET_DISCONNECTED); | |
Log.d("Connection_status", "onResponse: DISCONNECTED upTime : " + System.currentTimeMillis()); | |
listener.onConnectionDone(); | |
} | |
@Override | |
public void onResponse(@NonNull Call call, @NonNull Response response) throws IOException { | |
int responCode = response.code(); | |
if (responCode == 200) { | |
listener.onConnections(NET_CONNECTED); | |
Log.d("Connection_status", "onResponse: CONNECTED upTime : " + System.currentTimeMillis()); | |
} else { | |
listener.onConnections(NET_CONNECTING); | |
Log.d("Connection_status", "onResponse: CONNECTING upTime : " + System.currentTimeMillis()); | |
} | |
listener.onConnectionDone(); | |
} | |
}); | |
} | |
} | |
public void registerReceiver(Context context) { | |
STATUS_CHECKING = 1; | |
context.registerReceiver(networkReceiver, new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION)); | |
} | |
public void unregisterReceiver(Context context) { | |
if (networkReceiver != null) { | |
STATUS_CHECKING = 0; | |
context.unregisterReceiver(networkReceiver); | |
} | |
} | |
public void fireNetworkCall() { | |
fireNetworkCall(); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package jangan.lupa.bahagia; | |
/** | |
* Created by jonesrandom on 11/9/17. | |
* | |
* #JanganLupaBahagia | |
*/ | |
public interface NetworkListener { | |
void onConnections(int Stat); | |
void onConnectionDone(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment