android 线程查看工具,Android中子线程网络查看器与Handler消息处理器

xml文件代码部分

xmlns:tools="http://schemas.android.com/tools"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical"

>

android:id="@+id/p_w_picpath"

android:layout_width="fill_parent"

android:layout_height="0dip"

android:layout_weight="1"

/>

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:orientation="horizontal"

>

android:id="@+id/edit"

android:layout_width="0dip"

android:layout_height="wrap_content"

android:layout_weight="1"

android:singleLine="true"

android:text="http://e.hiphotos.baidu.com/p_w_picpath/pic/item/d52a2834349b033b6764dedb17ce36d3d439bdc6.jpg"

/>

android:id="@+id/go"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="Go"

android:textSize="20sp"

/>

Activitypackage com.example.android01;

import java.io.InputStream;

import java.net.HttpURLConnection;

import java.net.URL;

import android.app.Activity;

import android.graphics.Bitmap;

import android.graphics.BitmapFactory;

import android.os.Bundle;

import android.os.Handler;

import android.os.Message;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.Button;

import android.widget.EditText;

import android.widget.ImageView;

import android.widget.Toast;

public class MainActivity extends Activity implements OnClickListener {

private ImageView p_w_picpath;

private EditText edit;

private Button but;

private final int success=0;

private Handler handler=new Handler(){

@Override

public void handleMessage(android.os.Message msg) {

Bitmap b=(Bitmap)msg.obj;

if(b!=null)

{

if(msg.what==success)

{

p_w_picpath.setImageBitmap(b);

}

}else{

Toast.makeText(getApplicationContext(), "获取图片错误", 0).show();

}

};

};

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

p_w_picpath=(ImageView) findViewById(R.id.p_w_picpath);

edit=(EditText) findViewById(R.id.edit);

but=(Button) findViewById(R.id.go);

but.setOnClickListener(this);

}

@Override

public void onClick(View v) {

final String url=edit.getText().toString();

new Thread(new Runnable() {

@Override

public void run() {

Bitmap bit=getImageFromNet(url);

//p_w_picpath.setImageBitmap(bit);

Message msg=new Message();

msg.obj=bit;

msg.what=success;

handler.sendMessage(msg);

}

}).start();

}

private Bitmap getImageFromNet(String url){

HttpURLConnection conn=null;

try {

URL mURL=new URL(url);//创建一个URL连接

conn=(HttpURLConnection) mURL.openConnection();//得到一个connection对象

conn.setRequestMethod("GET");//设置请求方法为GET

conn.setConnectTimeout(10000);//设置连接超时时间

conn.setReadTimeout(5000);//设置读取过程中的异常

conn.connect();

int responseCode=conn.getResponseCode();//获取响应码,404,500,200

if(responseCode==200){

//访问成功

InputStream in=conn.getInputStream();

Bitmap bitmap=BitmapFactory.decodeStream(in);//将从服务器获取的流变成Bitmap位图

return bitmap;

}else{

Toast.makeText(this, "获取图片失败", 0).show();

}

} catch (Exception e) {

e.printStackTrace();

}finally{

if(conn!=null)

{

conn.disconnect();

}

}

return null;

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
关于进程&线程查看器 用过vc++自带的Process Viewer,觉得这个小程序简单易用,有时候作用还很大,我就产生了自己做一个类似程序的想法,想着想着就着手作了起来。  首先我查看了有关进程和线程的api函(在tlhelp32.h中)了解了他们的用法。  具体实施阶段:   界面设计:该实用程序的界面几乎完全参考vc++自带的Process Viewer,以前从未接触过CListView,这回是从头开始作,我参考了"MFC Windows程序设计"里面有详细的关于CListView的介绍和实例,不过有些地方它并未讲清楚,你只能看mfc源代码凭经验猜测某个函的用法以及某些事件产生的消息,比如当所选Item改变时产生消息LVN_ITEMCHANGED,这个消息使我能在选中某一个Item时能通知线程视图更新。界面设计的另一个问题是分割窗口,这也是我第一次接触CSplitterWnd类,我使用了静态分割创建窗口的方法,以加入两个不同的视图其类分为:CThreadList和CProcessView. 下面简单介绍一下,该程序的功能正如其名,当你启动该程序时,它会检索当前在该系统中的进程和线程,并将进程的基本信息以列表的形式列出来,当你选中某一进程时,程序会在下面的线程视图中列出该进程的所有线程的基本信息。由于刚刚作玩,所以没有进行系统的测试,里面可能有一些bug希望用户发现后能与我联系,谢谢。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值