[英]How to Detect if user has confirmed for installation in PackageInstaller (Android)
Background : I am installing android app programmatically using PackageInstaller.背景:我正在使用 PackageInstaller 以编程方式安装 android 应用程序。
What am I doing : The code which I am using is mentioned below.我在做什么:下面提到了我正在使用的代码。 This is the same code which has been provided in PackageInstaller sample by google.
这与 google 在 PackageInstaller 示例中提供的代码相同。
private void init() {
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);
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();
}
}
}
Query : The code works perfectly fine but PackageInstaller
doesn't give any status on clicking the INSTALL button where as onclicking the CANCEL button, it provides status.查询:代码工作得很好,但是
PackageInstaller
在单击 INSTALL 按钮时没有给出任何状态,而当单击 CANCEL 按钮时,它会提供状态。 I have to perform some action when user confirms installation by clicking install button.当用户通过单击安装按钮确认安装时,我必须执行一些操作。 Is there any other way to get the status when install button is clicked?
单击安装按钮时,还有其他方法可以获取状态吗?
Note : I don't want the status after installation is successful but I want it when INSTALL button is clicked.注意:我不想要安装成功后的状态,但我想要单击 INSTALL 按钮时的状态。
You can register a broadcast for action PACKAGE_ADDED
and even check if the package is installed using PackageManager#getPackageInfo
您可以为操作
PACKAGE_ADDED
注册广播,甚至可以使用PackageManager#getPackageInfo
检查是否安装了 package
Refer to these questions:参考这些问题:
Edit: After realizing that you are trying to retrieve the click event on the "Install" button instead- I don't think it's possible by any good means.编辑:在意识到您正在尝试检索“安装”按钮上的点击事件之后 - 我认为这不可能通过任何好的方式。 You can change the flow of your app and respond to the success of the installation instead or maybe wait for someone else to pick on this question.
您可以更改应用程序的流程并响应安装成功,或者等待其他人回答这个问题。 They might be some "hacky" way to do this but I'd still not recommend going with it.
他们可能是一些“hacky”的方式来做到这一点,但我仍然不建议使用它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.