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

 百万发平台     |      2020-01-04 13:22

最近在搭建兼容性性能平台,目前架构图还是内部才能够看到,具体之后做完我会开放出来。

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

其中一个就是需要去获取Android应用的大小,我们现在自身的apk还是会做的尽量小,但是用了一段时间之后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)

接着就发现一个问题,2.3的版本的确可以用。但是4.2以及以上版本就报错了。经过调查发现 PackageManager.getPackageSizeInfo(String packageName, IPackageStatsObserver observer)不可用,改为PackageManager.getPackageSizeInfo(String packageName, int userHandle, IPackageStatsObserver observer)。

二、通过调用系统提供的接口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");

然后我install到了Nexus5 Android5.0上面,点击一个应用之后看到如下Log,说明成功了~~~

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

这个会作为我监听apk中的一个模块存在~~~

/*
* 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
*
*     
*
* 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
Android apk的安装、卸载、更新升级(通过Eclipse实现静默安装)。     * 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

上一篇:后台设计浅析 下一篇:没有了