下载和链接安装APK [英] Download And Install apk from a link
问题描述
I`am试图从一些链接下载和安装APK,
I`am trying to download and install an apk from some link,
但出于某种原因,我得到一个例外。
but for some reason i get an exception.
我有一个方法downloadfile(),该下载文件并调用
I have one method downloadfile() which downloading the file and a call
要和installFile()方法,它应该在设备安装。
to and installFile() method, which supposed to install it in the device.
约code:
public void downloadFile()
{
String fileName = "someApplication.apk";
MsgProxyLogger.debug(TAG, "TAG:Starting to download");
try
{
URL u = new URL(
"http://10.122.233.22/test/someApplication.apk");
try
{
HttpURLConnection c = (HttpURLConnection) u.openConnection();
try
{
c.setRequestMethod("GET");
c.setDoOutput(true);
try
{
c.connect();
FileOutputStream f = context.openFileOutput(fileName,
context.MODE_WORLD_READABLE);
try
{
InputStream in = c.getInputStream();
byte[] buffer = new byte[1024];
int len1 = 0;
int totsize = 0;
try
{
while ((len1 = in.read(buffer)) > 0)
{
totsize += len1;
f.write(buffer, 0, len1);// .write(buffer);
}
} catch (IOException e)
{
e.printStackTrace();
}
f.close();
MsgProxyLogger.debug(TAG, TAG
+ ":Saved file with name: " + fileName);
InstallFile(fileName);
} catch (IOException e)
{
e.printStackTrace();
}
} catch (IOException e)
{
e.printStackTrace();
}
} catch (ProtocolException e)
{
e.printStackTrace();
}
} catch (IOException e)
{
e.printStackTrace();
}
} catch (MalformedURLException e)
{
e.printStackTrace();
}
}
和这是安装文件的方法:
and this is the install file method:
private void InstallFile(String fileName)
{
MsgProxyLogger.debug(TAG, TAG + ":Installing file " + fileName);
String src = String.format(
"file:///data/data/com.test/files/",
fileName);
Uri mPackageURI = Uri.parse(src);
PackageManager pm = context.getPackageManager();
int installFlags = 0;
try
{
PackageInfo pi = pm.getPackageInfo("com.mirs.agentcore.msgproxy",
PackageManager.GET_UNINSTALLED_PACKAGES);
if (pi != null)
{
MsgProxyLogger.debug(TAG, TAG + ":replacing " + fileName);
installFlags |= PackageManager.REPLACE_EXISTING_PACKAGE;
}
} catch (NameNotFoundException e)
{
}
try
{
// PackageInstallObserver observer = new PackageInstallObserver();
pm.installPackage(mPackageURI);
} catch (SecurityException e)
{
//!!!!!!!!!!!!!here i get an security exception!!!!!!!!!!!
MsgProxyLogger.debug(TAG, TAG + ":not permission? " + fileName);
}
这是异常的详细信息: 无论用户10057也不是当前进程android.permission.INSTALL_PACKAGES。
this is the exception details: "Neither user 10057 nor current process has android.permission.INSTALL_PACKAGES".
和我在我的主要的应用程序设置在清单中该权限。
and i have set in my main app that permission in the manifest.
任何人有任何想法?
感谢
射线。
推荐答案
您不能安装的APK这种方式 - 只属于系统固件的一部分,应用程序可以做到这一点。
You cannot install APKs that way -- only applications that are part of the system firmware can do that.
您应该可以使用 ACTION_VIEW
意图
,与MIME类型的应用/ vnd.android.package存档
和乌里
指向您的文件。请注意,这可能无法在不具备允许非市场将安装检查设备的正常工作。
You should be able to use an ACTION_VIEW
Intent
, with a MIME type of application/vnd.android.package-archive
and a Uri
pointing to your file. Note that this may not work on devices that do not have "allow non-Market installs" checked.
这篇关于下载和链接安装APK的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!