PackageInstaller的初始化
PackageInstaller是系统安装器,当点击apk文件谈出来的是否安装的页面,就是PackageInstaller安装程序;
PackageInstaller的入口—InstallStart Activity
InstallStart是PackageInstaller程序的入口Activity:
InstallStart.onCreate()
Uri packageUri = intent.getData();
if (packageUri == null) {
// if there's nothing to do, quietly slip into the ether
Intent result = new Intent();
result.putExtra(Intent.EXTRA_INSTALL_RESULT,
PackageManager.INSTALL_FAILED_INVALID_URI);
setResult(RESULT_FIRST_USER, result);
nextActivity = null;
} else {
if (packageUri.getScheme().equals(SCHEME_CONTENT)) {
nextActivity.setClass(this, InstallStaging.class);
} else {
nextActivity.setClass(this, PackageInstallerActivity.class);
}
}
}
if (nextActivity != null) {
startActivity(nextActivity);
}
finish();
根据Uri的scheme协议,如果是SCHEME_CONTENT就跳转到InstallStaging,如果不是就跳转到PackageInstallerActivity;7.0后会使用FileProvider来处理URI,隐藏共享文件的真实路径,将其转为content;
InstallStaging.onResume()
mStagingTask = new StagingAsyncTask();
mStagingTask.execute(getIntent().getData());
在InstallStaging Activity中,开启来一个StagingAsyncTask,并且传入content类型的uri
StagingAsyncTask
private final class StagingAsyncTask extends AsyncTask<Uri, Void, Boolean> {
@Override
protected Boolean doInBackground(Uri... params) {
Uri packageUri = params[0];
try (InputStream in = getContentResolver().openInputStream(packageUri)) {
// Despite the comments in ContentResolver#openInputStream the returned stream can
// be null.
if (in == null) {
return false;
}
try (OutputStream out = new FileOutputStream(mStagedFile)) {
byte[] buffer = new byte[4096];
int bytesRead;
while ((bytesRead = in.read(buffer)) >= 0) {
out.write(buffer, 0, bytesRead);
}
}
}
return true;
}
@Override
protected void onPostExecute(Boolean success) {
if (success) {
// Now start the installatio