一、通过Intent消息机制发送消息,调用系统应用进行,实现apk的安装/卸载 。 
(1) 调用系统的安装应用,让系统自动进行apk的安装

String fileName = "/data/data/com.zlc.ipanel.operate/FileOperate.apk";
  Uri uri = Uri.fromFile(new File(fileName));
  Intent intent = new Intent(Intent.ACTION_VIEW);
  intent.setDataAndType(uri, "application/vnd.android.package-archive");
  startActivity(intent);

上诉安装不仅可以安装新的apk(从无到有),也可以用于更新旧的apk(版本更新),在进行版本更新的时候,必须保证两个apk的签名是一致的。 
    如果是一般应用,安装到data/分区下面,新的apk会替换旧的apk。 
    如果是系统应用,一般安装到system/app下面,更新之后, system/app/下的旧apk仍然存在,系统会将新的apk被复制到data/app下。虽然同时存在两新旧apk,但运行时系统选择data分区的新apk运行, 如果执行卸载则是删除data分区的apk,再次启动程序运行的是system目录下旧的apk。 
(2)启动系统的卸载应用,让系统自动卸载apk

//通过程序的包名创建URI
Uri packageURI = Uri.parse("package:com.zlc.ipanel");
//创建Intent意图
Intent intent = new Intent(Intent.ACTION_DELETE,packageURI);
//执行卸载程序
startActivity(intent);

与apk安装不同的是,Intent消息这里改了:ACTION_DELETE,apk安装使用的是(ACTION_VIEW)

二、通过调用系统提供的接口packageManager对apk进行卸载、安装、获取权限等(静默安装)
(1)需要平台签名(写个Android.mk在源码下编译最省事,当然网上也有很多其他方法,比如:singapk命令行签名)
(2)由于调用的是未公开的API,所以需要引入源码(仅仅做一个马甲,不参与编译)
(3)在AndroidManifest.xml添加安装apk的权限
    <uses-permission android:name="android.permission.INSTALL_PACKAGES" />
(4) 如果升级失败出现 Unable to open zip '/data/FileOperate.apk': Permission denied,说明是权限不够,有可能当前apk只有(-rw-------)权限,通过Runtime.exec方法修改权限之后再进行升级是可以成功 的。

PackageManager pm = this.getPackageManager();
  File file = new File("/data/data/com.zlc.ipanel.operate/FileOperate.apk");
  pm.installPackage(Uri.fromFile(file), null,
    PackageManager.INSTALL_REPLACE_EXISTING, "com.zlc.ipanel");

(5)下面举一个例子通过Eclipse来实现静默安装/卸载。
    1)按照我们上面讲的静默安装需求一步步实现我们的操作。首先准备马甲(静默安装需要调用的接口)
    由于调用了系统未公开的接口,而这些接口有些是通过aidl实现的,下面我们把需要的马甲修改一下。
    PackageManager.java

/*
* Copyright (C) 2006 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*      http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package android.content.pm;

import android.content.Context;
import android.net.Uri;

/**
* Class for retrieving various kinds of information related to the application
* packages that are currently installed on the device.
*
* You can find this class through {@link Context#getPackageManager}.
*/
public abstract class PackageManager {

/**
     * Flag parameter for {@link #installPackage} to indicate that you want to replace an already
     * installed package, if one exists.
     * @hide
     */
    public static final int INSTALL_REPLACE_EXISTING = 0x00000002;

/**
     * @hide
     *
     * Install a package. Since this may take a little while, the result will
     * be posted back to the given observer.  An installation will fail if the calling context
     * lacks the {@link android.Manifest.permission#INSTALL_PACKAGES} permission, if the
     * package named in the package file's manifest is already installed, or if there's no space
     * available on the device.
     *
     * @param packageURI The location of the package file to install.  This can be a 'file:' or a
     * 'content:' URI.
     * @param observer An observer callback to get notified when the package installation is
     * complete. {@link IPackageInstallObserver#packageInstalled(String, int)} will be
     * called when that happens.  observer may be null to indicate that no callback is desired.
     * @param flags - possible values: {@link #INSTALL_FORWARD_LOCK},
     * {@link #INSTALL_REPLACE_EXISTING}, {@link #INSTALL_ALLOW_TEST}.
     * @param installerPackageName Optional package name of the application that is performing the
     * installation. This identifies which market the package came from.
     */
    public abstract void installPackage(
            Uri packageURI, IPackageInstallObserver observer, int flags,
            String installerPackageName);

/**
     * Attempts to delete a package.  Since this may take a little while, the result will
     * be posted back to the given observer.  A deletion will fail if the calling context
     * lacks the {@link android.Manifest.permission#DELETE_PACKAGES} permission, if the
     * named package cannot be found, or if the named package is a "system package".
     * (TODO: include pointer to documentation on "system packages")
     *
     * @param packageName The name of the package to delete
     * @param observer An observer callback to get notified when the package deletion is
     * complete. {@link android.content.pm.IPackageDeleteObserver#packageDeleted(boolean)} will be
     * called when that happens.  observer may be null to indicate that no callback is desired.
     * @param flags - possible values: {@link #DONT_DELETE_DATA}
     *
     * @hide
     */
    public abstract void deletePackage(
            String packageName, IPackageDeleteObserver observer, int flags);

}

