android监听自身被卸载的方法
Android上能不能实现卸载时提示呢,比如卸载某某软件时,做个用户调查卸载的原因。
我以前想着是的不行的,以前的想法是:
Windows上卸载时能实现此功能是因为有些程序的卸载是自己实现的,非系统操作。
但android上目前来说还不支持,系统卸载时,还没发现有啥接口可以和目标卸载程序交互。
呵呵,今天鼓捣LogCat,发现还是可以的。
实现基础是:
1.通过注册权限,能够获取LogCat的输出流的输出信息。
2.进入系统的卸载界面时,"打包安装程序(com.android.packageinstaller)"会输出如下信息
01-22 16:29:15.250: INFO/ActivityManager(147): Starting activity: Intent { act=android.intent.action.DELETE dat=package:lab.sodino.uninstallhint cmp=com.android.packageinstaller/.UninstallerActivity }
好了,有这句话就足够了。截取输出流信息,当获取字符串中包含"android.intent.action.DELETE"和"<you_package>"时,就启动卸载提示页面。
话就这么多了。接下来看效果图,上代码。
package lab.sodino.uninstallhint;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
/**
* @author Sodino E-mail:sodinoopen@hotmail.com
* @version Time:2011-1-12 上午10:09:59
*/
public class MainActivity extends Activity implements LogcatObserver {
private TextView txtInfo;
private Handler handler;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button btnScannerLogcat = (Button) findViewById(R.id.btnScanLogcat);
btnScannerLogcat.setOnClickListener(new Button.OnClickListener() {
public void onClick(View view) {
// 开启Logcat流监听
LogcatScanner.startScanLogcatInfo(MainActivity.this);
}
});
Button btnUninstallMe = (Button) findViewById(R.id.btnUninstallMe);
btnUninstallMe.setOnClickListener(new Button.OnClickListener() {
public void onClick(View view) {
// 调用应用程序信息
Intent intent = new Intent(Intent.ACTION_VIEW);
// com.android.settings/.InstalledAppDetails
intent.setClassName("com.android.settings", "com.android.settings.InstalledAppDetails");
intent.putExtra("pkg", "lab.sodino.uninstallhint");
startActivity(intent);
}
});
txtInfo = (TextView) findViewById(R.id.txtInfo);
handler = new Handler() {
public void handleMessage(Message msg) {
txtInfo.append(String.valueOf(msg.obj) + "\n");
}
};
}
public void handleNewLine(String info) {
Message msg = new Message();
msg.obj = info;
handler.sendMessage(msg);
if (info.contains("android.intent.action.DELETE") && info.contains(getPackageName())) {
// 启动删除提示
Intent intent = new Intent();
intent.setClass(this, UninstallWarningActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
}
}
文件二:
package lab.sodino.uninstallhint;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;
/**
*@author Sodino Email:sodinoopen@hotmail<br/>
*@version 2011-1-22 上午11:10:56
*/
public class LogcatScanner {
private static AndroidLogcatScanner scannerThead;
public final static void startScanLogcatInfo(LogcatObserver observer) {
if (scannerThead == null) {
scannerThead = new AndroidLogcatScanner(observer);
scannerThead.start();
LogOut.out(LogcatScanner.class, "scannerThread.start()");
}
}
static class AndroidLogcatScanner extends Thread {
private LogcatObserver observer;
public AndroidLogcatScanner(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.handleNewLine(line);
int waitValue;
waitValue = runtime.exec(cmds).waitFor();
observer.handleNewLine("waitValue=" + waitValue + "\n Has do Clear logcat cache.");
process = runtime.exec(shellCmd);
is = process.getInputStream();
dis = new DataInputStream(is);
while ((line = dis.readLine()) != null) {
observer.handleNewLine(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();
}
}
}
}
}
文件三:
package lab.sodino.uninstallhint;
/**
* @author Sodino E-mail:sodinoopen@hotmail.com
* @version Time:2011-1-22 下午03:00:54
*/
public interface LogcatObserver {
public void handleNewLine(String line);
}
文件四:
<uses-permission android:name="android.permission.READ_LOGS" />
<uses-permission android:name="android.permission.KILL_BACKGROUND_PROCESSES"/>
<uses-permission android:name="android.permission.RESTART_PACKAGES"/>
android监听自身被卸载的方法相关推荐
- Android 监听应用的卸载与安装
很多时候,我们都需要监听应用的卸载和安装并做相应的事件,比如:应用卸载后,删除安装包.清除垃圾.弹出网页:应用安装后,刷新某些事件,等等,都是非常有用的. 监听的步骤很简单,如下: 1. 新建监听类: ...
- Android监听手机短信
Android监听手机短信的方法有两种,分别为: 1.接受系统的短信广播:当手机收到新消息时,会发送一条广播,通过该广播就可以获取短信内容: 2.监听短信数据库:利用观察者模式监听短信数据库,当短信数 ...
- Android Studio 基础 之 获取蓝牙Bluetooth 的状态,设置的蓝牙Bluetooth 的开关状态,并监听蓝牙Bluetooth 的状态变化方法整理
Android Studio 基础 之 获取蓝牙Bluetooth 的状态,设置的蓝牙Bluetooth 的开关状态,并监听蓝牙Bluetooth 的状态变化方法整理 目录 Android Studi ...
- android 监听来电去电,Android监听来电和去电的实现方法
本文实例讲述了Android监听来电和去电的实现方法.分享给大家供大家参考,具体如下: 要监听android打电话和接电话,只需下面2步骤 第一步,写一个Receiver继承自BroadcastRec ...
- Android 关于ListView中按钮监听的优化问题(方法二)
关于ListView中按钮监听的优化问题(方法一)地址: http://www.cnblogs.com/steffen/p/3951901.html 之前的方法一,虽然能够解决position的传递, ...
- Android监听button按钮的click事件
方式一: 提供所有按钮监听事件入口,在OnClick方法中通过View.getId()获取到发生点击事件的按钮Id,再进行相应的处理, 在页面有多个按钮的情况下比较适用. package com.ex ...
- android 监听屏幕是否锁屏
今天,简单讲讲如何监听手机屏幕是否锁屏. 实现方法:1)通过BroadcastReceiver接收广播Intent.ACTION_SCREEN_ON和Intent.ACTION_SCREEN_OFF可 ...
- android 按键消息,Android监听Home按键消息
Android对屏幕下方常用的四个按键消息处理是不一致的: 搜索按键的消息在onKeyDown或者onKeyUp中接收: 菜单按键的消息在onCreateOptionsMenu.onKeyDown或o ...
- Android监听返回键、Home键+再按一次返回键退出应用
Android监听返回键需重写onKeyDown()方法 Home键keyCode==KeyEvent.KEYCODE_HOME [java] view plaincopy @Override pub ...
最新文章
- boost::random模块实现允许直观地检查分布函数的结果的测试程序
- 前端系统化学习【JS篇】:(二)Javascript、变量和值的简述
- UVALive5461 UVA615 POJ1308 Is It A Tree?(解法二)【废除!!!】
- android之Fragment(官网资料翻译)
- #【软件stm32cubeIDE下配置STM32F407uart1调试SBUS模块-学习笔记-基础样例-遥控小车与四轴模板】
- CompoundButton 源码分析
- 领域平均滤波 matlab,平均值滤波器
- MATLAB Simulink
- MathWorks 中国
- mysql自学完整_MySQL自学篇(三)
- ndows优化大师 免费版,Windows7优化大师
- stm32 NVIC中断管理实现[直接操作寄存器]
- 抽象类是不是必须要有抽象方法
- Java对日期时间的一些基本操作
- 视频教程-微信小程序商城15天从零实战课程-微信开发
- SLAB SLUB
- 奇数点偶数点fft的matlab,奇偶链表(奇数节点位于偶数节点之前) Odd Even Linked List...
- led灯亮度渐变实现
- 云服务器如何安装docker?
- ps磨皮滤镜portraiture安装教程mac
- python下载图片代码_使用Python下载Bing图片(代码)
- java 文字转语音_java 实现文字转语音 -仅限windows系统