Android Antivirus example and fundamentals

Source: Internet
Author: User
Tags virus scan


This article is from the Linux commune website (www.linuxidc.com) Source Link: http://www.linuxidc.com/Linux/2011-10/46257.htm
Purely a starting point of the role, but also hope that the study of the students to shoot bricks!


one of the most core of antivirus software is a virus database is an antivirus engine, virus database from the server, the antivirus engine is actually to determine whether the package name and signature in the program matches the package name and signature in the virus library, if the match is a virus, the interface uses frame animation to display.

Ideas:
1. Download the virus repository information from the server to store the parsed data in the list collection
2. Get the package name of all apps in the phone and the signature of the program
3. Match the virus library to the phone app package name and signature
4. Auto-scroll display with ScrollView tag


The key code is as follows

Information about the Trojan virus library:

<?xml version= "1.0" encoding= "Utf-8"?>

<list>

<virus>
<name>tory.virus</name>
<packname>
Cn.itcast.virus
</packname>

<description>
Malware, read user logs </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 the virus database, get the package name and signature of the program and match it)
* (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 a scanned virus animation
New Thread () {
@Override
public void Run () {
try {
URL url = new URL ("Http://192.168.1.168:8080/virus.xml");
HttpURLConnection conn = (httpurlconnection) url.openconnection ();
InputStream is = Conn.getinputstream ();

Parse the virus library from the server and get the collection to the virus library
Virusbeans = Virusinfo.getvirusinfos (IS);
TaskInfo taskinfo = new TaskInfo (killvirusactivity.this); Instantiating the package explorer

Get all the package names in the current phone
Infos = pm.getinstalledapplications (0);
for (ApplicationInfo Info:infos) {
Packagenames.add (Info.packagename);
}
int count=0;

Antivirus engine according to the virus cubby the current system inside the package name signature to disinfect
StringBuilder sb = new StringBuilder ();
for (String packname:packagenames) {
Sb.append ("scanning" + packname);
Sb.append ("\ n");
Message msg = new Message ();
Msg.what = scanning;
Msg.obj = SB;
Handler.sendmessage (msg);
Check the current packname and corresponding signature http://www.linuxidc.com is not the same as the information in the virus database
for (Virusbean Virusbean:virusbeans) {
if (Packname.equals (Virusbean.getpackname ()) &&
Taskinfo.getappsignature (Packname). Equals (Virusbean.getsignature ()))
{
Virusresult.add (packname);//Add a virus
}
}
Count ++;//The total number of viruses recorded
}
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 (Message msg) {
Super.handlemessage (msg);
Switch (msg.what) {
Case scanning:
StringBuilder sb = (StringBuilder) msg.obj;
Tv_killvirus_info.settext (Sb.tostring ());
Sv.scrollby (0, 25);//Each increase will automatically move down the screen
Break
Case Scanning_finish:
int i = (Integer) msg.obj;
StringBuilder sb1 = new StringBuilder ();
Sb1.append ("Scan complete Scan" + i+ "program");
if (Virusresult.size () >0) {
Sb1.append ("Found 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 the permissions
return packinfo.signatures[0].tocharsstring ();

} catch (Namenotfoundexception e) {
E.printstacktrace ();
return null;
}
}
Show scanned file pages and scroll 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>

Android Antivirus example and fundamentals

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.