这篇文章是整理的以前用过的一个功能,由于多种原因现在停用了,也希望这篇文章能帮助一些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{

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

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

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

代码下载地址:

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

*  iOS入门群:83702688
*  android开发进阶群:241395671
*  我的新浪微博:@张兴业TBOW
*  我的邮箱:xy-zhang#163.com(#->@)
*/

android实现卸载提示的更多相关文章

  1. Android之hint提示字体大小修改,显示完全

    Android之hint提示字体大小修改,显示完全 1.工作中遇到一个问题,就是自定义EditText的hint提示在超大字体下会显示不全, 2.然后在网上搜索了一下,在这里记录一下,分享给大家,在此 ...

  2. android中的提示信息显示方法(toast应用)

    android中的提示信息显示方法(toast应用) (2011-10-17 11:02:06) 转载▼ 标签: android toast 杂谈 分类: Android android中toast的 ...

  3. Android Studio中提示:Project SDK is not defined

    Android Studio中提示:Project SDK is not defined 2015 年 4 月 1 日 下午 9:04crifan已有2209人围观我来说几句 [背景] 之前用Andr ...

  4. 错误异常 (1)Android Studio错误提示:Gradle project sync failed. Basic functionality (eg. editing, debugging) will not work properly

    [已解决]Android Studio错误提示:Gradle project sync failed. Basic functionality (eg. editing, debugging) wil ...

  5. Xamarin Android设置界面提示类型错误

    Xamarin Android设置界面提示类型错误 错误信息:Integer types not allow (at ‘padding’ with value ’10’)Android界面属性的长度和 ...

  6. INNO:检测程序是否已经安装,是则弹出卸载提示。

    INNO:检测程序是否已经安装,是则弹出卸载提示. 作者:少轻狂 | 发布:2010-08-05 | 更新:2013-09-05 | 分类:部署 | Disposition | 热度:2816 ℃ 实 ...

  7. 第13讲- Android之消息提示Notification

    第13讲 Android之消息提示Notification .Notification Notification可以理解为通知的意思一般用来显示广播信息,通知可以显示到系统的上方的状态栏(status ...

  8. 第12讲- Android之消息提示Toast

    第12讲 Android之消息提示Toast .Toast Toast 是一个 View 视图,在应用程序上浮动显示少量的信息给用户,它永远不会获得焦点,不影响用户的输入等操作,主要用于向用户显示一些 ...

  9. Android 模式对话框提示Dialog

    1.先写一个Dialog类   CustomDialog package com.example.heng.adtest; import android.app.AlertDialog; import ...

随机推荐

  1. Spring AOP实现拦截转发控制

    import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import ...

  2. Java 8 List

    排序 依据自定义对象的某个属性进行排序. List<Student> students = Arrays.asList(student1, student2, student3); stu ...

  3. 【BIEE】14_开发流程介绍

    以上是BIEE开发的流程图,通过流程图我们可以看出在BIEE中存在以下主要内容: 仪表盘 仪表盘页 分析 仪表盘提示 主题区域 Catalog RPD 以下是一些文件以及资料库存储路径 资料库存储路径 ...

  4. python基础 实战作业 ---Excel基本读写与数据处理

    代码地址如下:http://www.demodashi.com/demo/11650.html 看完本篇需要: 10min 作业练习需要: 0.5h~3h(依练习者对python熟悉程度而定) 看完本 ...

  5. Python 的数据表示

    一.常量.变量和对象 1.常量:是指在程序的执行过程中不变的量.如:1,2,3,4,……,true.false 也有一些包含在模块中的用符号表示的常量,常用的如math模块中的pi和e,如: > ...

  6. 千万级大数据的Mysql数据库SQL语句优化

    1.对查询进行优化,应尽量避免全表扫描,首先应考虑在 where 及 order by 涉及的列上建立索引. 2.应尽量避免在 where 子句中对字段进行 null 值判断,否则将导致引擎放弃使用索 ...

  7. 20180113Go匿名函数和闭包

    最近codereview看到闭包,得学习下 https://studygolang.com/articles/5057 匿名函数:没有函数名的函数 闭包:外部函数定义的内部函数. 闭包给访问外部函数定 ...

  8. Scikit-learn的kmeans聚类

    1. 生成随机的二维数据: import numpy as np x1 = np.array([1, 2, 3, 1, 5, 6, 5, 5, 6, 7, 8, 9, 9]) x2 = np.arra ...

  9. OSI与TCP/IP你了解多少?

    刚开始接触计算机的时候,非常好奇数据是怎样传输的,通过后期的学习逐步了解了计算机的通信原理.相信很多人在学习计算机的时候,首先入门的第一门课就是计算机的通信原理,而通信原理又离不开两大参考模型:OSI ...

  10. Anaconda+Tensorflow环境安装与配置(转载)

    Anaconda+Tensorflow环境安装与配置 转载请注明出处:http://www.cnblogs.com/willnote/p/6746499.html Anaconda安装 在清华大学 T ...