安装成功的回调接口IPackageInstallObserver.java(修改过的马甲)

package android.content.pm;

import android.os.RemoteException;

public interface IPackageInstallObserver {
public class Stub implements IPackageInstallObserver{
  public void packageInstalled(String packageName, int returnCode)
    throws RemoteException {
   // TODO Auto-generated method stub

}
}
}

卸载成功的回调接口IPackageDeleteObserver.java(修改过的马甲)

package android.content.pm;

public interface IPackageDeleteObserver {
public class Stub implements IPackageDeleteObserver{
  public void packageDeleted(String packageName, int returnCode) {
   // TODO Auto-generated method stub 
  }
}
}

2)马甲准备好之后,就开始实现我们的操作逻辑。
    实现静默安装/卸载的方法,并且注册回调(无论失败成功都会返回对应值)

//静默安装
public static void installApkDefaul(Context context,String fileName,String pakcageName){
  Log.d(TAG, "jing mo an zhuang");
  File file = new File(fileName);
  int installFlags = 0;
  if(!file.exists())
   return;
  Log.d(TAG, "jing mo an zhuang  out");
  installFlags |= PackageManager.INSTALL_REPLACE_EXISTING; 
  PackageManager pm = context.getPackageManager();
  IPackageInstallObserver observer = new MyPakcageInstallObserver();
  pm.installPackage(Uri.fromFile(file), observer, installFlags, pakcageName);
}
//静默卸载
public static void uninstallApkDefaul(Context context,String packageName){
  PackageManager pm = context.getPackageManager();
  IPackageDeleteObserver observer = new MyPackageDeleteObserver();
        pm.deletePackage(packageName, observer, 0);
}
//静默卸载回调
private static class MyPackageDeleteObserver extends IPackageDeleteObserver.Stub{
  @Override
  public void packageDeleted(String packageName, int returnCode) {
   // TODO Auto-generated method stub
   Log.d(TAG, "returnCode = "+returnCode);//返回1代表卸载成功
  }

}
//静默安装回调
private static class MyPakcageInstallObserver extends IPackageInstallObserver.Stub{
  @Override
  public void packageInstalled(String packageName, int returnCode)
    throws RemoteException {
   // TODO Auto-generated method stub
       Log.i(TAG, "returnCode = " + returnCode);//返回1代表安装成功 
  }
}

3)主要的逻辑实现之后,在AndroidManifest.xml 添加安装和卸载的权限

<uses-permission android:name="android.permission.INSTALL_PACKAGES" />
<uses-permission android:name="android.permission.DELETE_PACKAGES" />

4)运行Eclipse,生成该应用的apk,下面就要进行系统签名了。写一个脚本来来实现替换成系统签名
    使用签名包签名首先我们要去源码里面取出下面这几个文件platform.x509.pem、platform.pk8、signapk.jar
    
    脚本1.bat (和生成的apk以及上面三个文件放到同一个目录下面,一般为工程下面的bin目录)

java -jar  signapk.jar  platform.x509.pem  platform.pk8 FileUpdateControl.apk 1.apk
adb uninstall com.zlc.ipanel.operate
adb  install 1.apk

5)当下载到系统里面的apk权限不够时(静默安装提示权限问题,有可能当前apk只有(-rw-------)权限)
    可以使用下面三种方式修改权限

1.使用Runtime来启动一个线程修改权限

try {
   Runtime.getRuntime().exec("chmod 777 " + file.getCanonicalPath());
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }

2.写一个native接口直接通过jni调用c的接口修改权限。
    3.使用FileUtils,这个类默认是隐藏的,官方sdk中不包含这个类,所以代码如果要使用这个类,需要将工程放到android源码中编译
          FileUtils.setPermissions(f.getAbsolutePath(), FileUtils.S_IRUSR | FileUtils.S_IWUSR | FileUtils.S_IRGRP | FileUtils.S_IROTH, -1, -1) ;

三、其他apk安装的方法

(1)通过cmd窗口 adb 命令,install / uninstall apk。
    (2)cp apk 到 system/app目录下,系统会自动安装 
    (3)直接通过代码调用pm命令执行apk的安装和卸载

1

execCommand("pm", "install", "-f", filePath);//安装apk,filePath为apk文件路径,如/sdcard/operate.apk

2

execCommand("pm", "uninstall", packageName);//卸载apk,packageName为包名,如com.zlc.ipanel

