码迷,mamicode.com
首页 > 移动开发 > 详细

我的手机管家(10)病毒扫描 拷贝病毒数据库

时间:2016-07-05 08:46:51      阅读:736      评论:0      收藏:0      [点我收藏+]

标签:

我的手机管家(10)病毒扫描    拷贝病毒数据库

(1)将病毒数据库复制到asserts文件夹下,然后复制到

"data/data/com.chb.myphonesafe/files/antivirus.db"

//数据库路径必须是data/data目录下,否则无法访问

拷贝:AssetUtil

 

package com.chb.myphonesave.util;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;

import android.app.ProgressDialog;
import android.content.Context;

public class AssetUtil {

/**
     * 从资产目录拷贝文件
     * @param context
     * @param filename
     *            资产目录的文件的名称
     * @param destfilename
     *            目标文件的路径
     * @return
     */
    public static File copy1(Context context, String filename, String destfilename, ProgressDialog pd) {

        try {
            InputStream in = context.getAssets().open(filename);
           
            File file = new File(destfilename);
            OutputStream out = new FileOutputStream(file);
            byte[] byt = new byte[1024];
            int len = 0;
            int total = 0;
            while ((len = in.read(byt)) != -1) {
                out.write(byt, 0, len);
                total += len;
            }
            out.flush();
            out.close();
            in.close();

            return file;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
}

拷贝数据使用子线程

/**
         * 将assert文件夹下的antivirus.db拷贝到指定位置
         * 拷贝病毒库的数据库文件
         */
        new Thread() {
            public void run() {
                File file = new File(getFilesDir(), "antivirus.db");
                Log.i("tag", "病毒的地址:"+file.getAbsolutePath());
                if (file.exists() && file.length() > 0) {//数据库文件已经拷贝成功

                } else {
                    AssetUtil.copy1(getApplicationContext(), "antivirus.db", file.getAbsolutePath(), null);
                }
            };

        }.start();

 

 

(2)手机病毒数据库管理,通过md5签名来获取应用的描述信息

查看是否是病毒

package com.chb.myphonesave.dao;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
/**
 * 手机病毒库的管理 
 * 通过md5值获取应用描述
 */
public class AntivirusDao {
    public static final String PATH="data/data/com.chb.myphonesafe/files/antivirus.db";

    public static String find(String md5){
        String desc=null;//数据描述
        try { 
            SQLiteDatabase db=SQLiteDatabase.openDatabase(PATH, 
                    null, 
                    SQLiteDatabase.OPEN_READONLY);//数据库路径必须是data/data目录下,否则无法访问
            Cursor cursor = db.rawQuery("select desc from datable where md5=?",new String[]{md5});

            if(cursor.moveToFirst()){
                desc = cursor.getString(0);
            }
            cursor.close();
            db.close();//关闭数据库
        } catch (Exception e) {
            e.printStackTrace();
        }
        return desc;
    }

}

 

逻辑代码:扫描病毒和显示应用的信息是使用

Thread和Handler结合。

package com.chb.myphonesave.activity;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import android.app.Activity;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.Signature;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.animation.Animation;
import android.view.animation.LinearInterpolator;
import android.view.animation.RotateAnimation;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import com.chb.myphonesave.R;
import com.chb.myphonesave.dao.AntivirusDao;
import com.chb.myphonesave.entity.ScannerInfo;
import com.chb.myphonesave.util.MD5Utils;
/**
 *杀毒功能 
 */
public class AntiVirusActivity extends Activity{
    private ImageView ivScanning;
    private TextView tvStatus;
    private ProgressBar progressBar;
    private PackageManager pm;
    
    private LinearLayout layout;
    private List<ScannerInfo> list;//病毒集合
    
    private Handler myHandler = new Handler(){
        public void handleMessage(Message msg) {
            switch (msg.what) {
            case 0://扫描完毕
                tvStatus.setText("扫描完毕");
                ivScanning.clearAnimation();//停止扫描
                //判断是否有病毒
                if(list.isEmpty()){//没有病毒
                    Toast.makeText(AntiVirusActivity.this, "您的手机非常安全", Toast.LENGTH_LONG).show();
                }else {//发现病毒,弹出杀毒
                    showAlterDailog();
                }
                
                break;
            case 1://更新状态
                ScannerInfo info = (ScannerInfo) msg.obj;
                tvStatus.setText("正在扫描:"+info.virusName);
                TextView textView= new TextView(getApplicationContext());
                if(info.isVirus){
                    textView.setText("发现病毒:"+info.virusName);
                    textView.setTextColor(Color.RED);
                }else{
                    textView.setText("扫描安全:"+info.virusName);
                    textView.setTextColor(Color.BLACK);
                }
                layout.addView(textView,0);
                break;
            default:
                break;
            }
            
            
        };
    };
    
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_antivirus);
        //初始化控件 
        pm = getPackageManager();
        ivScanning = (ImageView) findViewById(R.id.antivirus_scanning);
        tvStatus = (TextView) findViewById(R.id.antivirus_status);
        progressBar = (ProgressBar) findViewById(R.id.progressBar1);
        layout = (LinearLayout) findViewById(R.id.antivirus_layout);
        //设置扫描雷达的动态
        RotateAnimation animation = new RotateAnimation(0, 360,Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f);
        animation.setDuration(1000);
        animation.setRepeatCount(Animation.INFINITE);//无限循环
        animation.setInterpolator(new LinearInterpolator());//匀速运转
        ivScanning.setAnimation(animation);
        //更新进度条
        new Thread(){
            public void run() {
                //休眠2秒钟
                try {
                    Thread.sleep(2000);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                
                // 获取所有已安装和应用的签名信息
                 List<PackageInfo> packages=pm.getInstalledPackages(PackageManager.GET_SIGNATURES+PackageManager.GET_UNINSTALLED_PACKAGES);
                 progressBar.setMax(packages.size());
                 int progress=0;
                 Random random = new Random();
                 list = new ArrayList<ScannerInfo>();
                 for(PackageInfo ps:packages){
                     //获取病毒名称,病毒描述
                     ScannerInfo scannerInfo = new ScannerInfo();
                     
                     Signature [] sns = ps.signatures;
                     //应用名称
                     String name = ps.applicationInfo.loadLabel(pm).toString();
                     
                     scannerInfo.virusName = name;
                     //计算签名特征的MD5
                     String md5= MD5Utils.getMD5(sns[0].toCharsString());
                     String desc=AntivirusDao.find(md5);
                     
                     scannerInfo.desc = desc;
                     scannerInfo.packageName= ps.packageName;
                     if(desc!=null){//是病毒
                         scannerInfo.isVirus=true;
                         list.add(scannerInfo);
                     }else{//不是病毒
                        scannerInfo.isVirus = false;
                     }
                     progress++;
                     progressBar.setProgress(progress);
                     //更新查询状态
                     Message msg = Message.obtain();
                     msg.obj = scannerInfo;
                     msg.what=1;//更新状态
                     myHandler.sendMessage(msg);
                 }
                 try {
                    Thread.sleep(50+random.nextInt(50));
                } catch (Exception e) {
                    e.printStackTrace();
                }
                
                myHandler.sendEmptyMessage(0);//扫描完毕
            };
        }.start();
    }
    
    public void showAlterDailog(){
        
        Builder builder = new Builder(this);
        builder.setTitle("警告,有病毒");
        builder.setMessage("发现"+list.size()+"个病毒,建议立即处理");
        builder.setPositiveButton("卸载",
                new OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
            for(ScannerInfo info:list){
                //跳转到卸载界面
                Intent intent = new Intent(Intent.ACTION_DELETE);
                intent.setData(Uri.parse("package:"+info.packageName));
                intent.addCategory(Intent.CATEGORY_DEFAULT);
                startActivity(intent);
            }
            }
        });
        
        builder.setNegativeButton("暂时不处理", null);
        builder.create();
        builder.show();
    }
    
     

}

扫描的信息:是否是病毒

package com.chb.myphonesave.entity;
/**
 * 病毒实体类
 * @author Administrator
 *
 */
public class ScannerInfo{
    public String virusName;//病毒名称
    public boolean isVirus;//是否是病毒
    public String packageName;
    public String desc;//病毒描述
}

 

我的手机管家(10)病毒扫描 拷贝病毒数据库

标签:

原文地址:http://www.cnblogs.com/chengbao/p/5642257.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!