马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
原理概述 手机杀毒的话,其实就是通过PackageManager的管理者获得手机上面所有安装好的程序,然后在病毒库里面检测与当前程序是否与病毒库里面的程序匹配,如果匹配的话,那就有病毒,然后将其删除就行了! 在这之前需要一个DAO来进行检查是否有病毒: package cn.cbd.mobilesafe.activity.db.dao;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
public class AntVirusDao {
public static boolean isVirus(String md5)
{
boolean bol=false;
SQLiteDatabase db=SQLiteDatabase.openDatabase("/data/data/cn.cbd.mobilesafe.activity/files/antivirus.db", null,SQLiteDatabase.OPEN_READONLY);
Cursor cursor=db.rawQuery("select * from datable where md5=?", new String[]{md5});
while(cursor.moveToFirst())
{
bol=true;
}
cursor.close();
db.close();
return bol;
}
}
然后就是加载界面的主要类了:
package cn.cbd.mobilesafe.activity;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.graphics.Color;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.RotateAnimation;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;
import cn.cbd.mobilesafe.activity.db.dao.AntVirusDao;
import cn.cbd.mobilesafe.activity.util.MD5Util;
public class AntVirusActivity extends Activity {
private ImageView iv_scan;
private TextView tv_scan_stauts;
private ProgressBar pb_scan_status;
private LinearLayout ll_container;
private PackageManager pm;
private List<String> virusList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_kill_virus);
virusList=new ArrayList<String>();
tv_scan_stauts=(TextView) findViewById(R.id.tv_scan_stauts);
pb_scan_status=(ProgressBar) findViewById(R.id.pb_scan_status);
ll_container=(LinearLayout) findViewById(R.id.ll_container);
iv_scan=(ImageView) findViewById(R.id.iv_scan);
RotateAnimation animation=new RotateAnimation(0, 360, Animation.RELATIVE_TO_SELF, 1.0f, Animation.RELATIVE_TO_SELF, 1.0f);
animation.setDuration(1000);
animation.setRepeatCount(Animation.INFINITE);
iv_scan.startAnimation(animation);
new AsyncTask<Void, Object, Void>(){
@Override
protected void onPreExecute() {
tv_scan_stauts.setText("正在初始化八核扫描引擎...");
super.onPreExecute();
}
@Override
protected Void doInBackground(Void... params) {
try
{
Thread.sleep(1000);
pm=getPackageManager();
//通过包的管理者获得安装的程序集合
List<PackageInfo> packageInfos=pm.getInstalledPackages(PackageManager.GET_UNINSTALLED_PACKAGES | PackageManager.GET_SIGNATURES);
pb_scan_status.setMax(packageInfos.size()); //设置进度条的最大刻度
Log.i("aaa",""+packageInfos.size());
int total=0;
for(PackageInfo packageInfo : packageInfos)
{
//循环出每一个安装的程序
String signatures=packageInfo.signatures[0].toCharsString();
boolean isVirsus=AntVirusDao.isVirus(MD5Util.ecode(signatures));
//如果是病毒
if(isVirsus)
{
virusList.add(packageInfo.packageName);
}
Log.i("ccc",signatures);
total++;
pb_scan_status.setProgress(total);
publishProgress("正在扫描:"+packageInfo.applicationInfo.loadLabel(pm),packageInfo.applicationInfo,isVirsus);
Thread.sleep(200);
}
}
catch (Exception e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void result) {
tv_scan_stauts.setText("全部扫描完毕!");
iv_scan.clearAnimation();
if(virusList.size()>0) //检测病毒列表是不是大于0的如果大于0就弹出来一个对话框
{
AlertDialog.Builder builder=new Builder(AntVirusActivity.this);
builder.setTitle("警告");
builder.setMessage("发现病毒是否清理?");
builder.setPositiveButton("确定", new OnClickListener(){
@Override
public void onClick(DialogInterface dialog, int which) {
for (String packname : virusList) {
Intent intent = new Intent();
intent.setAction(Intent.ACTION_DELETE);
intent.setData(Uri.parse("package:" + packname));
startActivity(intent);
}
}
});
builder.setNegativeButton("取消", new OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
});
builder.create().show();
}
super.onPostExecute(result);
}
@Override
protected void onProgressUpdate(Object... values) {
/**
* 这里比较关键一些,这里的布局是这样的:
* <ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scrollbars="none">
<LinearLayout
android:id="@+id/ll_container"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
</LinearLayout>
</ScrollView>
也就是说,列表显示的部分是一个ScrollView布局
*/
tv_scan_stauts.setText((String)values[0]);
ApplicationInfo applicationInfo=(ApplicationInfo) values[1];
boolean isVirsus=(Boolean) values[2];
View view=View.inflate(getApplicationContext(), R.layout.layout_cache_item, null);
ImageView iv=(ImageView) view.findViewById(R.id.iv_cache);
TextView tv_name=(TextView) view.findViewById(R.id.tv_cache);
TextView tv_info=(TextView) view.findViewById(R.id.tv_cache_size);
iv.setImageDrawable(applicationInfo.loadIcon(pm));
tv_name.setText(applicationInfo.loadLabel(pm));
if(isVirsus)
{
tv_info.setTextColor(Color.RED);
tv_info.setText("发现病毒");
}else{
tv_info.setText("扫描安全");
}
ll_container.addView(view,0);
super.onProgressUpdate(values);
}
}.execute();
}
}
|