`
android_madness
  • 浏览: 39432 次
  • 性别: Icon_minigender_1
  • 来自: 福州
社区版块
存档分类
最新评论

[Android]APK程序卸载提示 (转)

阅读更多
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);
                        }
                });
        }
}


        <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"/>
分享到:
评论
1 楼 zhongqiuming 2012-04-27  

相关推荐

    Android APK+Dex文件反编译及回编译工具v2.0.2

    是一款,针对Android OS系统的APK程序,直接反编译修改的工具。 APKDB集合了当今最强悍,最犀利的APK及Dex文件编译工具; 正常安装后,它直接在【鼠标右键】创建快捷菜单; 非常方便汉化工作者,对APK或Dex文件...

    Android APK+Dex文件反编译及回编译工具(APKDB)v.1.9.2 正式版

    是一款,针对Android OS系统的APK程序,直接反编译修改的工具。 APKDB集合了当今最强悍,最犀利的APK及Dex文件编译工具; 正常安装后,它直接在【鼠标右键】创建快捷菜单; 非常方便汉化工作者,对APK或Dex文件...

    apktoolmv2.4.0_downcc.com.apk

    Apktool M v2.4.0 反编译 apk版,可以放到安卓手机,mumu模拟器等,功能强大 ...工具打开然后 1.做上面的操作 解压文件--&gt;全选 就能吧分包转成apks...●卸载apk -已安装的应用程序可以从应用程序列表中删除 (新菜单项)

    安卓手机(android)wifi传送文件源码.zip

    程序分为 android手机端的 apk 和 Pc端的 exe 两个 文件 。 程序会自己建立一个热点 ,另一个手机或PC 连接到网络 即可通过程序发送文件 。 不需要cmcc 等 第三方的无限网络 。 不耗GPRS 流量 可以放心 传送文件。...

    删除android系统应用,需要root

    删除android系统应用,需要root

    SuperSU Pro 2.82 SR5.apk

    注意:需要特殊程序进行卸载。如果你不喜欢这个应用程序,请不要*只是卸载它,你*会*失去根源。 超级用户访问管理贯穿所谓的“su二进制”。一次只能有一个。因此,如果您安装SuperSU,您以前的超级用户访问管理解决...

    Android APP 小工具测试“利器”

    1.优化Android 8.0上清理应用程序失败未提示的bug 2.增加卸载功能 3.解决备份apk时读取信息错误的问题 4.优化异常提示并给予回调显示 2019年3月6日 已拓展功能:截图、关闭应用程序、清除应用程序缓存并启动应用...

    新版Android开发教程.rar

    程序可以采用 JAVA 开发,但是因为它的虚拟机 (Virtual Machine) Dalvik ,是将 JAVA 的 bytecode 转成 自 己的格式,回避掉需要付给 SUN 有关 JAVA 的授权费用。 对手机制造者的影响 � Android 是款开源的移动计算...

    ADB-Google-Glass.app:自动注册ADB终端,以及从Mac将.apks快速安装和卸载到Android设备

    然后提示安装或卸载apk。 此构建的目的是简化USB和WiFi连接的Android设备到Android调试桥(ADB)的注册。 它被设计为一种多功能,轻巧的,随时可用的设置,可以快速注册并允许通过终端应用程序为ADB进行命令行输入...

    Android开发资料合集-World版!

    9.13、调用ANDROID INSTALLER 安装和卸载程序 215 9.14、后台监控应用程序包的安装&卸载 216 9.15、显示应用详细列表 224 9.16、寻找应用 224 9.17、注册一个BROADCASTRECEIVER 225 9.18、打开另一程序 225 9.19、...

    Android 开发技巧

    9.13、调用ANDROID INSTALLER 安装和卸载程序 215 9.14、后台监控应用程序包的安装&卸载 216 9.15、显示应用详细列表 224 9.16、寻找应用 224 9.17、注册一个BROADCASTRECEIVER 225 9.18、打开另一程序 225 9.19、...

    adb1.0.26包含fastboot.exe

    ADB,即 Android Debug Bridge,它是 Android 开发/测试人员不可替代的强大工具,也是 Android 设备玩家的好玩具。 注:有部分命令的支持情况可能与 Android 系统版本及定制 ROM 的实现有关。 基本用法 命令语法 ...

    open-quartz:谷歌眼镜开发 - GDK + SDK

    安装/卸载应用程序(.apks): adb install -r FILE.apk adb uninstall FILE.apk 运行应用程序: adb shell am start -n PACKAGE.NAME/.MAIN.ACTIVITY.NAME 列出您的 Android 设备上的所有包: adb shell pm

    手机关电脑

    3. 将CloseWinClient.apk 安装到Android手机运行。 4. 运行电脑上的软件,在手机的IP地址上输入电脑软件上给出的IP (个人创建热点的IP是192.168开头,校园网IP以122开头,还有联通网不知) 如果连的是个人创建的...

    一键获取索爱sk17i ROOT 权限

    1.进入手机“设置”,“应用程序”,勾选“未知来源”。 ┃ echo ┃ ┃ echo ┃ 2.进入手机“设置”,“应用程序”,“开发”,勾选“USB 调试”。 ┃ echo ┃ ┃ echo ┃ 3.确保您手机中插入了一张内存卡(最好先...

Global site tag (gtag.js) - Google Analytics