Android P实现静默安装的方法示例(官方Demo)
作者:WagonWagon 发布时间:2022-04-05 20:06:13
标签:AndroidP,静默安装
Android9.0无法通过以下两种方式实现静默安装:
1.runtime执行shell cmd
2.PackageInstall 反射机制
但是Google已经给我们推荐了相关的APIDemos,所以建议大家多看看源码~
在frameworks/base/core/java/android/content/pm/PackageInstaller.java有段关于该类的介绍:
The ApiDemos project contains examples of using this API:
<code>ApiDemos/src/com/example/android/apis/content/InstallApk*.java</code>.
/**
* Offers the ability to install, upgrade, and remove applications on the
* device. This includes support for apps packaged either as a single
* "monolithic" APK, or apps packaged as multiple "split" APKs.
* <p>
* An app is delivered for installation through a
* {@link PackageInstaller.Session}, which any app can create. Once the session
* is created, the installer can stream one or more APKs into place until it
* decides to either commit or destroy the session. Committing may require user
* intervention to complete the installation.
* <p>
* Sessions can install brand new apps, upgrade existing apps, or add new splits
* into an existing app.
* <p>
* Apps packaged as multiple split APKs always consist of a single "base" APK
* (with a {@code null} split name) and zero or more "split" APKs (with unique
* split names). Any subset of these APKs can be installed together, as long as
* the following constraints are met:
* <ul>
* <li>All APKs must have the exact same package name, version code, and signing
* certificates.
* <li>All APKs must have unique split names.
* <li>All installations must contain a single base APK.
* </ul>
* <p>
* ###########此处告诉开发者如何调用API安装apk##############
* The ApiDemos project contains examples of using this API:
* <code>ApiDemos/src/com/example/android/apis/content/InstallApk*.java</code>.
*/
public class PackageInstaller
翻阅源码,InstallApk*.java相关的一共两个demo
InstallApkSessionApi.java //静默安装
InstallApk.java //普通安装,调用系统install intent进行安装
下面是InstallApkSessionApi.java的具体demo
package com.example.android.apis.content;
// Need the following import to get access to the app resources, since this
// class is in a sub-package.
import com.example.android.apis.R;
import android.app.Activity;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.IntentSender;
import android.content.pm.PackageInstaller;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
/**
* Demonstration of package installation and uninstallation using the package installer Session
* API.
*
* @see InstallApk for a demo of the original (non-Session) API.
*/
public class InstallApkSessionApi extends Activity {
private static final String PACKAGE_INSTALLED_ACTION =
"com.example.android.apis.content.SESSION_API_PACKAGE_INSTALLED";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.install_apk_session_api);
// Watch for button clicks.
Button button = (Button) findViewById(R.id.install);
button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
PackageInstaller.Session session = null;
try {
PackageInstaller packageInstaller = getPackageManager().getPackageInstaller();
PackageInstaller.SessionParams params = new PackageInstaller.SessionParams(
PackageInstaller.SessionParams.MODE_FULL_INSTALL);
int sessionId = packageInstaller.createSession(params);
session = packageInstaller.openSession(sessionId);
addApkToInstallSession("HelloActivity.apk", session);
// Create an install status receiver.
Context context = InstallApkSessionApi.this;
Intent intent = new Intent(context, InstallApkSessionApi.class);
intent.setAction(PACKAGE_INSTALLED_ACTION);
//此处也可以使用getBoradcast或者getService回调通知
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
IntentSender statusReceiver = pendingIntent.getIntentSender();
// Commit the session (this will start the installation workflow).
session.commit(statusReceiver);
} catch (IOException e) {
throw new RuntimeException("Couldn't install package", e);
} catch (RuntimeException e) {
if (session != null) {
session.abandon();
}
throw e;
}
}
});
}
private void addApkToInstallSession(String assetName, PackageInstaller.Session session)
throws IOException {
// It's recommended to pass the file size to openWrite(). Otherwise installation may fail
// if the disk is almost full.
try (OutputStream packageInSession = session.openWrite("package", 0, -1);
InputStream is = getAssets().open(assetName)) {
byte[] buffer = new byte[16384];
int n;
while ((n = is.read(buffer)) >= 0) {
packageInSession.write(buffer, 0, n);
}
}
}
// Note: this Activity must run in singleTop launchMode for it to be able to receive the intent
// in onNewIntent().
@Override
protected void onNewIntent(Intent intent) {
Bundle extras = intent.getExtras();
if (PACKAGE_INSTALLED_ACTION.equals(intent.getAction())) {
int status = extras.getInt(PackageInstaller.EXTRA_STATUS);
String message = extras.getString(PackageInstaller.EXTRA_STATUS_MESSAGE);
switch (status) {
case PackageInstaller.STATUS_PENDING_USER_ACTION:
// This test app isn't privileged, so the user has to confirm the install.
Intent confirmIntent = (Intent) extras.get(Intent.EXTRA_INTENT);
startActivity(confirmIntent);
break;
case PackageInstaller.STATUS_SUCCESS:
Toast.makeText(this, "Install succeeded!", Toast.LENGTH_SHORT).show();
break;
case PackageInstaller.STATUS_FAILURE:
case PackageInstaller.STATUS_FAILURE_ABORTED:
case PackageInstaller.STATUS_FAILURE_BLOCKED:
case PackageInstaller.STATUS_FAILURE_CONFLICT:
case PackageInstaller.STATUS_FAILURE_INCOMPATIBLE:
case PackageInstaller.STATUS_FAILURE_INVALID:
case PackageInstaller.STATUS_FAILURE_STORAGE:
Toast.makeText(this, "Install failed! " + status + ", " + message,
Toast.LENGTH_SHORT).show();
break;
default:
Toast.makeText(this, "Unrecognized status received from installer: " + status,
Toast.LENGTH_SHORT).show();
}
}
}
}
另外,权限要求:
需要系统签名
permission
<uses-permission android:name="android.permission.INSTALL_PACKAGES" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
来源:https://www.jianshu.com/p/44360624db68


