一个应用被用户卸载肯定是有理由的,而开发者却未必能得知这一重要的理由,毕竟用户很少会主动反馈建议,多半就是用得不爽就卸,如果能在被卸载后获取到用户的一些反馈,那对开发者进一步改进应用是非常有利的。目前据我所知,国内的Android应用中实现这一功能的只有360手机卫士、360平板卫士,那么如何实现这一功能的?

我们可以把实现卸载反馈的问题转化为监听自己是否被卸载,只有得知自己被卸载,才可以设计相应的反馈处理流程。以下的列表是我在研究这一问题的思路:

1、注册BroadcastReceiver,监听"android.intent.action.PACKAGE_REMOVED"系统广播
结果:NO。未写代码,直接分析,卸载的第一步就是退出当前应用的主进程,而此广播是在已经卸载完成后才发出的,此时主进程都没有了,去哪onReceive()呢?

2、若能收到"将要卸载XX包"的系统广播,在主进程被退出之前就抢先进行反馈处理就好了,可惜没有这样的系统广播,不过经过调研,倒是发现了一个办法,读取系统log,当日志中包含"android.intent.action.DELETE"和自己的包名时,意味着自己将要被卸载。
结果:NO。调试时发现此方法有两个缺陷,(1)点击设置中的卸载按钮即发出此Intent,此时用户尚未在弹框中确认卸载;(2)pm命令卸载不出发此Intent,意味着被诸如手机安全管家,豌豆荚等软件卸载时,无法提前得知卸载意图。

3、由于时间点不容易把控,所以干脆不依赖系统广播或log,考虑到卸载过程会删除"/data/data/包名"目录,我们可以用线程直接轮询这个目录是否存在,以此为依据判断自己是否被卸载。
结果:NO。同方法1,主进程退出,相应的线程必定退出,线程还没等到判断目录是否存在就已经被销毁了。

4、改用C端进程轮询"/data/data/包名"目录是否存在
结果:YES。借助Java端进程fork出来的C端进程在应用被卸载后不会被销毁。

解决的方案确定了,下面来看一下代码吧:

  1. /*
  2. * Copyright (C) 2009 The Android Open Source Project
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. *      http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. *
  16. */
  17. #include <jni.h>
  18. #include <stdio.h>
  19. #include <stdlib.h>
  20. #include <string.h>
  21. #include <android/log.h>
  22. #include <unistd.h>
  23. #include <sys/inotify.h>
  24. #include "com_example_uninstalldemos_NativeClass.h"
  25. /* 宏定义begin */
  26. //清0宏
  27. #define MEM_ZERO(pDest, destSize) memset(pDest, 0, destSize)
  28. #define LOG_TAG "onEvent"
  29. //LOG宏定义
  30. #define LOGD(fmt, args...) __android_log_print(ANDROID_LOG_INFO, LOG_TAG, fmt, ##args)
  31. JNIEXPORT jstring JNICALL Java_com_example_uninstalldemos_NativeClass_init(JNIEnv* env, jobject thiz) {
  32. //初始化log
  33. LOGD("init start...");
  34. //fork子进程,以执行轮询任务
  35. pid_t pid = fork();
  36. if (pid < 0) {
  37. //出错log
  38. LOGD("fork failed...");
  39. } else if (pid == 0) {
  40. //子进程注册"/data/data/pym.test.uninstalledobserver"目录监听器
  41. int fileDescriptor = inotify_init();
  42. if (fileDescriptor < 0) {
  43. LOGD("inotify_init failed...");
  44. exit(1);
  45. }
  46. int watchDescriptor;
  47. watchDescriptor = inotify_add_watch(fileDescriptor,"/data/data/com.example.uninstalldemos", IN_DELETE);
  48. LOGD("watchDescriptor=%d",watchDescriptor);
  49. if (watchDescriptor < 0) {
  50. LOGD("inotify_add_watch failed...");
  51. exit(1);
  52. }
  53. //分配缓存,以便读取event,缓存大小=一个struct inotify_event的大小,这样一次处理一个event
  54. void *p_buf = malloc(sizeof(struct inotify_event));
  55. if (p_buf == NULL) {
  56. LOGD("malloc failed...");
  57. exit(1);
  58. }
  59. //开始监听
  60. LOGD("start observer...");
  61. size_t readBytes = read(fileDescriptor, p_buf,sizeof(struct inotify_event));
  62. //read会阻塞进程,走到这里说明收到目录被删除的事件,注销监听器
  63. free(p_buf);
  64. inotify_rm_watch(fileDescriptor, IN_DELETE);
  65. //目录不存在log
  66. LOGD("uninstall");
  67. //执行命令am start -a android.intent.action.VIEW -d http://shouji.360.cn/web/uninstall/uninstall.html
  68. execlp(
  69. "am", "am", "start", "-a", "android.intent.action.VIEW", "-d",
  70. "http://shouji.360.cn/web/uninstall/uninstall.html", (char *)NULL);
  71. //4.2以上的系统由于用户权限管理更严格,需要加上 --user 0
  72. //execlp("am", "am", "start", "--user", "0", "-a",
  73. //"android.intent.action.VIEW", "-d", "https://www.google.com",(char *) NULL);
  74. } else {
  75. //父进程直接退出,使子进程被init进程领养,以避免子进程僵死
  76. }
  77. return (*env)->NewStringUTF(env, "Hello from JNI !");
  78. }

