当前位置:  编程技术>移动开发

Android 静默方式实现批量安装卸载应用程序的深入分析

    来源: 互联网  发布时间:2014-10-18

    本文导语:  前段时间做了一个批量安装卸载应用程序的小应用,由于安装卸载应用程序的部分API是隐藏的,所以必须在ubuntu下下载Android系统源码,并编译之后使用MM命令编译生成APK文件,其实也难。思路是这样的,在XX/packages/apps目录下...

前段时间做了一个批量安装卸载应用程序的小应用,由于安装卸载应用程序的部分API是隐藏的,所以必须在ubuntu下下载Android系统源码,并编译之后使用MM命令编译生成APK文件,其实也难。

思路是这样的,在XX/packages/apps目录下有一个PackageInstaller的应用程序,Android机器中安装卸载都是由这个应用程序完成的。但是它没有批量安装和卸载的功能,如果要在自己的应用程序中添加批量安装和卸载的功能,其实很简单,只需要参考PakcageInstaller里面的安装卸载代码加个循环就可以了。但值得注意的是在编译的过程中必须复制PackageInstaller里面的Android.mk文件,修改文件为工程目录名。
好了,废话不再多说,下面是关键代码
1、 Android.mk文件
代码如下:

LOCAL_PATH:= $(call my-dir)
include $(CLEAR_VARS)

LOCAL_MODULE_TAGS := optional

LOCAL_SRC_FILES := $(call all-subdir-java-files)

LOCAL_PACKAGE_NAME := PackageInstaller
LOCAL_CERTIFICATE := platform

include $(BUILD_PACKAGE)

代码如下:

    LOCAL_PATH:= $(call my-dir) 
    include $(CLEAR_VARS) 

    LOCAL_MODULE_TAGS := optional 

    LOCAL_SRC_FILES := $(call all-subdir-java-files) 

    LOCAL_PACKAGE_NAME := PackageInstaller 
    LOCAL_CERTIFICATE := platform 

    include $(BUILD_PACKAGE) 

