这篇文章是整理的以前用过的一个功能,由于多种原因现在停用了,也希望这篇文章能帮助一些android入门的小童鞋。android是不提供监控卸载自己的功能的,这里使用了监控android日志的功能,android日志相关知识可以参考adb logcat 查看日志这篇文章。

android卸载提示的思路是启动一个服务监控android系统的打印日志,当监控到"android.intent.action.DELETE"并且包含自己应用的包名时,提示给用户。

监控代码

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);        }}}

上面的代码基本实现了卸载提示,最后不要忘了权限:

<uses-permission android:name="android.permission.READ_LOGS"></uses-permission>

代码下载地址:

http://download.csdn.net/detail/xyz_lmn/4904797


/**
* @author 张兴业
* 邮箱:xy-zhang#163.com
* android开发进阶群:278401545
*http://blog.csdn.net/xyz_lmn
*/

更多相关文章

  1. Android日志系统分析之日志设备驱动程序代码阅读
  2. Android(安卓)NDK日志调试
  3. Android日志系统分析之日志设备驱动程序代码阅读
  4. 【Android】 Android中Log调试详解
  5. 监听Android应用程序是否卸载成功
  6. Logger详解(一)
  7. 监听Android应用程序是否卸载成功
  8. Android静默安装相关
  9. .Net 转战 Android(安卓)4.4 日常笔记(6)--Android(安卓)Studio DD

随机推荐

  1. 如何在 Mac 上的“照片”中将色阶调整应
  2. 灵魂拷问:为什么做自动化测试的效率总是难
  3. Linux运维入门教程05-01 (正文处理命令及
  4. Hystrix 实战经验分享
  5. 如何使用iMovie对抖动视频进行防抖处理?
  6. centos6.8 update centos7.2
  7. centos6 无法使用yum命令
  8. xxl-job 搭建使用随笔
  9. 浅析liunx负载均衡(apache篇)
  10. flex容器属性的功能,参数,以及作用