Android apk的安装、卸载、更新升级(通过Eclipse实现静默安装)的更多相关文章

  1. Android应用程序的自动更新升级(自身升级、通过tomcat)(转)

    Android应用程序的自动更新升级(自身升级.通过tomcat) http://blog.csdn.net/mu0206mu/article/details/7204746 刚入手android一个 ...

  2. 15个Linux Yum命令实例--安装/卸载/更新

    在linux环境中, 安装, 卸载更新包是一个极为常见的操作.大部分的linux环境都提供了包的管理工具, 例如:apt-get, dpkg, rpm, yum等等. 一些Linux环境中,yum是默 ...

  3. maven的安装,maven库配置和Eclipse插件的安装

    maven的安装,maven库配置和Eclipse插件的安装 1.下载并解压maven 2.配置环境变量 3.配置maven配置文件 1.下载链接 Downloading Apache Maven 2 ...

  4. Android 安装 卸载 更新 程序

    安装程序的方法: .通过Intent机制,调出系统安装应用,重新安装应用的话,会保留原应用的数据. 1. String fileName =Environment.getExternalStorage ...

  5. rpm 更新/升级 软件包(libGL-devel手动安装过程)

    rpm参数解释 -i 安装 -h 解压rpm的时候打印50个斜条 (#) -v 显示详细信息 升级命令rpm -Uvh rpm文件名 参数解释 -U 升级 -h 解压rpm的时候打印50个斜条 (#) ...

  6. APP安全测试之安装/卸载/更新测试

    在app测试中,有个不可忽视的测试方向,就是安装.卸载和更新,有很多人问到了这个问题,我就在这里做了一个总结,有补充的请留言哦 安装 1.正常安装测试,检查是否安装成功. 2.APP版本覆盖测试.例如 ...

  7. Android公共库(缓存 下拉ListView 下载管理Pro 静默安装 root运行 Java公共类)

    介绍总结的一些android公共库,包含缓存(图片缓存.预取缓存).公共View(下拉及底部加载更多ListView.底部加载更多ScrollView.滑动一页Gallery).及Android常用工 ...

  8. 最全APP安装/卸载/更新测试点

    1.安装查看在安装过程中存在的提示信息是否明确,意思是否明确在安装过程中,点击取消按钮,能否正常退出安装程序,软件是否可用.安装时是否识别有SD卡,并默认安装到sd卡中安装过程中,接听电话或者短信,安 ...

  9. Ubuntu16.04中pip无法更新升级,采用源码方式安装

    1.从pip官网下载最新版 https://pypi.org/project/pip/#files 2.ubuntu中创建文件位置,我的放在一下路径,之后进行解压 3.解压后进入pip的文件夹,在执行 ...

随机推荐

  1. HDU 4325-Flowers(线段树+离散化)

    题意: 给出每个花开花的时间段,每询问一个时间点输出该时间点开花的数量 分析: 线段树的区间更新,单点查询,但发现时间很大,没法存区间,就想到了离散化. 离散化就是把要处理的数据统一起来重新标号. # ...

  2. IOS 通知 alarm 记录

    所有的内容融为一体,去除某一个项不知道结果如何. 最主要的前提:APP 会长期保留在后台 1.在info.plist 文件里面,加入 audio 后台请求 2.当APP 点击home进入后台之后,请求 ...

  3. 瞬间从IT屌丝变大神——CSS规范

    CSS规范主要包括以下内容: CSS Reset用YUI的CSS Reset. CSS采用CSSReset+common.css+app.css的形式. app.css采用分工制,一个前端工程师负责一 ...

  4. 瞬间从IT屌丝变大神——HTML规范

    HTML规范包含以下内容: DTD统一用<!DOCTYPE HTML PUBLIC "_//W3C//DTD XHTML 1.0 Transitional//EN"" ...

  5. -exec 与 xargs 的区别

    实地在shell里执行下如下命令就知道区别了: $ find -type f -exec echo {} \; 很明显,exec是对每个找到的文件执行一次命令.从这里可以看出exec的缺点是每处理一个 ...

  6. homework_06 围棋程序改进

    1) 把程序编译通过, 跑起来. 读懂程序,在你觉得比较难懂的地方加上一些注释,这样大家就能比较容易地了解这些程序在干什么. 把正确的 playPrev(GoMove) 的方法给实现了. 注释见Git ...

  7. Codeforces 599C Day at the Beach(想法题,排序)

    C. Day at the Beach One day Squidward, Spongebob and Patrick decided to go to the beach. Unfortunate ...

  8. nyoj 10 skiing(记忆化搜索)

    skiing 时间限制:3000 ms  |  内存限制:65535 KB 难度:5   描述 Michael喜欢滑雪百这并不奇怪, 因为滑雪的确很刺激.可是为了获得速度,滑的区域必须向下倾斜,而且当 ...

  9. UVA 11354 Bond(MST + LCA)

    n<=50000, m<=100000的无向图,对于Q<=50000个询问,每次求q->p的瓶颈路. 其实求瓶颈路数组maxcost[u][v]有用邻接矩阵prim的方法.但是 ...

  10. ASP.NET MVC- Area 使用

    ASP.NET MVC允许使用 Area(区域)来组织Web应用程序,每个Area代表应用程序的不同功能模块.这对于大的工程非常有用,Area 使每个功能模块都有各自的文件夹,文件夹中有自己的Cont ...