I am trying the following code to install apk file saved on the SD Card and getting following exception: "android.content.ActivityNotFoundException: Unable to find explicit activity class {com.android.packageinstaller/.PackageInstallerActivity}". In the code, file
is representing the apk file's SD Card path.
Intent resultIntent = new Intent(android.content.Intent.ACTION_VIEW);
resultIntent.setDataAndType(Uri.fromFile(file),"application/vnd.android.package-archive");
resultIntent.setClassName("com.and开发者_运维技巧roid.packageinstaller", ".PackageInstallerActivity");
startActivity(resultIntent);
How do I manage this?
You might want to read this blog post on how to install and remove packages using the PackageInstaller.
Note that since the package installer is registered to handle the MIME type application/vnd.android.package-archive
, you shouldn't have to specify the intent class name explicitly; the intent resolver should find the right one to start.
Also, the operation will succeed only if the user has enabled INSTALL_NON_MARKET_APPS
. This is directly related to the failure you are seeing, but it's something to keep in mind as you'll likely run into that as well.
Try to add the Intent.FLAG_ACTIVITY_NEW_TASK
like so:
Intent resultIntent = new Intent();
resultIntent.setAction(Intent.ACTION_VIEW);
resultIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
resultIntent.setDataAndType(Uri.fromFile(file),"application/vnd.android.package-archive");
And then call
startActivity(resultIntent);
NOTE! that the condition regarding INSTALL_NON_MARKET_APPS
, as mentioned earlier by Franci Penov, still must be valid!
精彩评论