Created
March 4, 2020 12:45
-
-
Save mehmetbalbay/997d058b49e2dc5dbf73e81e09954e48 to your computer and use it in GitHub Desktop.
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
import android.content.BroadcastReceiver; | |
import android.content.Context; | |
import android.content.Intent; | |
import android.net.ConnectivityManager; | |
import android.net.NetworkInfo; | |
import com.yarolegovich.lovelydialog.LovelyInfoDialog; | |
public class NetworkChangeReceiver extends BroadcastReceiver { | |
Context context; | |
boolean isConnected = false; | |
@Override | |
public void onReceive(final Context context, final Intent intent) { | |
isNetworkAvailable(context); //receiver çalıştığı zaman çağırılacak method | |
} | |
private boolean isNetworkAvailable(Context context) { | |
ConnectivityManager connectivity = (ConnectivityManager) | |
context.getSystemService(Context.CONNECTIVITY_SERVICE); //Sistem ağını dinliyor internet var mı yok mu | |
if (connectivity != null) { | |
NetworkInfo[] info = connectivity.getAllNetworkInfo(); | |
if (info != null) { | |
for (int i = 0; i < info.length; i++) { | |
if (info[i].getState() == NetworkInfo.State.CONNECTED) { | |
if(!isConnected){ //internet varsa | |
isConnected = true; | |
} | |
return true; | |
} | |
} | |
} | |
} | |
isConnected = false; | |
new LovelyInfoDialog(context) | |
.setTopColorRes(R.color.colorPrimary) | |
.setIcon(R.drawable.ic_info_outline_black_24dp) | |
//This will add Don't show again checkbox to the dialog. You can pass any ID as argument | |
.setTitle(R.string.info_title) | |
.setMessage(R.string.info_message) | |
.setCancelable(false) | |
.show(); | |
return false; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment