Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android application self-update [duplicate]

Possible Duplicate:
Android: install .apk programmatically

I need to update my android application. Internally the program, I download the new version. How can I replace the current version by that new was downloaded (programmatically)?

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
like image 306
nonickh Avatar asked Jan 28 '12 14:01

nonickh


1 Answers

If the updated apk has the same package name and is signed with the same key you can just send a intent which will call a default android installer. The installed apk will be overriden.

Intent intent = new Intent(Intent.ACTION_VIEW);
Uri uri = Uri.fromFile(new File(pathToApk));
intent.setDataAndType(uri, "application/vnd.android.package-archive");
startActivity(intent);
like image 162
Alexei Avatar answered Oct 30 '22 11:10

Alexei