Bu internet kontrol kodu internet.java diye ekleyebilrsin
import android.app.AlertDialog; import android.content.BroadcastReceiver; import android.content.Context; import android.content.DialogInterface; import android.content.Intent; import android.net.ConnectivityManager; import android.net.NetworkInfo; public class internet extends BroadcastReceiver { private static final String LOG_TAG = "Otomatik internet Kontrol¸"; static 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(final 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; //Log.v(LOG_TAG, "internete Bağlandınız!"); // Toast.makeText(context, "internete Bağlandınız!", Toast.LENGTH_LONG).show(); } return true; } } } } isConnected = false; AlertDialog.Builder builder = new AlertDialog.Builder(context); builder.setMessage("Lütfen internet bağlantınızın varlığını kontrol ediniz.").setPositiveButton("OK", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { System.exit(0); //internet yoksa burda napcağın yazıyo ben programı kapattırıyorum direk mesela } }); final AlertDialog alert = builder.create(); alert.show(); //Toast.makeText(context, "İnternet Yok", Toast.LENGTH_LONG).show(); //Log.v(LOG_TAG, "İnternet Yok!"); return false; } }
Bu kodlarda kontrol için
import android.annotation.SuppressLint; import android.annotation.TargetApi; import android.content.Intent; import android.content.IntentFilter; import android.net.ConnectivityManager; import android.os.Build; import android.support.v4.widget.DrawerLayout; import android.support.v7.app.ActionBarDrawerToggle; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.util.Log; import android.view.MenuItem; import android.webkit.WebSettings; import android.webkit.WebView; import android.webkit.WebViewClient; public class hepsi extends AppCompatActivity { DrawerLayout drawerLayout; ActionBarDrawerToggle drawerToggle; private static final String LOG_TAG = "Otomatik internet Kontrol¸"; private internet receiver; @TargetApi(Build.VERSION_CODES.HONEYCOMB) @SuppressLint("SetJavaScriptEnabled") protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_hepsi); IntentFilter filter = new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION); receiver = new internet(); registerReceiver(receiver, filter); WebView myWebView = (WebView) findViewById(R.id.wv1); myWebView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true); myWebView.getSettings().setBuiltInZoomControls(true); myWebView.getSettings().setDisplayZoomControls(false); myWebView.getSettings().setAllowFileAccess(true); myWebView.getSettings().setDomStorageEnabled(true); myWebView.getSettings().setJavaScriptEnabled(true); myWebView.getSettings().setJavaScriptEnabled(true); myWebView.loadUrl("https://siteadı"); WebSettings webSettings = myWebView.getSettings(); webSettings.setJavaScriptEnabled(true); myWebView.setWebViewClient(new WebViewClient()); } @SuppressLint("LongLogTag") @Override protected void onDestroy() { Log.v(LOG_TAG, "onDestory"); super.onDestroy(); unregisterReceiver(receiver); } }
İnternet varsa site yüklenir yoksa bildirim gelir internet bağlantınızı kontrol ve program kapanır