严正声明
本人本着技术开放,思想分享的目的,撰写本文。文章仅供参考之用,请勿使之于非法或有害于社会和谐之用。
Sodino
2011-01-24
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);
- }
- package lab.sodino.uninstallhint;
- import android.app.Activity;
- import android.app.ActivityManager;
- import android.content.Context;
- import android.content.Intent;
- import android.os.Bundle;
- import android.view.View;
- import android.widget.Button;
- /**
- * @author Sodino E-mail:sodinoopen@hotmail.com
- * @version Time:2011-1-12 上午10:26:09
- */
- public class UninstallWarningActivity extends Activity {
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.uninstall_warning);
- Button btnContinue = (Button) findViewById(R.id.btnContinue);
- btnContinue.setOnClickListener(new Button.OnClickListener() {
- public void onClick(View view) {
- UninstallWarningActivity.this.finish();
- }
- });
- Button btnCancel = (Button) findViewById(R.id.btnCancel);
- btnCancel.setOnClickListener(new Button.OnClickListener() {
- public void onClick(View view) {
- UninstallWarningActivity.this.finish();
- ActivityManager actMag = (ActivityManager) UninstallWarningActivity.this
- .getSystemService(Context.ACTIVITY_SERVICE);
- //杀掉系统的打包安装程序。
- if (android.os.Build.VERSION.SDK_INT < 8) {
- actMag.restartPackage("com.android.packageinstaller");
- } else {
- actMag.killBackgroundProcesses("com.android.packageinstaller");
- }
- Intent i = new Intent(Intent.ACTION_MAIN);
- i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
- i.addCategory(Intent.CATEGORY_HOME);
- startActivity(i);
- }
- });
- }
- }
最后在AndroidManifest.xml中添加上权限。
- <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"/>