这里面主要是用到了Linux中的inotify,这个相关的内容可以自行百度一下~~

这里有一个很重要的知识,也是解决这个问题的关键所在,就是Linux中父进程死了,但是子进程不会死,而是被init进程领养。所以当我们应用(进程)卸载了,但是我们fork的子进程并不会销毁,所以我们上述的逻辑代码就可以放到这里来做了。(学习了)

Android应用程序代码:

MyActivity.java

  1. package com.example.uninstalldemos;
  2. import android.app.Activity;
  3. import android.content.Intent;
  4. import android.os.Bundle;
  5. import android.util.Log;
  6. public class MyActivity extends Activity {
  7. @Override
  8. public void onCreate(Bundle savedInstanceState) {
  9. super.onCreate(savedInstanceState);
  10. setContentView(R.layout.activity_main);
  11. Intent intent = new Intent(this, SDCardListenSer.class);
  12. startService(intent);
  13. NativeClass nativeObj = new NativeClass();
  14. nativeObj.init();
  15. }
  16. static {
  17. Log.d("onEvent", "load jni lib");
  18. System.loadLibrary("hello-jni");
  19. }
  20. }

SDCardListenSer.java

  1. package com.example.uninstalldemos;
  2. import android.annotation.SuppressLint;
  3. import android.app.Service;
  4. import android.content.Context;
  5. import android.content.Intent;
  6. import android.net.Uri;
  7. import android.os.Environment;
  8. import android.os.FileObserver;
  9. import android.os.IBinder;
  10. import android.util.Log;
  11. import java.io.File;
  12. import java.io.IOException;
  13. public class SDCardListenSer extends Service {
  14. SDCardListener[] listenners;
  15. @SuppressLint("SdCardPath")
  16. @Override
  17. public void onCreate() {
  18. SDCardListener[] listenners = {
  19. new SDCardListener("/data/data/com.example.uninstalldemos", this),
  20. new SDCardListener(Environment.getExternalStorageDirectory() + File.separator + "1.txt", this) };
  21. this.listenners = listenners;
  22. Log.i("onEvent", "=========onCreate============");
  23. for (SDCardListener listener : listenners) {
  24. listener.startWatching();
  25. }
  26. File file = new File(Environment.getExternalStorageDirectory() + File.separator + "1.txt");
  27. Log.i("onEvent", "dddddddddddddddddddddd nCreate============");
  28. if (file.exists())
  29. file.delete();
  30. /*try {
  31. file.createNewFile();
  32. } catch (IOException e) {
  33. e.printStackTrace();
  34. }*/
  35. }
  36. @Override
  37. public void onDestroy() {
  38. for (SDCardListener listener : listenners) {
  39. listener.stopWatching();
  40. }
  41. }
  42. @Override
  43. public IBinder onBind(Intent intent) {
  44. return null;
  45. }
  46. }
  47. class SDCardListener extends FileObserver {
  48. private String mPath;
  49. private final Context mContext;
  50. public SDCardListener(String parentpath, Context ctx) {
  51. super(parentpath);
  52. this.mPath = parentpath;
  53. this.mContext = ctx;
  54. }
  55. @Override
  56. public void onEvent(int event, String path) {
  57. int action = event & FileObserver.ALL_EVENTS;
  58. switch (action) {
  59. case FileObserver.DELETE:
  60. Log.i("onEvent", "delete path: " + mPath + File.separator + path);
  61. //openBrowser();
  62. break;
  63. case FileObserver.MODIFY:
  64. Log.i("onEvent", "更改目录" + mPath + File.separator + path);
  65. break;
  66. case FileObserver.CREATE:
  67. Log.i("onEvent", "创建文件" + mPath + File.separator + path);
  68. break;
  69. default:
  70. break;
  71. }
  72. }
  73. protected void openBrowser() {
  74. Uri uri = Uri.parse("http://aoi.androidesk.com");
  75. Intent intent = new Intent(Intent.ACTION_VIEW, uri);
  76. mContext.startActivity(intent);
  77. }
  78. public void exeShell(String cmd) {
  79. try {
  80. Runtime.getRuntime().exec(cmd);
  81. } catch (Throwable t) {
  82. t.printStackTrace();
  83. }
  84. }
  85. }

开启一个服务,在这个服务中我们可以看到,用到了一个很重要的一个类FileObserver,也是用来监听文件的变更的,这个和上面的inotify功能差不多。关于这个类的具体用法和介绍,可以自行百度呀~~

运行:

我们将应用安装之后,打开log进行检测日志:

adb logcat -s onEvent

当我们从设置中卸载应用的时候,会弹出如下界面:

注:这里我特定说了是从设置界面中去卸载应用,因为当我使用小米手机自带的那种快捷卸载应用的时候并不会跳转。这个具体的原因还有待解决(当然360的这个问题也没有解决掉。。)

