android监听自身被卸载的方法
来源:互联网 发布:模拟炒股软件下载 编辑:程序博客网 时间:2024/06/06 03:29
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监听自身的程序被卸载
- Android监听自身的程序被卸载-LOG监视
- android监听应用自身被卸载
- Android监听自身被卸载与反馈
- Android监听程序自身被卸载
- android 如何监听自身应用被卸载
- android监听应用自身被卸载
- android 如何监听自身应用被卸载
- 监听自身APP被卸载
- Android研究之监听自身应用被卸载代码实现
- Android监听自身被卸载与监听其他应用被卸载、安装
- Android App监听自身卸载,反馈统计
- Android App 监听自身卸载,反馈统计
- Android_监听自身应用被卸载
- Android_监听自身应用被卸载
- Android监听自身卸载,弹出用户反馈调查
- android 监听程序的监听和卸载
- 有效使用Python来完成数论作业之同余式
- mastudio 下载
- 故事再美,还是会有结局:物是人非的空间日志
- while语句构成循环
- Dan计划:重新定义人生的10000个小时
- android监听自身被卸载的方法
- 这样学英语三个月超过你过去学三年
- 游戏界面设计的10项黄金准则
- 有效使用Python完成信息论作业
- 模拟Jquery addClass() removeClass() 并应用于隔行变色 高亮当前行
- iOS Block学习
- Linux根文件系统的制作
- 我知道,没有人会在意我的一切
- Win+R 运行命令归纳