SoFunction
Updated on 2025-04-09

Android programming antivirus implementation principle and specific examples

This article describes the implementation principle of Android antivirus. Share it for your reference, as follows:

The most core part of an anti-virus soft armor is the virus library and the anti-virus engine. The virus library is obtained from the server. The anti-virus engine actually determines whether the package name and signature in the program match the package name and signature in the virus library. If it matches, it is a virus. The interface uses frame animation to display it.

Ideas:

1. Download the virus version library information from the server and store the parsed data in the List collection.
2. Obtain the package names and program signatures of all applications in the mobile phone
3. Match the virus library to the mobile application package name and signature
4. Use ScrollView tag for automatic scrolling display

The key code is as follows:

Information about the * Horse Virus Library:

<?xml version="1.0" encoding="utf-8"?>
<list>
<virus>
 <name></name>
 <packname>

</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 library, obtain the package name and signature of the program and match it)
  * (non-Javadoc)
  * @see #onTouchEvent()
  */
@Override
public boolean onTouchEvent(MotionEvent event) {
 packagenames = new ArrayList<String>();
 virusResult = new ArrayList<String>();
 infos = new ArrayList<ApplicationInfo>();
 ();//Play the animation of scanning viruses new Thread(){
  @Override
  public void run() {
   try {
    URL url = new URL("http://192.168.1.168:8080/");
    HttpURLConnection conn = (HttpURLConnection) ();
    InputStream is = ();
    //Parse the virus database from the server and obtain the collection of the virus database    virusbeans = (is);
    TaskInfo taskInfo = new TaskInfo(); //Instantiated Package Explorer    //Get all the package names in the current mobile phone    infos = (0);
    for(ApplicationInfo info : infos ){
     ();
    }
    int count=0;
    // Antivirus engine Compare the package name and signature in the current system according to the virus database to kill virus    StringBuilder sb = new StringBuilder();
    for(String packname : packagenames){
     ("Scanning"+ packname);
     ("\n");
     Message msg = new Message();
      = SCANNING;
      = sb;
     (msg);
     //Check whether the current packname and corresponding signature are the same as the information in the virus database     for(VirusBean virusbean : virusbeans){
      if((())&&
        (packname).equals(()))
      {
       (packname);//Add a virus      }
     }
     count ++;//Record the total number of viruses    }
    Message msg = new Message();
     = SCANNING_FINISH;
     = count;
    (msg);
   } catch (Exception e) {
    ();
   }
  }
 }.start();
 return (event);
}

Display virus scan information:

Handler handler = new Handler(){
 @Override
 public void handleMessage(Message msg) {
  (msg);
  switch () {
  case SCANNING:
   StringBuilder sb = (StringBuilder) ;
   tv_killvirus_info.setText(());
   (0, 25);//Every time the increase will automatically move the screen downward   break;
  case SCANNING_FINISH:
   int i = (Integer) ;
   StringBuilder sb1 = new StringBuilder();
   ("Scan Completed Scan Total"+ i+ "Programs");
   if(()>0){
    ("Virus Discovery\n");
     for(String packname : virusResult){
      ("Virus Name"+ packname);
      ("\n");
     }
    }
    tv_killvirus_info.setText(());
    ();
   break;
  }
 }
};

Get the program signature:

/*
  * Get the program's signature
  */
public String getAppSignature(String packname){
  try {
   PackageInfo packinfo =(packname, PackageManager.GET_SIGNATURES);
   //Get all permissions   return [0].toCharsString();
  } catch (NameNotFoundException e) {
   ();
   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:
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:
></TextView>
</ScrollView>

I hope this article will be helpful to everyone's Android programming design.