总结:

我写这篇文章的目的以及我从这个过程中唯一学习到的一个知识点就是当父进程消亡了,子进程并不会消亡,所以我们可以记住这个知识点,以后遇到像应用被卸载之后的一些逻辑操作都可以采用这种方式去解决。

JNI的Demo下载

Android中的Demo下载

Android卸载程序之后跳转到指定的反馈页面的更多相关文章

  1. android 卸载程序、清除数据、停止服务用法

    要实现卸载程序.清除数据.停止正在执行的服务这几大模块,如今将代码粗略总结例如以下: 主要运用到的类有 PackageManager ActivityManager ApplicationInfo R ...

  2. Android Webview 调用JS跳转到指定activity

    JAVA: WebView wv; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(save ...

  3. cakephp跳转到指定的错误页面

    第一步:修改core.php 第二步:创建AppExceptionRender.php文件 参考:https://blog.jordanhopfner.com/2012/09/11/custom-40 ...

  4. Android原生PDF功能实现:PDF阅读、PDF页面跳转、PDF手势伸缩、PDF目录树、PDF预览缩略图

    1.背景 近期,公司希望实现安卓原生端的PDF功能,要求:高效.实用. 经过两天的调研.编码,实现了一个简单Demo,如上图所示. 关于安卓原生端的PDF功能实现,技术点还是很多的,为了咱们安卓开发的 ...

  5. html5手机返回按钮跳转到指定页面问题

    最近在做活动的时候有一个这样的场景,在主页面点击跳出一个弹层表单,填写完信息后,点击确认跳转到指定的展示页面了.这时候在手机端点击浏览器自带的返回按钮后,回到主页面,这时候主页面无法刷新,弹层信息还在 ...

  6. [Android]APK程序卸载二次确认的实现

    严正声明        本人本着技术开放,思想分享的目的,撰写本文.文章仅供参考之用,请勿使之于非法或有害于社会和谐之用. Sodino 2011-01-24 Android上能不能实现卸载时提示呢, ...

  7. android如何添加桌面图标和卸载程序后自动删除图标

    android如何添加桌面图标和卸载程序后自动删除桌面图标,这是一个应用的安装与卸载过程对桌面图标的操作,下面与大家分享下具体是如何实现的,感兴趣的朋友可以参考下哈 1:创建图标如下 Intent i ...

  8. ASP.NET MVC默认配置如有跳转到指定的Area区域中的对应程序中

    今天在搭建一个基于MVC的项目,因为项目涉及到了手机和pc端,为了方便和减少二者之间的耦合我在区域(Areas)中建立了两个 程序空间,那么问题来了我想让程序默认跳转到我所指定的areas中的对应项目 ...

  9. Android预安装可卸载程序

    /***************************************************************************** * Android预安装可卸载程序 * 说 ...

随机推荐

  1. windows查看端口占用以及关闭相应的进程

    开始--运行--cmd 进入命令提示符 输入netstat -ano 即可看到所有连接的PID 之后在任务管理器中找到这个PID所对应的程序如果任务管理器中没有PID这一项,可以在任务管理器中选&qu ...

  2. l段子

    段子简介 L段子起源.L段子是开发者根据市场调查, 用户可在等候列车,飞机,或在公交车上无 聊之余使用本应用.打发无聊的时间,分为段 子,图片,活动和个人中心,用户可根据自己 喜好进入不同的区域.段子 ...

  3. Python之路【第十八章】:Django基础

    Django基本配置 Python的WEB框架有Django.Tornado.Flask 等多种,Django相较与其他WEB框架其优势为:大而全,框架本身集成了ORM.模型绑定.模板引擎.缓存.Se ...

  4. Win8电脑蓝屏并提示dpc_watchdog_violation

    用尽系统自带的工具均无法恢复,F8能进系统.后来使用如下方法解决了 这种错误情况的发生可能是由于 iastor.sys 驱动没有完全兼容 Windows 8系统所造成的. 微软正在研究一种可行方案,来 ...

  5. itertools模块

    itertools模块中有很多函数,返回的是一个迭代器 参考: http://www.wklken.me/posts/2013/08/20/python-extra-itertools.html#_1

  6. RabbitMQ系列之Centos 7安装RabbitMQ 3.6.1

    1.安装EPEL-7: rpm -ivh http://dl.fedoraproject.org/pub/epel/7/x86_64/e/epel-release-7-5.noarch.rpm 2.安 ...

  7. CURL in windows

    目前版本为: 7.50.3  >> 不同操作系统及版本的下载页面 https://curl.haxx.se/download/?C=M;O=D Windows上的下载入口及地址为: htt ...

  8. linux查看进程启动时间

    1. ps axu 2. 精确查看 for pid in $(pgrep httpd); do echo -n "${pid} " ; ps -p ${pid} -o lstart ...

  9. winserver2008 management note

    1,磁盘online及介质保护 Windows server 2008 增加的磁盘无法初始化-提示:介质受写入保护.插了下相关说明,在VMware的帖子找到了解决办法: 开始-运行,cmd.打开命令提 ...

  10. LoadRunner连接Genymotion