12

重複の可能性:
Android:プログラムで.apkをインストールします

Androidアプリケーションを更新する必要があります。プログラムの内部で、新しいバージョンをダウンロードします。現在のバージョンを(プログラムで)ダウンロードされた新しいバージョンに置き換えるにはどうすればよいですか?

URL url = new URL("http://www.mySite.com/myFolder/myApp.apk");
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
try 
{
    FileOutputStream fos = this.getApplicationContext().openFileOutput("myApp.apk", Context.MODE_WORLD_READABLE|Context.MODE_WORLD_WRITEABLE);

    InputStream in = new BufferedInputStream(urlConnection.getInputStream());
    BufferedReader br = new BufferedReader(new InputStreamReader(in, "UTF-8"));

    StringBuilder  sb = new StringBuilder();

    byte[] buffer = new byte[8192];
    int len;
    while ((len = in.read(buffer)) != -1) 
    {
       // EDIT - only write the bytes that have been written to
       // the buffer, not the whole buffer
       fos.write(buffer, 0, len);  //  file to save app
    }
    fos.close();

    ....     here I have the file of new app, now I need use it
4

1 に答える 1

17

更新された apk のパッケージ名が同じで、同じキーで署名されている場合は、デフォルトの Android インストーラーを呼び出すインテントを送信できます。インストールされた apk は上書きされます。

Intent intent = new Intent(Intent.ACTION_VIEW);
Uri uri = Uri.fromFile(new File(pathToApk));
intent.setDataAndType(uri, "application/vnd.android.package-archive");
startActivity(intent);
于 2012-01-28T14:15:50.797 に答える