<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" > <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="当前网络:" /> <TextView android:id="@+id/txt_show" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hello_world" /> <Button android:id="@+id/button1" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Button" /> <Button android:id="@+id/button2" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Button" /> <ImageView android:id="@+id/imageView1" android:layout_width="278dp" android:layout_height="wrap_content" android:layout_weight="1.14" /> </LinearLayout>
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<String, Void, Drawable> { /** 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
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="app.android.demo" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="18" /> <uses-permission android:name="android.permission.INTERNET"/> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="app.android.demo.MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
效果 :
时间: 2024-12-19 08:27:41