分类:Android 318人阅读 评论(0) 收藏 举报

MainActivity如下:

[java] view plain copy
  1. packagecc.testremoveapp;
  2. importandroid.os.Bundle;
  3. importandroid.app.Activity;
  4. importandroid.content.Intent;
  5. /**
  6. *Demo描述:
  7. *监听应用程序本身被卸载
  8. *
  9. *注意权限:
  10. *<uses-permissionandroid:name="android.permission.READ_LOGS"></uses-permission>
  11. *
  12. *参考资料:
  13. *http://blog.csdn.net/xyz_lmn/article/details/8330710
  14. *Thankyouverymuch
  15. */
  16. publicclassMainActivityextendsActivity{
  17. @Override
  18. protectedvoidonCreate(BundlesavedInstanceState){
  19. super.onCreate(savedInstanceState);
  20. setContentView(R.layout.main);
  21. init();
  22. }
  23. //启动服务
  24. privatevoidinit(){
  25. Intentintent=newIntent(this,LogcatScannerService.class);
  26. startService(intent);
  27. }
  28. }


LogcatObserverInterface如下:

[java] view plain copy
  1. packagecc.testremoveapp;
  2. //业务接口
  3. publicinterfaceLogcatObserverInterface{
  4. publicvoidhandleLog(StringlogcatInfo);
  5. }


LogcatScannerService如下:

[java] view plain copy
  1. packagecc.testremoveapp;
  2. importandroid.app.Service;
  3. importandroid.content.Intent;
  4. importandroid.os.IBinder;
  5. publicclassLogcatScannerServiceextendsServiceimplementsLogcatObserverInterface{
  6. @Override
  7. publicvoidonCreate(){
  8. super.onCreate();
  9. }
  10. @Override
  11. publicvoidonStart(Intentintent,intstartId){
  12. super.onStart(intent,startId);
  13. LogcatScannerThreadlogcatScannerThread=newLogcatScannerThread(this);
  14. logcatScannerThread.start();
  15. }
  16. @Override
  17. publicIBinderonBind(Intentarg0){
  18. returnnull;
  19. }
  20. @Override
  21. publicvoidonDestroy(){
  22. super.onDestroy();
  23. }
  24. /**
  25. *实现LogcatObserverInterface接口中的方法
  26. */
  27. @Override
  28. publicvoidhandleLog(StringlogcatInfo){
  29. if(logcatInfo.contains("android.intent.action.DELETE")&&logcatInfo.contains(getPackageName())){
  30. /**
  31. *注意事项:
  32. *LogCat有时会多次甚至一直输出卸载应用的信息
  33. *所以在实际项目中需要对此处留意处理
  34. */
  35. Intentintent=newIntent(LogcatScannerService.this,UninstallActivity.class);
  36. intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
  37. startActivity(intent);
  38. }
  39. }
  40. }


LogcatScannerThread如下:

[java] view plain copy
  1. packagecc.testremoveapp;
  2. importjava.io.DataInputStream;
  3. importjava.io.InputStream;
  4. publicclassLogcatScannerThreadextendsThread{
  5. privateLogcatObserverInterfacemLogcatObserverInterface;
  6. publicLogcatScannerThread(LogcatObserverInterfacelogcatObserverInterface){
  7. this.mLogcatObserverInterface=logcatObserverInterface;
  8. }
  9. @Override
  10. publicvoidrun(){
  11. super.run();
  12. intwaitValue;
  13. Stringline="";
  14. String[]cmds={"logcat","-c"};
  15. StringshellCMD="logcat";
  16. Processprocess=null;
  17. InputStreaminputStream=null;
  18. DataInputStreamdataInputStream=null;
  19. Runtimeruntime=Runtime.getRuntime();
  20. try{
  21. mLogcatObserverInterface.handleLog(line);
  22. waitValue=runtime.exec(cmds).waitFor();
  23. mLogcatObserverInterface.handleLog("waitValue="+waitValue+"\nHasdoClearlogcatcache.");
  24. process=runtime.exec(shellCMD);
  25. inputStream=process.getInputStream();
  26. dataInputStream=newDataInputStream(inputStream);
  27. while((line=dataInputStream.readLine())!=null){
  28. if(mLogcatObserverInterface!=null){
  29. mLogcatObserverInterface.handleLog(line);
  30. }
  31. }
  32. }catch(Exceptione){
  33. e.printStackTrace();
  34. }finally{
  35. try{
  36. if(dataInputStream!=null){
  37. dataInputStream.close();
  38. }
  39. if(inputStream!=null){
  40. inputStream.close();
  41. }
  42. if(process!=null){
  43. process.destroy();
  44. }
  45. }catch(Exceptione){
  46. e.printStackTrace();
  47. }
  48. }
  49. }
  50. }


UninstallActivity如下:

[java] view plain copy
  1. packagecc.testremoveapp;
  2. importandroid.app.Activity;
  3. importandroid.os.Bundle;
  4. publicclassUninstallActivityextendsActivity{
  5. @Override
  6. protectedvoidonCreate(BundlesavedInstanceState){
  7. super.onCreate(savedInstanceState);
  8. setContentView(R.layout.uninstall);
  9. }
  10. }


main.xml如下:

[html] view plain copy
  1. <RelativeLayoutxmlns: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. <TextView
  7. android:layout_width="wrap_content"
  8. android:layout_height="wrap_content"
  9. android:text="监听应用本身被卸载"
  10. android:layout_centerInParent="true"
  11. />
  12. </RelativeLayout>


uninstall.xml如下:

[html] view plain copy
  1. <RelativeLayoutxmlns: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. <TextView
  7. android:layout_width="wrap_content"
  8. android:layout_height="wrap_content"
  9. android:text="确定卸载本应用?"
  10. android:layout_centerInParent="true"
  11. />
  12. </RelativeLayout>

更多相关文章

  1. 【Android】简单的接口回调
  2. Android framework学习参考资料
  3. 【APP】微信接口对Android与IOS验证方式的差异
  4. Android 接口回调机制详解
  5. [置顶] Android本地接口JNI的使用分析
  6. Android行驶证离线识别sdk接口

随机推荐

  1. Android单元测试Unable to find instrume
  2. Android之Notification
  3. Android(安卓)adb: The connection to ad
  4. 35、键盘布局的tableLayout备份
  5. Android基础-Android的生命周期
  6. Android判断真机和模拟器
  7. Android进度条总结
  8. Android进度条总结
  9. android 自动更新apk版本
  10. Android使用代码模拟HOME键的功能