handle 的初始化。(主线程)
private Handler handle= new Handler(){ public void handleMessage(android.os.Message msg) { switch (msg.what) { case SCANING: ScanInfo scanInfo=(ScanInfo) msg.obj; tv_scan_status.setText("正在扫描:"+scanInfo.name); TextView tv= new TextView(getApplicationContext()); if(scanInfo.isvirus){ tv.setTextColor(Color.RED); tv.setText("发现病毒:"+scanInfo.name); }else{ tv.setTextColor(Color.BLACK); tv.setText("扫描安全:"+scanInfo.name); } ll_scan_container.addView(tv,0); break; case FINISH: tv_scan_status.setText("扫描完毕"); iv_scan.clearAnimation(); break; } }; };
把类提取出来 作为子线程和主线程之间数据交互的桥梁
/** * 扫描信息的内布拉 * @author lyq * */ class ScanInfo{ String packname; String name; boolean isvirus; }
子线程的中的Message 设置。
new Thread() { public void run() { List<PackageInfo> infos= pm.getInstalledPackages(0);//获取所有的安装应用 try { Thread.sleep(500); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } pb_scan.setMax(infos.size()); int progress=0; for(PackageInfo info:infos){ String datadir= info.applicationInfo.dataDir; String sourcedir=info.applicationInfo.sourceDir; String md5=getFileMd5(sourcedir); ScanInfo scanInfo= new ScanInfo(); scanInfo.name=info.applicationInfo.loadLabel(pm).toString(); scanInfo.packname=info.applicationInfo.packageName; System.out.println(md5+":"+info.applicationInfo.loadLabel(pm)); if(AntivirsuDao.isVirus(md5)){ //发现病毒 scanInfo.isvirus=true; }else{ //扫描安全 scanInfo.isvirus=false; } progress++; pb_scan.setProgress(progress); Message msg= Message.obtain(); msg.obj=scanInfo; msg.what=SCANING; handle.sendMessage(msg); try { Thread.sleep(500); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } Message msg= Message.obtain(); msg.what=FINISH; handle.sendMessage(msg); }; }.start();