android监听应用自身被卸载

jopen 10年前

在实际开发中,常常需要监听应用本身是否被卸载或相近的需求。在网上淘了很久都没有看到实际的做法,最多就给出一个思路,可以通过捕捉系统日志来检测到这个应用是否被卸载,继而做相关的操作。

通过监听Intent.ACTION_PACKAGE_REMOVED意图只能监听到其他应用程序是否被卸载,无法监听自身!

        本例子也是通过监听系统日志,来监听应用本身是否被卸载。

LogcatObserver.java

    public interface LogcatObserver {                        public void handleLog(String info);        }  
</div> </div>

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;            }                }  
</div> </div>
        调用时只需startService(this,LogcatScannerService.class)既可。
        例子本身,经过测试确定可以监听到应用本身被卸载的动作,但是在handleLog()方法中我能只能做些省时的操作,撸主测试可以发出短信,但是无法完成http的发送!例子本身是通过不断的读日志来监听应用本身是否被卸载,因而是非常好电和性能的