手机安全卫士第九天

来源:互联网 发布:南方大数据100指数评论 编辑:程序博客网 时间:2024/05/18 04:58

手机安全卫士第九天实现的功能:

手机杀毒功能


手机杀毒的原理:每个应用程序都会有对应的MD5值,我们只需要拿到应用程序的MD5值然后与病毒数据库中的值进行对比,若发现有相同的MD5值则判定为病毒文件,如没有相同的MD5值,也不能说明这不是一个病毒因为可能只是数据库中没有那个病毒的MD5。


手机杀毒实现的过程:

1: 创建杀毒页面AntiVirusActivity并创建对应的XML布局

2:获取每一个应用程序的MD5值并与病毒数据库文件进行对比

3:把每次对比的结果实时的展现在界面


首先创建AntiVirusActivity的XML布局和拷贝病毒数据库文件,拷贝病毒数据库文件和拷贝号码归属地方法一样所有直接调用即可,代码如下:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"              android:layout_width="match_parent"              android:layout_height="match_parent"              android:orientation="vertical">    <TextView        style="@style/TitleStyle"        android:text="病毒查杀"        />    <LinearLayout        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:layout_marginTop="10dp"        android:orientation="horizontal">        <FrameLayout            android:layout_width="wrap_content"            android:layout_height="wrap_content">            <ImageView                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:layout_marginLeft="10dp"                android:layout_marginRight="10dp"                android:background="@drawable/ic_scanner_malware"/>            <ImageView                android:id="@+id/iv_scanning"                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:layout_marginLeft="10dp"                android:layout_marginRight="10dp"                android:background="@drawable/act_scanning_03"/>        </FrameLayout>        <LinearLayout            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:layout_marginTop="10dp"            android:orientation="vertical">            <TextView                android:id="@+id/tv_init_virus"                android:layout_width="match_parent"                android:layout_height="wrap_content"                android:gravity="center_horizontal"                android:text="初始化8核杀毒引擎"/>            <ProgressBar                android:id="@+id/progressBar1"                style="?android:attr/progressBarStyleHorizontal"                android:layout_width="match_parent"                android:layout_height="wrap_content"                android:layout_marginLeft="10dp"                android:layout_marginRight="10dp"/>        </LinearLayout>    </LinearLayout>    <ScrollView        android:id="@+id/scrollView"        android:layout_width="match_parent"        android:layout_height="match_parent">        <LinearLayout            android:id="@+id/ll_content"            android:layout_width="match_parent"            android:layout_height="match_parent"            android:orientation="vertical">        </LinearLayout>    </ScrollView></LinearLayout>

创建好布局之后需要拿到用户手机里每个应用程序,然后用这个程序的MD5值与病毒数据库中进行对比,由于这是一个耗时操作需要开启一个子线程,每次对比一个应用程序,发消息给handler,然后更新UI线程:

   // 扫描开始    protected static final int BEGING = 1;    // 扫描中    protected static final int SCANING = 2;    // 扫描结束    protected static final int FINISH = 3;    private Message message;    Handler handler = new Handler() {        public void handleMessage(android.os.Message msg) {            switch (msg.what) {                case BEGING:                    tv_init_virus.setText("初始化八核引擎");                    break;                case SCANING:                    // 病毒扫描中:                    TextView child = new TextView(AntivirusActivity.this);                    ScanInfo scanInfo = (ScanInfo) msg.obj;                    // 如果为true表示有病毒                    if (scanInfo.desc) {                        child.setTextColor(Color.RED);                        child.setText(scanInfo.appName + "有病毒");                    } else {                        child.setTextColor(Color.BLACK);//// 为false表示没有病毒                        child.setText(scanInfo.appName + "扫描安全");                    }                    ll_content.addView(child, 0);                    break;                case FINISH:                    // 当扫描结束的时候。停止动画                    iv_scanning.clearAnimation();                    break;            }        }        ;<pre name="code" class="java">  private void initData() {        new Thread() {            public void run() {                message = Message.obtain();                message.what = BEGING;                PackageManager packageManager = getPackageManager();                // 获取到所有安装的应用程序                List<PackageInfo> installedPackages = packageManager                        .getInstalledPackages(0);                // 返回手机上面安装了多少个应用程序                int size = installedPackages.size();                // 设置进度条的最大值                pb.setMax(size);                int progress = 0;                for (PackageInfo packageInfo : installedPackages) {                    ScanInfo scanInfo = new ScanInfo();                    // 获取到当前手机上面的app的名字                    String appName = packageInfo.applicationInfo.loadLabel(                            packageManager).toString();                    scanInfo.appName = appName;                    String packageName = packageInfo.applicationInfo.packageName;                    scanInfo.packageName = packageName;                    // 首先需要获取到每个应用程序的目录                    String sourceDir = packageInfo.applicationInfo.sourceDir;                    // 获取到文件的md5                    String md5 = MD5Utils.getFileMd5(sourceDir);                    // 判断当前的文件是否是病毒数据库里面                    String desc = AntivirusDao.checkFileVirus(md5);                    // 如果当前的描述信息等于null说明没有病毒                    if (desc == null) {                        scanInfo.desc = false;                    } else {                        scanInfo.desc = true;                    }                    progress++;                    SystemClock.sleep(30);                    pb.setProgress(progress);                    message = Message.obtain();                    message.what = SCANING;                    message.obj = scanInfo;                    handler.sendMessage(message);                }                message = Message.obtain();                message.what = FINISH;                handler.sendMessage(message);            }            ;        }.start();    }    static class ScanInfo {        boolean desc;        String appName;        String packageName;    }

每次handler收到消息,会实时的提示用户当前应用程序是否安全,由于病毒数据会实时增加,所有需要更新病毒数据库文件,所有在 SplashActivity中连接服务器并更新数据库,连接服务器拿到JSON串,若是数组形式的JSON,则使用谷歌开发的GSON进行解析,解析之后把病毒插入到本地数据库中。

GItHub下载地址: https://github.com/AndroidDai/mobliesafe/tree/master/MobileSafe

0 0
原创粉丝点击