Estoy tratando de descargar e instalar un apk desde algún enlace,Descargar e instalar apk desde un enlace
pero por alguna razón me sale una excepción.
que tienen un método DownloadFile() que descargar el archivo y una llamada
a Installfile y el método(), lo que supuso para instalarlo en el dispositivo.
algo de código:
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();
}
}
y este es el método de archivo de instalación:
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);
}
Ésta es la información de excepción: "10057 ni actual proceso de Ni usuario tiene android.permission.INSTALL_PACKAGES" .
y he establecido en mi aplicación principal ese permiso en el manifiesto.
alguien tiene alguna idea?
gracias,
ray.
su código es demasiado torpe, no hay necesidad de detectar tantas excepciones, solo deben usarse en casos excepcionales –