-
Uri uri = Uri.parse("url_of_apk_file");
-
Intent it = new Intent(Intent.ACTION_VIEW, uri);
-
it.setData(uri);
-
it.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
-
it.setClassName("com.android.packageinstaller",
-
"com.android.packageinstaller.PackageInstallerActivity");
-
startActivity(it);
-
方法二:
private void installApk(final String apkPath)
{
Intent intent;
LogUtils.d(TAG, "================ installRuntime ===============: " + apkPath);
if (VersionUtils.getCurrentVersion() < VersionUtils.ICE_CREAM_SANDWICH)
{
intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(new File(apkPath)), "application/vnd.android.package-archive");
}
else
{
intent = new Intent(Intent.ACTION_INSTALL_PACKAGE);
intent.setData(Uri.fromFile(new File(apkPath)));
intent.putExtra(Intent.EXTRA_NOT_UNKNOWN_SOURCE, true);
intent.putExtra(Intent.EXTRA_RETURN_RESULT, true);
intent.putExtra(Intent.EXTRA_INSTALLER_PACKAGE_NAME,
getApplicationInfo().packageName);
}
startActivityForResult(intent, REQUEST_INSTALL);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
if (requestCode == REQUEST_INSTALL) {
if (VersionUtils.getCurrentVersion() < VersionUtils.ICE_CREAM_SANDWICH)
{
if (pkgExists(xxxxx)) //check to see if the package exists or not
{
//Succeedded
}
else
{
//Failed or canceled
}
}
else
{
if (resultCode == Activity.RESULT_OK)
{
//Succeedded
}
else if (resultCode == Activity.RESULT_CANCELED)
{
//Canceled
}
else
{
//Failed
}
}
}
}
Simple:
Install from native apk
Intent promptInstall = new Intent(Intent.ACTION_VIEW) .setData(Uri.parse("file:///path/to/your.apk")) .setType("application/vnd.android.package-archive"; startActivity(promptInstall); Install from Market Intent goToMarket = new Intent(Intent.ACTION_VIEW) .setData(Uri.parse("market://details?id=com.package.name")); startActivity(goToMarket);