猜你喜欢
- 一、定界符成帧Framer接口package framer;import java.io.IOException;import java.i
- 这几天在上海出差,忙里偷闲学习了一下Apk的反编译工具的基本使用。下面就简单介绍一下如何将我们从网上下载的Apk文件进行反编译得到我们想要获
- public static string GetMD5(string sDataIn) &nb
- 本文实例讲述了Java实现的日期处理类。分享给大家供大家参考,具体如下:开发中常常要使用日期,先小结如下,以备后用。import java.
- 本文实例讲述了Android编程实现简单文件浏览器功能。分享给大家供大家参考,具体如下:运行效果:布局:<LinearLayout x
- 1.Nuget搜索Tesseract2.项目安装Tesseract3.引用命名空间using Tesseract;4.上Github下载别人
- 本文为大家分享了SpringBoot使用邮箱发送验证码实现注册功能实例,供大家参考,具体内容如下这里有两种方式:使用Apache Commo
- 目录一、泛型类型二、为什么需要泛型三、类型擦除四、类型擦除的后遗症五、Kotlin 泛型六、上界约束七、类型通配符 & 星号投影八、
- System.ComponentModel.Design.DesignSurface是为设计组件提供一个用户界面,通过它可以实现一个简单的窗
- 1、思维导图2、什么是MVC?MVC全名是Model View Controller,是模型(model)-视图(view)-控制器(con
- 步骤:1、创建一个项目,该项目主要用来设计用户控件。2、创建一个用户控件窗体,用来设计用户控件。3、向用户控件窗体中添加一个按钮(butto
- 本文演示如何在Android中实现ListView圆角效果。无论是网站,还是APP,人们都爱看一些新颖的视图效果。直角看多了,就想看看圆角,
- 看了网上关于记事本的查找替换很多,但都没有达到我想要的结果,然后自己学习总结了以下的方法:统计字符串(汉字,字母,数字,字符)先上效果图定义
- 在 Java 中,LinkedList 和 ArrayList 的性能是不同的,具体取决于你所需要的操作。对于频繁的插入和删除操作,Link
- 1.application.yml中添加两个datasourceserver: port: 8080spring: application:
- 进行GC性能调优时, 需要明确了解, 当前的GC行为对系统和用户有多大的影响。有多种监控GC的工具和方法, 本章将逐一介绍常用的工具。JVM
- 本文实例讲述了dotNet中的反射用法。分享给大家供大家参考,具体如下:参考MSDN:ms-help://MS.VSCC.2003/MS.M
- 1. 创建自定义 * 类并实现 HandlerInterceptor 接口package com.xgf.online_mall.inter
- 影响排序效率的一般从3个方面比较:数据比较的次数,数据移动的次数,内存空间占用的大小。我们就冒泡排序、选择排序、插入排序、快速排序做一个总的
- 近期在项目中,策划给出了一个需求就是,让按钮按照一个轮盘的轨迹进行滑动的效果,经过一番测试,实现了初步的效果。我这里区分了横向滑动和纵向滑动