java
package app.android.demo;import android.os.AsyncTask;import android.os.Bundle;import android.telephony.TelephonyManager;import android.util.Log;import java.io.IOException;import java.net.URL;import android.app.Activity;import android.content.Context;import android.graphics.drawable.Drawable;import android.net.ConnectivityManager;import android.net.NetworkInfo;import android.view.Menu;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.ImageView;import android.widget.TextView;import android.widget.Toast;public class MainActivity extends Activity{ private ImageView mImageView = null ; Button mButton1; Button mButton2; Button mButton3; Button mButton4; TextView mTextView; TextView mTextView1; ConnectivityManager connectMgr; NetworkInfo[] networkInfo; //The image url we are downloading from internet . private final String IMAGE_URL1 = "http://a.hiphotos.baidu.com/image/pic/item/c2fdfc039245d688acbcd54ba0c27d1ed31b24a6.jpg" ; private final String IMAGE_URL2 = "http://imgsrc.baidu.com/forum/w%3D580/sign=f9f1029647166d223877159c76230945/b69ad3f9d72a6059892764472c34349b023bbaca.jpg" ; private final String IMAGE_URL3 = "http://static.open-open.com/lib/uploadImg/20130810/20130810175659_519.jpg" ; private final String IMAGE_URL4 = "http://imgsrc.baidu.com/forum/w%3D580/sign=f9f1029647166d223877159c76230945/b69ad3f9d72a6059892764472c34349b023bbaca.jpg" ; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mImageView = (ImageView)this.findViewById(R.id.imageView1) ; mButton1=(Button) findViewById(R.id.button1); mButton2=(Button) findViewById(R.id.button2); mTextView=(TextView) findViewById(R.id.txt_show); mTextView1=(TextView) findViewById(R.id.textView1); if (isNetworkAvailable(MainActivity.this)) { mButton1.setOnClickListener(new Button1Lislen()); mButton2.setOnClickListener(new Button2Lislen()); mTextView1.setText("当前网络:=>可用"); Toast.makeText(getApplicationContext(), "当前有可用网络!", Toast.LENGTH_LONG).show(); } else { mTextView1.setText("当前网络:=>不可用"); Toast.makeText(getApplicationContext(), "当前没有可用网络!", Toast.LENGTH_LONG).show(); } } class Button1Lislen implements OnClickListener { @Override public void onClick(View v) { new Thread(new Runnable(){ @Override public void run() { // TODO Auto-generated method stub final Drawable drawable = loadImageFromNetwork(IMAGE_URL1); mImageView.post(new Runnable(){ @Override public void run() { // TODO Auto-generated method stub mImageView.setImageDrawable(drawable) ; }}) ; } }).start() ; } } private Drawable loadImageFromNetwork(String imageUrl) { Drawable drawable = null; try { drawable = Drawable.createFromStream( new URL(imageUrl).openStream(), "image.gif"); } catch (IOException e) { Log.d("test", e.getMessage()); } if (drawable == null) { Log.d("test", "null drawable"); } else { Log.d("test", "not null drawable"); } return drawable ; } class Button2Lislen implements OnClickListener { @Override public void onClick(View v) { new DownloadImageTask().execute(IMAGE_URL2) ; } } private class DownloadImageTask extends AsyncTask{ /** The system calls this to perform work in a worker thread and * delivers it the parameters given to AsyncTask.execute() */ protected Drawable doInBackground(String... urls) { return loadImageFromNetwork(urls[0]); } /** The system calls this to perform work in the UI thread and delivers * the result from doInBackground() */ protected void onPostExecute(Drawable result) { mImageView.setImageDrawable(result); } } //to see if these is a network public boolean isNetworkAvailable(Activity activity) { Context context = activity.getApplicationContext(); // 获取手机所有连接管理对象(包括对wi-fi,net等连接的管理) ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); if (connectivityManager == null) { return false; } else { // 获取NetworkInfo对象 NetworkInfo[] networkInfo = connectivityManager.getAllNetworkInfo(); if (networkInfo != null && networkInfo.length > 0) { for (int i = 0; i < networkInfo.length; i++) { System.out.println(i + "===状态===" + networkInfo[i].getState()); System.out.println(i + "===类型===" + networkInfo[i].getTypeName()); mTextView.setText("===网络状态===>" + networkInfo[i].getState()+"\n"+ "===网络类型===>" + networkInfo[i].getTypeName()); //Toast.makeText(MainActivity.this, "===状态===" + networkInfo[i].getState()+networkInfo[i].getTypeName(), 3000).show(); // 判断当前网络状态是否为连接状态 if (networkInfo[i].getState() == NetworkInfo.State.CONNECTED) {// mTextView.setText("===网络状态===>" + networkInfo[i].getState()+"\n"+// "===网络类型===>" + networkInfo[i].getTypeName()); return true; }// mTextView.setText("===状态===>" + networkInfo[i].getState()+"\n"+// "===类型===>" + networkInfo[i].getTypeName()); } } } return false; } }
权限:
manifest
效果 :