现在的位置: 首页 > 综合 > 正文

Android利用Logcat监听应用程序本身被卸载

2017年11月02日 ⁄ 综合 ⁄ 共 5587字 ⁄ 字号 评论关闭
分类: Android 318人阅读 评论(0) 收藏 举报

MainActivity如下:

[java] view
plain
copy

  1. package cc.testremoveapp;  
  2.   
  3. import android.os.Bundle;  
  4. import android.app.Activity;  
  5. import android.content.Intent;  
  6. /** 
  7.  * Demo描述: 
  8.  * 监听应用程序本身被卸载 
  9.  *  
  10.  * 注意权限: 
  11.  * <uses-permission android:name="android.permission.READ_LOGS"></uses-permission> 
  12.  *  
  13.  * 参考资料: 
  14.  * http://blog.csdn.net/xyz_lmn/article/details/8330710 
  15.  * Thank you very much 
  16.  */  
  17. public class MainActivity extends Activity {  
  18.     @Override  
  19.     protected void onCreate(Bundle savedInstanceState) {  
  20.         super.onCreate(savedInstanceState);  
  21.         setContentView(R.layout.main);  
  22.         init();  
  23.     }  
  24.     //启动服务  
  25.     private void init(){  
  26.         Intent intent=new Intent(this, LogcatScannerService.class);  
  27.         startService(intent);  
  28.     }  
  29.       
  30.   
  31. }  

LogcatObserverInterface如下:

[java] view
plain
copy

  1. package cc.testremoveapp;  
  2. //业务接口  
  3. public interface LogcatObserverInterface {  
  4.     public void handleLog(String logcatInfo);  
  5. }  

LogcatScannerService如下:

[java] view
plain
copy

  1. package cc.testremoveapp;  
  2.   
  3. import android.app.Service;  
  4. import android.content.Intent;  
  5. import android.os.IBinder;  
  6.   
  7. public class LogcatScannerService extends Service implements LogcatObserverInterface {  
  8.     @Override  
  9.     public void onCreate() {  
  10.         super.onCreate();  
  11.     }  
  12.       
  13.     @Override  
  14.     public void onStart(Intent intent, int startId) {  
  15.         super.onStart(intent, startId);  
  16.         LogcatScannerThread logcatScannerThread=new LogcatScannerThread(this);  
  17.         logcatScannerThread.start();  
  18.     }  
  19.       
  20.     @Override  
  21.     public IBinder onBind(Intent arg0) {  
  22.         return null;  
  23.     }  
  24.       
  25.     @Override  
  26.     public void onDestroy() {  
  27.         super.onDestroy();  
  28.     }  
  29.       
  30.     /** 
  31.      * 实现LogcatObserverInterface接口中的方法 
  32.      */  
  33.     @Override  
  34.     public void handleLog(String logcatInfo) {  
  35.         if (logcatInfo.contains("android.intent.action.DELETE")&& logcatInfo.contains(getPackageName())) {  
  36.             /** 
  37.              * 注意事项: 
  38.              * LogCat有时会多次甚至一直输出卸载应用的信息 
  39.              * 所以在实际项目中需要对此处留意处理 
  40.              */  
  41.             Intent intent = new Intent(LogcatScannerService.this,UninstallActivity.class);  
  42.             intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);  
  43.             startActivity(intent);  
  44.         }  
  45.     }  
  46.   
  47. }  

LogcatScannerThread如下:

[java] view
plain
copy

  1. package cc.testremoveapp;  
  2. import java.io.DataInputStream;  
  3. import java.io.InputStream;  
  4.   
  5. public class LogcatScannerThread extends Thread {  
  6.       
  7.     private LogcatObserverInterface mLogcatObserverInterface;  
  8.       
  9.     public LogcatScannerThread(LogcatObserverInterface logcatObserverInterface){  
  10.         this.mLogcatObserverInterface=logcatObserverInterface;  
  11.     }  
  12.     @Override  
  13.     public void run() {  
  14.         super.run();  
  15.         int waitValue;  
  16.         String line = "";  
  17.         String[] cmds = { "logcat""-c" };  
  18.         String shellCMD = "logcat";  
  19.         Process process = null;  
  20.         InputStream inputStream = null;  
  21.         DataInputStream dataInputStream = null;  
  22.         Runtime runtime = Runtime.getRuntime();  
  23.         try {  
  24.                 mLogcatObserverInterface.handleLog(line);  
  25.                 waitValue = runtime.exec(cmds).waitFor();  
  26.                 mLogcatObserverInterface.handleLog("waitValue=" + waitValue + "\n Has do Clear logcat cache.");  
  27.                 process = runtime.exec(shellCMD);  
  28.                 inputStream = process.getInputStream();  
  29.                 dataInputStream = new DataInputStream(inputStream);  
  30.                 while ((line = dataInputStream.readLine()) != null) {  
  31.                     if(mLogcatObserverInterface!=null){  
  32.                         mLogcatObserverInterface.handleLog(line);  
  33.                     }  
  34.                 }  
  35.         } catch (Exception e) {  
  36.                 e.printStackTrace();  
  37.         } finally {  
  38.             try {  
  39.                 if (dataInputStream != null) {  
  40.                     dataInputStream.close();  
  41.                 }  
  42.                 if (inputStream != null) {  
  43.                     inputStream.close();  
  44.                 }  
  45.                 if (process != null) {  
  46.                     process.destroy();  
  47.                 }  
  48.             } catch (Exception e) {  
  49.                 e.printStackTrace();  
  50.             }  
  51.         }  
  52.     }  
  53.   
  54. }  

UninstallActivity如下:

[java] view
plain
copy

  1. package cc.testremoveapp;  
  2.   
  3. import android.app.Activity;  
  4. import android.os.Bundle;  
  5.   
  6. public class UninstallActivity extends Activity {  
  7.     @Override  
  8.     protected void onCreate(Bundle savedInstanceState) {  
  9.         super.onCreate(savedInstanceState);  
  10.         setContentView(R.layout.uninstall);  
  11.     }  
  12.       
  13.   
  14. }  

main.xml如下:

[html] view
plain
copy

  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     >  
  6.   
  7.     <TextView  
  8.         android:layout_width="wrap_content"  
  9.         android:layout_height="wrap_content"  
  10.         android:text="监听应用本身被卸载"  
  11.         android:layout_centerInParent="true"  
  12.         />  
  13.   
  14. </RelativeLayout>  

uninstall.xml如下:

[html] view
plain
copy

  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     >  
  6.   
  7.     <TextView  
  8.         android:layout_width="wrap_content"  
  9.         android:layout_height="wrap_content"  
  10.         android:text="确定卸载本应用?"  
  11.         android:layout_centerInParent="true"  
  12.         />  
  13.   
  14. </RelativeLayout>  

抱歉!评论已关闭.