How to check my internet access on my android application

there is one menu in my application, you can only open it with an Internet connection, I will try to put some source code, but it doesn’t work ... can someone help me ..? this is my source code:

public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); String[] listpeta = new String[] { "TMII","Anjungan", "Museum", "Tempat Ibadah","Taman","Wahana Rekreasi"}; //Menset nilai array ke dalam list adapater sehingga data pada array akan dimunculkan dalam list this.setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, listpeta)); } @Override protected void onListItemClick(ListView l, View v, int position, long id) { super.onListItemClick(l, v, position, id); //Menangkap nilai text yang dklik Object o = this.getListAdapter().getItem(position); final String keyword = o.toString(); //Menampilkan list peta. final ProgressDialog myProgressDialog = ProgressDialog.show(ListPeta.this, "Loading", "Mohon Tunggu...!!!", true); new Thread() { public void run() { try{ Thread.sleep(1000); if(keyword=="TMII"){petapa="tmii";} else if(keyword=="Anjungan"){petapa="anjungan";} else if(keyword=="Museum"){petapa="museum";} else if(keyword=="Tempat Ibadah"){petapa="tempatibadah";} else if(keyword=="Taman"){petapa="taman";} else if(keyword=="Wahana Rekreasi"){petapa="rekreasi";} Intent slide2 = new Intent(ListPeta.this, FormPetaTmiiOnline.class); startActivity(slide2); } catch (Exception e) { } // Dismiss the Dialog myProgressDialog.dismiss(); } }.start(); } 

if someone helps me and gives me the source code .. tell me where I should put it in my source code ... :)

+4
source share
2 answers

Use this code to test connectivity:

 final ConnectivityManager conMgr = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE); final NetworkInfo activeNetwork = conMgr.getActiveNetworkInfo(); if (activeNetwork != null && activeNetwork.isConnected()) { //being here means you are connected } else { //being here means you are not connected } 

And also include this in your manifest:

 <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission> 

Thus, you can set the boolean value to true or false depending on the connectivity and use it to determine whether to open the menu or not.

+1
source

Try this code to check internet connection .

 public boolean isOnline() { ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo netInfo = cm.getActiveNetworkInfo(); if (netInfo != null && netInfo.isConnectedOrConnecting()) { return true; } // your code here(Toast) return false; } 

And include this permission in your manifest:

 <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission> 

and call this method before setContentView .

 /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); isOnline(); setContentView(R.layout.main); 
0
source

All Articles