Android P實(shí)現(xiàn)靜默安裝的方法示例(官方Demo)
Android9.0無法通過以下兩種方式實(shí)現(xiàn)靜默安裝:
1.runtime執(zhí)行shell cmd
2.PackageInstall 反射機(jī)制
但是Google已經(jīng)給我們推薦了相關(guān)的APIDemos,所以建議大家多看看源碼~
在frameworks/base/core/java/android/content/pm/PackageInstaller.java有段關(guān)于該類的介紹:
- 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>
* ###########此處告訴開發(fā)者如何調(diào)用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相關(guān)的一共兩個(gè)demo
InstallApkSessionApi.java //靜默安裝
InstallApk.java //普通安裝,調(diào)用系統(tǒng)install intent進(jìn)行安裝
下面是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回調(diào)通知
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();
}
}
}
}
另外,權(quán)限要求:
需要系統(tǒng)簽名
permission
<uses-permission android:name="android.permission.INSTALL_PACKAGES" /> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
到此這篇關(guān)于Android P實(shí)現(xiàn)靜默安裝的方法示例(官方Demo)的文章就介紹到這了,更多相關(guān)Android P 靜默安裝 內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- Android9.0 靜默安裝源碼的實(shí)現(xiàn)
- Android 靜默安裝和智能安裝的實(shí)現(xiàn)方法
- Android程序靜默安裝安裝后重新啟動(dòng)APP的方法
- Android 靜默安裝和卸載的方法
- Android實(shí)現(xiàn)靜默安裝實(shí)例代碼
- Android 靜默安裝實(shí)現(xiàn)方法
- Android實(shí)現(xiàn)靜默安裝的兩種方法
- Android靜默安裝實(shí)現(xiàn)方案 仿360手機(jī)助手秒裝和智能安裝功能
- Android無需root實(shí)現(xiàn)apk的靜默安裝
- android實(shí)現(xiàn)靜默安裝與卸載的方法
相關(guān)文章
Android編程實(shí)現(xiàn)多列顯示的下拉列表框Spinner功能示例
這篇文章主要介紹了Android編程實(shí)現(xiàn)多列顯示的下拉列表框Spinner功能,結(jié)合具體實(shí)例形式分析了Android多列表顯示功能的相關(guān)布局操作實(shí)現(xiàn)技巧,需要的朋友可以參考下2017-06-06
android之json數(shù)據(jù)過長(zhǎng)打印不全問題的解決
這篇文章主要介紹了android之json數(shù)據(jù)過長(zhǎng)打印不全問題的解決,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-04-04
Android實(shí)現(xiàn)橫屏切換科學(xué)計(jì)算器
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)橫屏切換科學(xué)計(jì)算器,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-06-06
Android本地搜索業(yè)務(wù)優(yōu)化方案
這篇文章主要為大家介紹了Android本地搜索業(yè)務(wù)優(yōu)化方案詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-05-05
Android實(shí)現(xiàn)動(dòng)態(tài)定值范圍效果的控件
這篇文中給大家分享一個(gè)Android的控件,這個(gè)控件實(shí)現(xiàn)是一個(gè)可以動(dòng)態(tài)選擇定值范圍的效果,實(shí)現(xiàn)后的效果很不錯(cuò),對(duì)大家日常開發(fā)或許有所幫助,感興趣的朋友們可以一起來看看。2016-09-09
Android 通過Base64上傳圖片到服務(wù)器實(shí)現(xiàn)實(shí)例
這篇文章主要介紹了Android 通過Base64上傳圖片到服務(wù)器實(shí)現(xiàn)實(shí)例的相關(guān)資料,需要的朋友可以參考下2017-05-05
Android view更改背景資源與padding消失的問題解決辦法
這篇文章主要介紹了Android view更改背景資源與padding消失的問題解決辦法的相關(guān)資料,需要的朋友可以參考下2017-04-04
Android 中使用 AsyncTask 異步讀取網(wǎng)絡(luò)圖片
這篇文章主要介紹了Android 中使用 AsyncTask 異步讀取網(wǎng)絡(luò)圖片的相關(guān)資料,需要的朋友可以參考下2016-02-02

