这篇文章是整理的以前用过的一个功能,由于多种原因现在停用了,也希望这篇文章能帮助一些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. Android使用sqlliteOpenhelper更改数据库的存储路径放到SD卡上

    假设使用默认的系统管理,默认放在包以下.比較省心.并且在卸载app后不会造成数据残留.可是这样也有一个问题.比方我做一个背单词的软件,那么当用户卸载掉这个app时,他辛辛苦苦下载的单词库也没了... ...

  2. jquery 中attr和prop的区别

    在jQuery API中也有专门解释: Attributes VS. Properties 在一些特殊的情况下,attributes和properties的区别非常大.在jQuery1.6之前,.at ...

  3. HDU 5360 Hiking(优先队列)2015 Multi-University Training Contest 6

    Hiking Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Total S ...

  4. python——数据结构之单链表的实现

    链表的定义: 链表(linked list)是由一组被称为结点的数据元素组成的数据结构,每个结点都包含结点本身的信息和指向下一个结点的地址.由于每个结点都包含了可以链接起来的地址 信息,所以用一个变量 ...

  5. INSTALL_FAILED_OLDER_SDK

    Uploading file local path: /Users/Rubert/Android/workspace/MyApplication/app/build/outputs/apk/app-d ...

  6. swift算法手记-10

    http://blog.csdn.net/myhaspl private func findnode(val:Int)->Bool{//http://blog.csdn.net/myhaspl ...

  7. lua与c++ 中布尔布bool值对应关系

    lua代码返回值为真c++ lua_toboolean 返回一个 int  lua true = 1 false = 0 c++给lua返回 lua_pushboolean 1 = true 0 = ...

  8. asp.net+mvc+easyui+sqlite 简单用户系统学习之旅(六)—— 简单过滤器的使用

    有些同学喜欢在测试或运行项目时,直接跳转页面到Home/Index下,但本次项目直接输入Home/Index则会报错 因为home/index中有个user.name参数,如果启动项目后直接跳转到ho ...

  9. Docker 方式运行 jenkins

    原文地址:https://testerhome.com/topics/5798 简介说明 docker 是官方推荐的一种 jenkins 启动方式. 打开 jenkins 的官网,点击进入的是: ht ...

  10. AMD 3600+ X2 CPU配合昂达A69T主板超频教程

    本次超频使用电脑硬件配置如下: AMD 3600+ X2 90nm 昂达 A69T HDMI 宇瞻 DDR II 1G 667普条 西数 WD2500KS SATAII  250G 16M 戴尔 普通 ...