【问题标题】:Parse error while installing downloaded .apk file安装下载的 .apk 文件时出现解析错误
【发布时间】:2012-06-23 12:59:36
【问题描述】:

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

我的实施范围是尝试从我有更新的 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);
}

【问题讨论】:

  • 你在主线程上从网络上下载东西。不要这样做。看看 IntentService;这是一个在后台线程上执行操作的服务。
  • 所以你说的是扩展 IntentService 而不是服务权。

标签: android android-layout exception-handling


【解决方案1】:

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

FileOutputStream fos = openFileOutput(file, Context.MODE_PRIVATE)

Context.MODE_WORLD_READABLE。我添加了 INSTALL_PACKAGES 等权限。

【讨论】:

    猜你喜欢
    • 2013-12-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多