这篇文章是整理的以前用过的一个功能,由于多种原因现在停用了,也希望这篇文章能帮助一些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. password技术应用设计实践-安全信息传输系统(SITS)(用Java实现DES、RSA、MD5算法)

    本系统包含五个模块,注冊模块.登录模块.RSA算法模块.DES算法模块.MD5算法模块. 这五个模块每个实现不同的功能. 注冊模块实现将username和password写入文件里,登录模块则负责将其 ...

  2. hdu5289 Assignment (区间查询最大值最小值,st算法...)

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=5289 题意:给定长度为n的序列a和一个整数K,找出最大值和最小值的差值小于K的区间.输出满足条件的区间的个 ...

  3. Ajax库的编写及使用

    ajax使用在服务器端. ajax.js function ajax(url,fnSucc,fnFail) { //1.创建ajax对象 var oAjax = null; if(window.XML ...

  4. 适用于 Charles任 意版 本的 注 册 码

    // Charles Proxy License // 适用于Charles任意版本的注册码,谁还会想要使用破解版呢. // Charles 4.2目前是最新版,可用. Registered Name ...

  5. react-native + react-native-tab-navigator 实现 TabBar

    1.安装 react-native-tab-navigator yarn add react-native-tab-navigator 2.页面调用 /** * 主页面 */ import React ...

  6. Android短信发送器(2)

    在上一篇的<Android短信发送器>当中.发送功能并不完好.当发送内容超过限定字数时,短信就会发送失败,此时就须要推断内容是否超过限制,假设不超过限制.就直接发送,反之.则对其进行处理再 ...

  7. 经典SQL语句使用方法大全

    一.基础1.说明:创建数据库CREATE DATABASE database-name2.说明:删除数据库drop database dbname3.说明:备份sql server--- 创建 备份数 ...

  8. 了解机器学习框架CoreML

    代码地址如下:http://www.demodashi.com/demo/11972.html CoreML是iOS 11新推出的机器学习框架,是人工智能的核心内容,他可以在训练好的机器学习模型应用到 ...

  9. LINPACK測试

    1简单介绍 LINPACK是线性系统软件包(Linear system package) 的缩写. Linpack如今在国际上已经成为最流行的用于測试高性能计算机系统浮点性能的benchmark.通过 ...

  10. 10、驱动中的阻塞与非阻塞IO

        阻塞,就是在获取资源的时候,不能获取到,那么就会将当前的进程挂起(睡眠,也就是将当前进程从调度器拿走了,不会调度当前进程),直到满足条件为止再进行操作.相反,非阻塞,就是即使不能获取到资源,非 ...