2、PakcageInstaller.java文件(关键代码)
代码如下:

    package cn.ceadic.apkmgr;

    import java.io.File;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;

    import android.content.Context;
    import android.content.Intent;
    import android.content.pm.PackageInfo;
    import android.content.pm.PackageManager;
    import android.content.pm.PackageManager.NameNotFoundException;
    import android.net.Uri;
    import android.util.Log;

    import android.content.pm.IPackageInstallObserver;
    import android.content.pm.IPackageDeleteObserver;
    import android.os.FileUtils;

    
    public class PackageInstaller {

        private File mTmpFile;
        private final String TMP_FILE_NAME = "tmpCopy.apk";

        private final static String TAG = "PackInstaller";
        private Context mContext;

        public PackageInstaller(Context context) {
            mContext = context;
        }

        
        public void install(String path,String packageName){
             Intent intent = new Intent(Intent.ACTION_VIEW);
             intent.setDataAndType(Uri.fromFile(new File(path)),
             "application/vnd.android.package-archive");
             mContext.startActivity(intent);
        }

        public void instatllBatch(String path, String packageName) {

            Log.i(TAG, "path=" + path);
            int installFlags = 0;
            PackageManager pm = mContext.getPackageManager();
            try {
                PackageInfo pi = pm.getPackageInfo(packageName,
                        PackageManager.GET_UNINSTALLED_PACKAGES);
                if (pi != null) {
                    installFlags |= PackageManager.INSTALL_REPLACE_EXISTING;
                }
            } catch (NameNotFoundException e) {
            }
            if ((installFlags & PackageManager.INSTALL_REPLACE_EXISTING) != 0) {
                Log.w(TAG, "Replacing package:" + packageName);
            }

            // Create temp file before invoking install api
            mTmpFile = createTempPackageFile(path);
            if (mTmpFile == null) {
                // Message msg = mHandler.obtainMessage(INSTALL_COMPLETE);
                // msg.arg1 = PackageManager.INSTALL_FAILED_INSUFFICIENT_STORAGE;
                // mHandler.sendMessage(msg);
                return;
            }
            Uri mPackageURI = Uri.parse("file://" + mTmpFile.getPath());
            String installerPackageName = mContext.getIntent().getStringExtra(
                    Intent.EXTRA_INSTALLER_PACKAGE_NAME);

            PackageInstallObserver observer = new PackageInstallObserver();
            pm.installPackage(mPackageURI, observer, installFlags,
                    installerPackageName);
        }

        private File createTempPackageFile(String filePath) {
            File tmpPackageFile = mContext.getFileStreamPath(TMP_FILE_NAME);
            if (tmpPackageFile == null) {
                Log.w(TAG, "Failed to create temp file");
                return null;
            }
            if (tmpPackageFile.exists()) {
                tmpPackageFile.delete();
            }
            // Open file to make it world readable
            FileOutputStream fos;
            try {
                fos = openFileOutput(TMP_FILE_NAME, MODE_WORLD_READABLE);
            } catch (FileNotFoundException e1) {
                Log.e(TAG, "Error opening file " + TMP_FILE_NAME);
                return null;
            }
            try {
                fos.close();
            } catch (IOException e) {
                Log.e(TAG, "Error opening file " + TMP_FILE_NAME);
                return null;
            }

            File srcPackageFile = new File(filePath);
            if (!FileUtils.copyFile(srcPackageFile, tmpPackageFile)) {
                Log.w(TAG, "Failed to make copy of file: " + srcPackageFile);
                return null;
            }
            return tmpPackageFile;
        }

        private class PackageInstallObserver extends IPackageInstallObserver.Stub {
            public void packageInstalled(String packageName, int returnCode) {
                // Message msg = mHandler.obtainMessage(INSTALL_COMPLETE);
                // msg.arg1 = returnCode;
                // mHandler.sendMessage(msg);
                Log.i(TAG, "====INSTALL_COMPLETE");
            }
        }

        private class PackageDeleteObserver extends IPackageDeleteObserver.Stub {
            public void packageDeleted(boolean succeeded) {
    //            Message msg = mHandler.obtainMessage(UNINSTALL_COMPLETE);
    //            msg.arg1 = succeeded?SUCCEEDED:FAILED;
    //            mHandler.sendMessage(msg);
                Log.i(TAG, "====UNINSTALL_COMPLETE");
            }
        }

        public void uninstall(String packageName){
            Uri packageURI = Uri.parse("package:" + packageName);
            Intent uninstallIntent = new Intent(Intent.ACTION_DELETE,
            packageURI);
            mContext.startActivity(uninstallIntent);
        }

        public void uninstallBatch(String packageName) {
            PackageDeleteObserver observer = new PackageDeleteObserver();
            mContext.getPackageManager().deletePackage(packageName, observer, 0);

        }
    } 

