2

嗨,两周后,我再次开始研究并努力解决这个错误*解析错误: *解析包有问题。

我的实施范围是尝试从我有更新的 apk 文件的服务器更新我的应用程序,并使用服务通过我的应用程序下载它。现在我可以从该服务器下载文件我可以手动安装它。但我的范围就像从服务器下载文件后它应该自动调用安装应用程序弹出窗口。安装时出现上述错误。我在问这个问题之前尝试过谁在这里问过同样的问题.

这是我的代码:

public class Myservice extends Service {
String versionName;

@Override
public IBinder onBind(Intent intent) {
    // TODO Auto-generated method stub
    return null;
}

@Override
public void onCreate() {
    Toast.makeText(this, "My Service Created", Toast.LENGTH_LONG).show();

}

@Override
public void onDestroy() {
    Toast.makeText(this, "My Service Stopped", Toast.LENGTH_LONG).show();

}

@Override
public void onStart(Intent intent, int startid) {
    try {
        versionName = getPackageManager().getPackageInfo(getPackageName(),
                0).versionName;
    } catch (NameNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }



     DownloadFromUrl();


}

public void DownloadFromUrl() {  //this is the downloader method
      try {
              URL url = new URL("http://61.12.5.34:10140/test/UpateTest.apk");
              String  file ="YeldiUpateTest.apk";

              long startTime = System.currentTimeMillis();

              /* Open a connection to that URL. */
              URLConnection ucon = url.openConnection();

              /*
               * Define InputStreams to read from the URLConnection.
               */
              InputStream is = ucon.getInputStream();
              BufferedInputStream bis = new BufferedInputStream(is);

              /*
               * Read bytes to the Buffer until there is nothing more to read(-1).
               */
              ByteArrayBuffer baf = new ByteArrayBuffer(50);
              int current = 0;
              while ((current = bis.read()) != -1) {
                      baf.append((byte) current);

              }

              /* Convert the Bytes read to a String. */
              FileOutputStream fos = openFileOutput(file, Context.MODE_PRIVATE);
              Log.v("wsd", "write");
              fos.write(baf.toByteArray());
              fos.close();
              Log.d("Download time", "download ready in"
                              + ((System.currentTimeMillis() - startTime) / 1000)
                              + " sec");

              install();
      } catch (IOException e) {
              Log.d("ImageManager", "Error: " + e);
      }
 }

这是我的安装方法():

void install()
{

     File path=getFileStreamPath("UpateTest.apk");

     Intent intent=new Intent(Intent.ACTION_VIEW);
     intent.setDataAndType(Uri.fromFile(path), "application/vnd.android.package-archive");
     intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
     startActivity(intent);
}
4

1 回答 1

3

最后我可以安装从我的服务器下载的apk,没有任何解析错误。我尝试使用外部存储,它没有任何错误发生。将apk存储在内部存储中并想要安装意味着你需要更改你的

FileOutputStream fos = openFileOutput(file, Context.MODE_PRIVATE)

Context.MODE_WORLD_READABLE我添加了类似INSTALL_PACKAGES的权限。

于 2012-06-22T05:09:52.520 回答