This article illustrates the implementation principle of Android antivirus. Share to everyone for your reference, specific as follows:
An antivirus soft armor is the most core part of a virus is an anti-virus engine, virus library from the server, antivirus engine is actually to determine whether the program's package name and signature match the virus library in the package name and signature, if the match is a virus, the interface using frame animation to display.
Ideas:
1. Download the virus version library information from the server side to store the parsed data in the list collection
2. Get the package name and program signature of all applications on the phone
3. Match the virus database to the phone application package name and signature
4. Automatic scrolling display with ScrollView label
The key code is as follows:
Trojan Horse Virus Library information:
<?xml version= "1.0" encoding= "Utf-8"?> <list> <virus> <name>tory.virus</name> < packname> Cn.itcast.virus </packname> <description> malware, reading user log </description> <signature
> 3082020730820170a00302010202044ea7598f300d06092a864886f70d010105050030483
10a30080603550406130131310a30080603550408130131310a3008060355040713013131
0a3008060355040a130131310a3008060355040b130131310a30080603550403130131301
e170d3131313032363030353132375a170d3231313032333030353132375a3048310a3008
0603550406130131310a30080603550408130131310a30080603550407130131310a30080
60355040a130131310a3008060355040b130131310a3008060355040313013130819f300d
06092a864886f70d010101050003818d0030818902818100d915d7a98cde8bcd69b87ec52
11012ace847de42129a71bf679a059c2c55e893bc0ea886874432ab8b9097724211df6769
Eacd3381ccac779ab7422d8101320b1e0b14e06ac8ee095b20e52cbe6163e10a87dc410b8 a91fb73d53c5bdb4a22d1295c61e04b8f8b68c475e69c1754a1dc35745e7c6ae0275c2620 b863b0d9ea8f0203010001300d06092a864886f70d01010505000381810038e1119fbb710
4180fddba4bc8b2c275df63f0df418b7480d8eba2891da20d34d3d083cfed7bb3eb546863
C76bc67cc93f2fa0e9377c470881c9a763c99cc035093184bb50f76e74155592eca3566a3 10af55e5fec19d6fdc1a74f226aef485f84389126e8e3f4b59fe2797cbfcac660b9f2cc81 e6f3dcaa7cb2001ecc496a7b </
Signature> </virus> </list>
Antivirus engine:
* * Antivirus engine (download virus library, get package name and signature of program and Match) * (non-javadoc) * @see android.app.activity#ontouchevent (android.view.MotionEvent) *
/@Override public boolean ontouchevent (Motionevent event) {packagenames = new arraylist<string> ();
Virusresult = new arraylist<string> ();
Infos = new arraylist<applicationinfo> (); Animationdrawable.start ()//Play scan virus new Thread () {@Override public void run () {try {URL url = new URL ("Htt
P://192.168.1.168:8080/virus.xml ");
HttpURLConnection conn = (httpurlconnection) url.openconnection ();
InputStream is = Conn.getinputstream ();
The virus library is parsed from the server and a collection of virus libraries is obtained Virusbeans = Virusinfo.getvirusinfos (IS); TaskInfo taskinfo = new TaskInfo (killvirusactivity.this);
Instantiate the Package Resource Manager//get to the current phone inside all the package names Infos = pm.getinstalledapplications (0);
for (ApplicationInfo Info:infos) {packagenames.add (info.packagename);
int count=0;
Antivirus engine according to virus cubby the current system inside the package name signature for antivirus StringBuilder sb = new StringBuilder (); for (String packname:packagenames) {sb.append ("scanning" + packname);
Sb.append ("\ n");
msg = new Message ();
Msg.what = scanning;
Msg.obj = SB;
Handler.sendmessage (msg); Check that the current packname and the corresponding signature are the same as the information in the virus library for (Virusbean Virusbean:virusbeans) {if (Packname.equals Virusbean.getpac Kname ()) && taskinfo.getappsignature (packname). Equals (Virusbean.getsignature ()) {Virusresult
. Add (Packname);//Adding a virus}} Count ++;//recording the total number of viruses} message msg = new Message ();
Msg.what = Scanning_finish;
Msg.obj = count;
Handler.sendmessage (msg);
catch (Exception e) {e.printstacktrace ();
}}.start ();
Return Super.ontouchevent (event);
}
Display virus Scan information:
Handler Handler = new Handler () {
@Override public
void Handlemessage (msg) {
super.handlemessage ( msg);
Switch (msg.what) {case
scanning:
StringBuilder sb = (StringBuilder) msg.obj;
Tv_killvirus_info.settext (Sb.tostring ());
Sv.scrollby (0, 25);//Every increase will automatically move down the screen break
;
Case scanning_finish:
int i = (Integer) msg.obj;
StringBuilder sb1 = new StringBuilder ();
Sb1.append ("Scan complete scanning" + i+ "program");
if (Virusresult.size () >0) {
sb1.append ("Virus \ n");
for (String packname:virusresult) {
sb1.append ("Virus name" + packname);
Sb1.append ("\ n")
;
}
Tv_killvirus_info.settext (Sb1.tostring ());
Animationdrawable.stop ();
break;
}
}
;
Get the signature of the program:
* * Get the signature of the program
/Public String getappsignature (string packname) {
try {
PackageInfo packinfo = Pm.getpackageinfo (Packname, packagemanager.get_signatures);
Get all Permissions return
packinfo.signatures[0].tocharsstring ();
} catch (Namenotfoundexception e) {
E.printstacktrace ();
return null;
}
}
Displays the scanned file page and scrolls automatically:
<scrollview
android:layout_width= "wrap_content"
android:layout_height= "Wrap_content"
Android : layout_below= "@id/iv_killvirus_am"
android:id= "@+id/sv_killvirus"
>
<textview
Android:layout_width= "Wrap_content"
android:layout_height= "wrap_content"
android:id= "@+id/tv_" Killvirus_info "
></TextView>
</ScrollView>
I hope this article will help you with the Android program.