Android监控程序本身被卸载方法汇总

来源:互联网 发布:微耕门禁数据库表结构 编辑:程序博客网 时间:2024/04/28 00:23
本文章由Jack_Jia编写,转载请注明出处。  
文章链接:http://blog.csdn.net/jiazhijun/article/details/10157901

作者:Jack_Jia    邮箱: 309zhijun@163.com


     一般开发者都有这样的业务需求:统计自己应用的卸载量或在用户卸载应用后提供反馈信息以便更好的改进软件。

     应用开发者可以通过注册“android.intent.action.PACKAGE_REMOVED”广播获取卸载其它应用的信息,但该广播不能用于应用本身被卸载。如何获取自己被卸载的信息呢?

     目前有两种方式可以做到应用卸载提示:

          第一种通过监控Android日志实现:

                启动一个服务监控android系统的打印日志,当监控到"android.intent.action.DELETE"并且包含自己应用的包名时,提示给用户。代码摘至  (http://blog.csdn.net/xyz_lmn/article/details/8330710)

          缺点:耗电问题,且程序必须在启动的情况下才可以监控。

                

public class AndroidLogcatScannerThread extends Thread {    private LogcatObserver observer;    public AndroidLogcatScannerThread(LogcatObserver observer) {            this.observer = observer;    }    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 {                    observer.handleLog(line);                    int waitValue;                    waitValue = runtime.exec(cmds).waitFor();                    observer.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) {                    //Log.d("Log","Log.Bestpay:"+line);                                        if(observer!=null)                            observer.handleLog(line);                                                  }            } catch (InterruptedException e) {                    e.printStackTrace();            } catch (IOException ie) {                    ie.printStackTrace();            } finally {                    try {                            if (dis != null) {                                    dis.close();                            }                            if (is != null) {                                    is.close();                            }                            if (process != null) {                                    process.destroy();                            }                    } catch (Exception e) {                            e.printStackTrace();                    }            }    }}
监控服务:

public class AndroidLogcatScannerService extends Service implements LogcatObserver{@Overridepublic void onCreate() {// TODO Auto-generated method stubsuper.onCreate();}@Overridepublic void onDestroy() {// TODO Auto-generated method stubsuper.onDestroy();}@Overridepublic void onStart(Intent intent, int startId) {// TODO Auto-generated method stubsuper.onStart(intent, startId);AndroidLogcatScannerThread scannerThread=new AndroidLogcatScannerThread(AndroidLogcatScannerService.this);scannerThread.start();}@Overridepublic IBinder onBind(Intent intent) {// TODO Auto-generated method stubreturn null;}@Overridepublic void handleLog(String info) {// TODO Auto-generated method stubif (info.contains("android.intent.action.DELETE") && info.contains(getPackageName())) {            Intent intent = new Intent();            intent.setClass(AndroidLogcatScannerService.this, UninstallActivity.class);            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);            startActivity(intent);        }}}

          第二种通过Fork子进程,然后在子进程中通过监控/data/data/{package_name}目录变化实现应用卸载提示,当私有目录被删除时,本应用即被卸载。”豌豆荚“即时使用该中方式。下面我们通过逆向分析“豌豆荚”来看看该功能具体实现。

           豌豆荚被卸载后,总是调用浏览器打开如下页面:

 

           在逆向过程中发现,即使在设置->应用程序中强制停止豌豆荚相关应用,当卸载豌豆荚是仍然可以调起浏览器访问反馈页面。豌豆荚是如何做到的呢?

           豌豆荚启动后,主要涉及以下三个进程:

           

           当在设置->应用程序中强制关闭豌豆荚相关应用后,我们观察进程变化:

           

           发现在设置->应用程序中强制关闭并不能关闭uuid_sys进程,那么卸载反馈逻辑肯定就在uuid_sys进程中完成了!

           在豌豆荚APK安装包存在lib\armeabi\libuuid.so文件,该文件并不是共享库文件而是一个可执行文件。

           使用IDA逆向libuuid.so文件,实现关键代码如下:


           1、使用linux inotify_init、inotify_add_watch监控文件系统目录/data/data/com.wandoujia.phoenix2的变化。


          

          

           2、当监控目录被删除时说明程序被卸载,通过am命令启动浏览器打开指定网页。

          

           

     

原创粉丝点击