代码如下:

    package cn.ceadic.apkmgr; 

    import java.io.File; 
    import java.io.FileNotFoundException; 
    import java.io.FileOutputStream; 
    import java.io.IOException; 

    import android.content.Context; 
    import android.content.Intent; 
    import android.content.pm.PackageInfo; 
    import android.content.pm.PackageManager; 
    import android.content.pm.PackageManager.NameNotFoundException; 
    import android.net.Uri; 
    import android.util.Log; 

    import android.content.pm.IPackageInstallObserver; 
    import android.content.pm.IPackageDeleteObserver; 
    import android.os.FileUtils; 

     
    public class PackageInstaller { 

        private File mTmpFile; 
        private final String TMP_FILE_NAME = "tmpCopy.apk"; 

        private final static String TAG = "PackInstaller"; 
        private Context mContext; 

        public PackageInstaller(Context context) { 
            mContext = context; 
        } 

         
        public void install(String path,String packageName){ 
             Intent intent = new Intent(Intent.ACTION_VIEW); 
             intent.setDataAndType(Uri.fromFile(new File(path)), 
             "application/vnd.android.package-archive"); 
             mContext.startActivity(intent); 
        } 

        public void instatllBatch(String path, String packageName) { 

            Log.i(TAG, "path=" + path); 
            int installFlags = 0; 
            PackageManager pm = mContext.getPackageManager(); 
            try { 
                PackageInfo pi = pm.getPackageInfo(packageName, 
                        PackageManager.GET_UNINSTALLED_PACKAGES); 
                if (pi != null) { 
                    installFlags |= PackageManager.INSTALL_REPLACE_EXISTING; 
                } 
            } catch (NameNotFoundException e) { 
            } 
            if ((installFlags & PackageManager.INSTALL_REPLACE_EXISTING) != 0) { 
                Log.w(TAG, "Replacing package:" + packageName); 
            } 

            // Create temp file before invoking install api 
            mTmpFile = createTempPackageFile(path); 
            if (mTmpFile == null) { 
                // Message msg = mHandler.obtainMessage(INSTALL_COMPLETE); 
                // msg.arg1 = PackageManager.INSTALL_FAILED_INSUFFICIENT_STORAGE; 
                // mHandler.sendMessage(msg); 
                return; 
            } 
            Uri mPackageURI = Uri.parse("file://" + mTmpFile.getPath()); 
            String installerPackageName = mContext.getIntent().getStringExtra( 
                    Intent.EXTRA_INSTALLER_PACKAGE_NAME); 

            PackageInstallObserver observer = new PackageInstallObserver(); 
            pm.installPackage(mPackageURI, observer, installFlags, 
                    installerPackageName); 
        } 

        private File createTempPackageFile(String filePath) { 
            File tmpPackageFile = mContext.getFileStreamPath(TMP_FILE_NAME); 
            if (tmpPackageFile == null) { 
                Log.w(TAG, "Failed to create temp file"); 
                return null; 
            } 
            if (tmpPackageFile.exists()) { 
                tmpPackageFile.delete(); 
            } 
            // Open file to make it world readable 
            FileOutputStream fos; 
            try { 
                fos = openFileOutput(TMP_FILE_NAME, MODE_WORLD_READABLE); 
            } catch (FileNotFoundException e1) { 
                Log.e(TAG, "Error opening file " + TMP_FILE_NAME); 
                return null; 
            } 
            try { 
                fos.close(); 
            } catch (IOException e) { 
                Log.e(TAG, "Error opening file " + TMP_FILE_NAME); 
                return null; 
            } 

            File srcPackageFile = new File(filePath); 
            if (!FileUtils.copyFile(srcPackageFile, tmpPackageFile)) { 
                Log.w(TAG, "Failed to make copy of file: " + srcPackageFile); 
                return null; 
            } 
            return tmpPackageFile; 
        } 

        private class PackageInstallObserver extends IPackageInstallObserver.Stub { 
            public void packageInstalled(String packageName, int returnCode) { 
                // Message msg = mHandler.obtainMessage(INSTALL_COMPLETE); 
                // msg.arg1 = returnCode; 
                // mHandler.sendMessage(msg); 
                Log.i(TAG, "====INSTALL_COMPLETE"); 
            } 
        } 

        private class PackageDeleteObserver extends IPackageDeleteObserver.Stub { 
            public void packageDeleted(boolean succeeded) { 
    //            Message msg = mHandler.obtainMessage(UNINSTALL_COMPLETE); 
    //            msg.arg1 = succeeded?SUCCEEDED:FAILED; 
    //            mHandler.sendMessage(msg); 
                Log.i(TAG, "====UNINSTALL_COMPLETE"); 
            } 
        } 

        public void uninstall(String packageName){ 
            Uri packageURI = Uri.parse("package:" + packageName); 
            Intent uninstallIntent = new Intent(Intent.ACTION_DELETE, 
            packageURI); 
            mContext.startActivity(uninstallIntent); 
        } 

        public void uninstallBatch(String packageName) { 
            PackageDeleteObserver observer = new PackageDeleteObserver(); 
            mContext.getPackageManager().deletePackage(packageName, observer, 0); 

        } 
    } 

