* >>#此处告诉开发者如何调⽤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
ample.t;
// Need the following import to get access to the app resources, since this
// class is in a sub-package.
ample.android.apis.R;
import android.app.Activity;
import android.app.PendingIntent;
隧道隔音降噪施工
t.Context;
t.Intent;
t.IntentSender;
t.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.msisdn>扣具
*
* @see InstallApk for a demo of the original (non-Session) API.
*/
public class InstallApkSessionApi extends Activity {
private static final String PACKAGE_INSTALLED_ACTION =
"ample.t.SESSION_API_PACKAGE_INSTALLED";
@Override
protected void onCreate(Bundle 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 = ateSession(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 = Activity(context, 0, intent, 0);
IntentSender statusReceiver = IntentSender();
// Commit the session (this will start the installation workflow).
sessionmit(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 = Extras();
if (PACKAGE_INSTALLED_ACTION.Action())) {
int status = Int(PackageInstaller.EXTRA_STATUS);
String message = String(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) (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();
}
}
}
马德保半球实验}
另外,权限要求:
需要系统签名
permissionTJA1100
<uses-permission android:name="android.permission.INSTALL_PACKAGES" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
到此这篇关于Android P实现静默安装的⽅法⽰例(官⽅Demo)的⽂章就介绍到这了,更多相关Android P 静默安装内容请搜索以前的⽂章或继续浏览下⾯的相关⽂章希望⼤家以后多多⽀持!