android实现卸载提示

文章原地址:http://blog.csdn.net/xyz_lmn/article/details/8330710

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

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

监控代码

  1. public class AndroidLogcatScannerThread extends Thread { 
  2.     private LogcatObserver observer; 
  3.     public AndroidLogcatScannerThread(LogcatObserver observer) { 
  4.             this.observer = observer; 
  5.     } 
  6.  
  7.     public void run() { 
  8.             String[] cmds = { "logcat", "-c" }; 
  9.             String shellCmd = "logcat"
  10.             Process process = null
  11.             InputStream is = null
  12.             DataInputStream dis = null
  13.             String line = ""
  14.             Runtime runtime = Runtime.getRuntime(); 
  15.             try
  16.                     observer.handleLog(line); 
  17.                     int waitValue; 
  18.                     waitValue = runtime.exec(cmds).waitFor(); 
  19.                     observer.handleLog("waitValue=" + waitValue + "\n Has do Clear logcat cache."); 
  20.                     process = runtime.exec(shellCmd); 
  21.                     is = process.getInputStream(); 
  22.                     dis = new DataInputStream(is); 
  23.                     while ((line = dis.readLine()) != null) { 
  24.                         //Log.d("Log","Log.Bestpay:"+line); 
  25.                          
  26.                         if(observer!=null
  27.                             observer.handleLog(line);   
  28.                              
  29.                     } 
  30.             } catch (InterruptedException e) { 
  31.                     e.printStackTrace(); 
  32.             } catch (IOException ie) { 
  33.                     ie.printStackTrace(); 
  34.             } finally
  35.                     try
  36.                             if (dis != null) { 
  37.                                     dis.close(); 
  38.                             } 
  39.                             if (is != null) { 
  40.                                     is.close(); 
  41.                             } 
  42.                             if (process != null) { 
  43.                                     process.destroy(); 
  44.                             } 
  45.                     } catch (Exception e) { 
  46.                             e.printStackTrace(); 
  47.                     } 
  48.             } 
  49.     } 
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();
                    }
            }
    }
}


 

监控服务:

  1. public class AndroidLogcatScannerService extends Service implements LogcatObserver{ 
  2.  
  3.     @Override 
  4.     public void onCreate() { 
  5.         // TODO Auto-generated method stub 
  6.         super.onCreate(); 
  7.     } 
  8.  
  9.     @Override 
  10.     public void onDestroy() { 
  11.         // TODO Auto-generated method stub 
  12.         super.onDestroy(); 
  13.     } 
  14.  
  15.     @Override 
  16.     public void onStart(Intent intent, int startId) { 
  17.         // TODO Auto-generated method stub 
  18.         super.onStart(intent, startId); 
  19.          
  20.         AndroidLogcatScannerThread scannerThread=new AndroidLogcatScannerThread(AndroidLogcatScannerService.this); 
  21.         scannerThread.start(); 
  22.     } 
  23.  
  24.     @Override 
  25.     public IBinder onBind(Intent intent) { 
  26.         // TODO Auto-generated method stub 
  27.         return null
  28.     } 
  29.  
  30.     @Override 
  31.     public void handleLog(String info) { 
  32.         // TODO Auto-generated method stub 
  33.         if (info.contains("android.intent.action.DELETE") && info.contains(getPackageName())) { 
  34.  
  35.                 Intent intent = new Intent(); 
  36.                 intent.setClass(AndroidLogcatScannerService.this, UninstallActivity.class); 
  37.                 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
  38.                 startActivity(intent); 
  39.         } 
  40.     } 
  41.  
public class AndroidLogcatScannerService extends Service implements LogcatObserver{

	@Override
	public void onCreate() {
		// TODO Auto-generated method stub
		super.onCreate();
	}

	@Override
	public void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
	}

	@Override
	public void onStart(Intent intent, int startId) {
		// TODO Auto-generated method stub
		super.onStart(intent, startId);
		
		AndroidLogcatScannerThread scannerThread=new AndroidLogcatScannerThread(AndroidLogcatScannerService.this);
		scannerThread.start();
	}

	@Override
	public IBinder onBind(Intent intent) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void handleLog(String info) {
		// TODO Auto-generated method stub
		if (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);
        }
	}

}


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

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


代码下载地址:

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

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值