我在SourceForge(https://sourceforge.net/projects/pokedroid/)上托管了一个应用。我决定添加一个按钮,直接从CVS服务器下载最新版本的应用程序。 .apk下载很好,但是当我尝试安装它时,软件包安装程序提供了“无法解析软件包”错误。我正在使用的代码:
private class DownloadAndInstall extends AsyncTask<String, Integer, Boolean>
{
protected Boolean doInBackground(String... derp)
{
String ur=derp[0];
String fileName=derp[1];
ByteArrayBuffer baf = new ByteArrayBuffer(50);
try
{
URL url = new URL(ur);
URLConnection ucon = null;
ucon = url.openConnection();
InputStream is = ucon.getInputStream();
BufferedInputStream bis = new BufferedInputStream(is);
int current = 0;
int updateCount=0;
while ((current = bis.read()) != -1)
{
if(updateCount==256)
{
publishProgress(baf.length());
updateCount=0;
}
baf.append((byte) current);
updateCount++;
}
FileOutputStream fos = PokeDroid.openFileOutput(fileName, Context.MODE_WORLD_READABLE);
fos.write(baf.toByteArray());
fos.close();
} catch (Exception e) {
Log.e("pokedroid", e.toString());
}
MessageDigest digest = null;
try {
digest = java.security.MessageDigest.getInstance("MD5");
} catch (NoSuchAlgorithmException e) {
Log.e("pokedroid", e.toString());
}
digest.update(baf.toByteArray());
byte[] h = digest.digest();
if(baf.length()==0)
return null;
String[] fileList=fileList();
boolean exists=false;
for(String i:fileList)
if(i.equals("updatehash.md5"))
exists=true;
String newHash=new String(h);
Log.e("pokedroid", "new="+newHash);
if(exists)
{
try
{
String oldHash=loadObject("updatehash.md5");
Log.e("pokedroid", "old="+oldHash);
if(oldHash.equals(newHash))
return false;
else
saveObject(newHash, "updatehash.md5");
}
catch (Exception e)
{
Log.e("pokedroid",e.toString());
}
}
else
{
try {
saveObject(newHash, "updatehash.md5");
} catch (IOException e) {
Log.e("pokedroid",e.toString());
}
}
return true;
}
protected void onProgressUpdate(Integer...integers)
{
p.setMessage("Downloading update...\n"+integers[0]/1000+"kb downloaded so far.");
}
protected void onPostExecute(Boolean b)
{
if(b==null)
{
noConnection.show();
deleteFile("PokeDroid.apk");
p.dismiss();
return;
}
if(!b)
noNewUpdate.show();
else
{
Intent intent=new Intent();
intent.setAction(android.content.Intent.ACTION_VIEW);
intent.setDataAndType(Uri.parse("file:///data/data/com.games.pokedroid/files/PokeDroid.apk"), "application/vnd.android.package-archive");
startActivity(intent);
deleteFile("PokeDroid.apk");
}
p.dismiss();
}
public void saveObject(String obj, String filename) throws IOException
{
FileOutputStream fos = openFileOutput(filename,Context.MODE_PRIVATE);
ObjectOutputStream out = new ObjectOutputStream(fos);
out.writeObject(obj);
out.close();
fos.close();
}
public String loadObject(String filename) throws StreamCorruptedException, IOException, ClassNotFoundException
{
FileInputStream fis=openFileInput(filename);
ObjectInputStream in=new ObjectInputStream(fis);
String out=(String) in.readObject();
in.close();
fis.close();
return out;
}
}
当然,这是我的Activity中的子类。我做错了什么?
答案 0 :(得分:3)
我可以在您的代码中看到一个问题:
startActivity(intent);
deleteFile("PokeDroid.apk");
此代码发送intent,然后删除该文件。请注意,startActivity
是异步函数。即它发送请求,但不等待该请求的完成。因此,当Application Installer Activity实际收到intent时(让我们这样调用),您的.apk
文件已被删除。
如何修复:
您需要用以下代码替换这两行代码:
startActivityForResult(intent);
然后在onActivityResult
函数:
deleteFile("PokeDroid.apk");
答案 1 :(得分:1)
您是否检查过您的手机已配置为接受第三方应用?
是apk签名?