The android listening application is uninstalled.

Source: Internet
Author: User

In actual development, you often need to monitor whether the application is uninstalled or similar. I haven't seen the actual practice for a long time on the Internet. I can give a idea at most. I can capture system logs to check whether the application is uninstalled and then perform related operations.

By listening to the Intent. ACTION_PACKAGE_REMOVED Intent, you can only monitor whether other applications are uninstalled and cannot monitor yourself!

In this example, the system logs are monitored to determine whether the application is uninstalled.

LogcatObserver. java

public interface LogcatObserver {          public void handleLog(String info); } public interface LogcatObserver {       public void handleLog(String info);}LogcatScannerService.java
import java.io.DataInputStream; import java.io.IOException; import java.io.InputStream; import android.app.Service; import android.content.Intent; import android.os.IBinder;  public class LogcatScannerService extends Service implements LogcatObserver {     @Override     public void onStart(Intent intent, int startId) {         super.onStart(intent, startId);         new AndroidLogcatScannerThread(this).start();     }      @Override     public void handleLog(String info) {         if (info.contains("android.intent.action.DELETE")                 && info.contains(getPackageName())) {             //do something yourself          }     }      private class AndroidLogcatScannerThread extends Thread {          private LogcatObserver mObserver;          public AndroidLogcatScannerThread(LogcatObserver observer) {             mObserver = observer;         }          @Override         public void run() {             String[] cmds = { "logcat", "-c" };             String shellCmd = "logcat";             Process process = null;             InputStream is = null;             DataInputStream dis = null;             String line = "";             Runtime runtime = Runtime.getRuntime();             try {                 mObserver.handleLog(line);                 int waitValue;                 waitValue = runtime.exec(cmds).waitFor();                 mObserver.handleLog("waitValue=" + waitValue                         + "\n Has do Clear logcat cache.");                 process = runtime.exec(shellCmd);                 is = process.getInputStream();                 dis = new DataInputStream(is);                 while ((line = dis.readLine()) != null) {                     if (mObserver != null)                         mObserver.handleLog(line);                  }             } catch (InterruptedException e) {                 e.printStackTrace();             } catch (IOException ie) {             } finally {                 try {                     if (dis != null) {                         dis.close();                     }                     if (is != null) {                         is.close();                     }                     if (process != null) {                         process.destroy();                     }                 } catch (Exception e) {                 }             }         }     }          @Override     public IBinder onBind(Intent intent) {         return null;     }  } import java.io.DataInputStream;import java.io.IOException;import java.io.InputStream;import android.app.Service;import android.content.Intent;import android.os.IBinder;public class LogcatScannerService extends Service implements LogcatObserver {    @Override    public void onStart(Intent intent, int startId) {        super.onStart(intent, startId);        new AndroidLogcatScannerThread(this).start();    }    @Override    public void handleLog(String info) {        if (info.contains("android.intent.action.DELETE")                && info.contains(getPackageName())) {            //do something yourself        }    }    private class AndroidLogcatScannerThread extends Thread {        private LogcatObserver mObserver;        public AndroidLogcatScannerThread(LogcatObserver observer) {            mObserver = observer;        }        @Override        public void run() {            String[] cmds = { "logcat", "-c" };            String shellCmd = "logcat";            Process process = null;            InputStream is = null;            DataInputStream dis = null;            String line = "";            Runtime runtime = Runtime.getRuntime();            try {                mObserver.handleLog(line);                int waitValue;                waitValue = runtime.exec(cmds).waitFor();                mObserver.handleLog("waitValue=" + waitValue                        + "\n Has do Clear logcat cache.");                process = runtime.exec(shellCmd);                is = process.getInputStream();                dis = new DataInputStream(is);                while ((line = dis.readLine()) != null) {                    if (mObserver != null)                        mObserver.handleLog(line);                }            } catch (InterruptedException e) {                e.printStackTrace();            } catch (IOException ie) {            } finally {                try {                    if (dis != null) {                        dis.close();                    }                    if (is != null) {                        is.close();                    }                    if (process != null) {                        process.destroy();                    }                } catch (Exception e) {                }            }        }    }       @Override    public IBinder onBind(Intent intent) {        return null;    }}

You only need to call startService (this, LogcatScannerService. class.
The example itself is tested and determined to be able to listen to the action of the application being uninstalled. However, in the handleLog () method, I can only perform some time-saving operations. In the butler main test, I can send text messages, however, http sending cannot be completed!

The example itself monitors whether the application itself is uninstalled by constantly reading logs, so it is very good for electricity and performance! If the majority of users have better ideas, we can discuss them together!

 


 

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.