博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
androi 多线程
阅读量:5009 次
发布时间:2019-06-12

本文共 6532 字,大约阅读时间需要 21 分钟。

 

 

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

  

效果 :

 

转载于:https://www.cnblogs.com/laopo/p/5827318.html

你可能感兴趣的文章
【转】每天一个linux命令(3):pwd命令
查看>>
merge-two-sorted-lists
查看>>
MySQL(3)
查看>>
poj1061——扩展gcd水题
查看>>
UVa400.Unix ls
查看>>
POJ 2299 Ultra-QuickSort 归并排序、二叉排序树,求逆序数
查看>>
Educational Codeforces Round 60 (Rated for Div. 2) C. Magic Ship
查看>>
Windows 2008 R2系统开机时如何不让Windows进行磁盘检测?
查看>>
WP7应用开发笔记(18) 本地化与多语言
查看>>
解决 .so文件64与32不兼容问题
查看>>
归并排序法
查看>>
【剑指offer】面试题26:复杂链表的复制
查看>>
spark开发生成EXE
查看>>
Vue 全家桶介绍
查看>>
WPF Bitmap转Imagesource
查看>>
Java compiler level does not match the version of the installed Java project facet.解决方法
查看>>
笔记_小结
查看>>
Linux lsof命令 umount U盘
查看>>
自定义Font
查看>>
linux svn 服务端搭建
查看>>