Thursday, September 22, 2011

connect internet with android exam

note use emulator
setting -> network-> wireless–>airplane==true
@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Log.i("connect", "connect-internet");
        if(isOnline()==false)
        {
         Log.i("connect", "isOnline()");
         Log.i("connect", "No connect");
        }
        else
        {}
    }
 public boolean isOnline() {
   Context context = getApplicationContext();
   ConnectivityManager connectivity = 
(ConnectivityManager) 
context.getSystemService(Context.CONNECTIVITY_SERVICE);
   if (connectivity == null) {
    return false;
   } else {
    NetworkInfo[] info = connectivity.getAllNetworkInfo();
    if (info != null) {
     for (int i = 0; i < info.length; i++) {
      if (info[i].getState() == NetworkInfo.State.CONNECTED) {
       return true;
      }
     }
    }
   }
   return false;
  }
AndroidManifest.xml
<uses-permission android:name=”android.permission.INTERNET”></uses-permission>
<uses-permission android:name=”android.permission.ACCESS_NETWORK_STATE”>
</uses-permission>

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.