如何在我的应用程序android上检查我的互联网访问

时间:2012-07-30 00:04:07

标签: android networking

我的应用程序上有一个菜单,只能打开网络连接,我试着放一些源代码,但它不起作用......任何人都可以帮助我......? 这是我的源代码:

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();

}

如果有人帮助我并给我一个源代码..告诉我在哪里我必须把它放在我的源代码上......:)

2 个答案:

答案 0 :(得分:1)

使用此代码检查连接:

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
} 

并在你的清单中加入:

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

因此,您可以根据连接将布尔值设置为true或false,并使用它来确定是否应该打开菜单。

答案 1 :(得分:0)

尝试使用此代码检查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;

}

在您的清单中包含此permission

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

并在setContentView之前调用此metod。

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