Android anti-virus Implementation Principles and Examples

Source: Internet
Author: User
Tags virus scan

The most important part of a antivirus software package is the virus database and the virus engine. The virus database is obtained from the server. the antivirus engine is used to determine whether the package name and signature in the program match the package name and signature in the virus database, if the match is a virus, frame animation is used to display the interface.

 

Ideas:

1. Download the virus version library information from the server and store the parsed data in the list set.

2. Obtain the package name and signature of all applications on the mobile phone.

3. Match the virus library with the mobile app package name and signature

4. Use the scrollview label for automatic Scroll display

 

The key code is as follows:

Information about the Trojan Horse virus database:

<? XML version = "1.0" encoding = "UTF-8"?> <List> <virus> <Name> rule. virus </Name> <packname> CN. itcast. virus </packname> <description> malware, reads user logs </description> <signature> logs </Signature> </virus> </List>

 

Anti-Virus engine:

/** Anti-Virus engine (download the virus database, obtain the package name and signature of the program, and perform matching) * (non-javadoc) * @ see android. app. activity # ontouchevent (Android. view. motionevent) */@ overridepublic Boolean ontouchevent (motionevent event) {packagenames = new arraylist <string> (); virusresult = new arraylist <string> (); infos = new arraylist <applicationinfo> (); animationdrawable. start (); // play the scan virus animation new thread () {@ overridepublic void run () {try {URL url = new URL ("http: // 192.168.1.168: 8080/virus. XML "); httpurlconnection conn = (httpurlconnection) URL. openconnection (); inputstream is = Conn. getinputstream (); // parses the virus database from the server and obtains the set virusbeans = virusinfo of the virus database. getvirusinfos (is); taskinfo = new taskinfo (killvirusactivity. this); // instantiate the package Resource Manager // obtain all package names in the current mobile phone. Infos = PM. getinstalledapplications (0); For (applicationinfo info: Infos) {packagenames. add (info. packagename) ;}int COUNT = 0; // The Anti-Virus engine performs the anti-virus stringbuilder sb = new stringbuilder () based on the package name signature in the current system based on the virus database; 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 whether the current packname and corresponding signature are the same as those in the virus database for (virusbean: virusbeans) {If (packname. equals (virusbean. getpackname () & taskinfo. getappsignature (packname ). equals (virusbean. getsignature () {virusresult. add (packname); // Add a virus} count ++; // record 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 () {@ overridepublic 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); // The break is automatically moved down each time the image is added; Case scanning_finish: int I = (integer) MSG. OBJ; stringbuilder SB1 = new stringbuilder (); sb1.append ("Scan completed" + I + "programs"); If (virusresult. size ()> 0) {sb1.append ("detected virus \ n"); For (string packname: virusresult) {sb1.append ("virus name" + packname ); sb1.append ("\ n") ;}} TV _killvirus_info.settext (sb1.tostring (); animationdrawable. stop (); break ;}}};

 

Obtain the program signature:

/** Obtain the program signature */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 ;}}

 

Display the scanned file page 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>

 

 

Related Article

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.