3、别忘记添加权限
代码如下:

   
       
       
       
       
         

代码如下:

     
         
         
         
         
         

以上代码在Android2.1的SDK中编译通过,并正确批量安装卸载应用程序

    
 
 

您可能感兴趣的文章:

  • 使用python编写批量卸载手机中安装的android应用脚本
  • 在Android 模拟器上安装和卸载APK包的方法
  • android开发教程之卸载sd卡对MediaServer的处理
  • android如何添加桌面图标和卸载程序后自动删除图标
  • android监听安装和卸载示例
  • Android 监听apk安装替换卸载广播的实现代码
  • 开发android应用程序时,用java调用so库输出日志问题
  • Android获取应用程序名称(ApplicationName)示例
  • android开发教程之获取使用当前api的应用程序名称
  • 分享Android平板电脑上开发应用程序不能全屏显示的问题解决
  • 解析Android应用程序运行机制
  • Android 程序应用的生命周期
  • android通过代码的形式来实现应用程序的方法
  • ANDROID应用程序的混淆打包分享
  • Android判断当前应用程序处于前台还是后台的两种方法
  • Android的Service应用程序组件基本编写方法
  • android 为应用程序创建桌面快捷方式技巧分享
  • Android实现获取应用程序相关信息列表的方法
  • Android获取应用程序大小的方法
  • android通过蓝牙接收文件打开时无法自动选择合适的应用程序
  • android 完全退出应用程序实现代码
  • Android 完全退出应用程序的解决方法
  • Android应用程序签名步骤及相关知识介绍
  • android实现获取正在运行的应用程序
  • 数据库 iis7站长之家
  •  
    本站(WWW.)旨在分享和传播互联网科技相关的资讯和技术,将尽最大努力为读者提供更好的信息聚合和浏览方式。
    本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。












  • 相关文章推荐
  • 深入android Unable to resolve target 'android-XX'详解
  • 深入Android开发FAQ的详解
  • Android 自动化测试经验分享 深入UiScrollable
  • 基于android中读取assets目录下a.txt文件并进行解析的深入分析
  • Android中asset文件夹与raw文件夹的区别深入解析
  • Android开发之文件操作模式深入理解
  • Android Mms之:深入理解对话列表管理
  • android生命周期深入分析(一)
  • Android 关机弹出选择菜单的深入解析
  • 深入Android SQLite 事务处理详解
  • ubuntu 12.10 上 android 编译环境搭建的深入解析
  • 深入Android Browser配置管理的详解
  • 深入Android MediaPlayer的使用方法详解
  • Android中使用PULL方式解析XML文件深入介绍
  • 深入android中The connection to adb is down的问题以及解决方法
  • Android笔记之:深入为从右向左语言定义复杂字串的详解
  • Android开发笔记之:深入理解Cursor相关的性能问题
  • 深入Understanding Android ContentProvider详解
  • Android笔记之:深入ViewStub的应用
  • 深入分析Android ViewStub的应用详解
  • 申请Android Map 的API Key(v2)的最新申请方式(SHA1密钥)
  • Android瀑布流实例 android_waterfall
  • Android开发需要的几点注意事项总结
  • Android系统自带样式 (android:theme)
  • android 4.0 托管进程介绍及优先级和回收机制
  • Android网络共享软件 Android Wifi Tether
  • Android访问与手机通讯相关类的介绍
  • Android 图标库 Android GraphView
  • Android及andriod无线网络Wifi开发的几点注意事项
  • 轻量级Android开发工具 Android Tools
  • Android 2.3 下StrictMode介绍


  • 站内导航:


    特别声明:169IT网站部分信息来自互联网,如果侵犯您的权利,请及时告知,本站将立即删除!

    ©2012-2021,,E-mail:www_#163.com(请将#改为@)

    浙ICP备11055608号-3