Android Auto Update + IIS7 add apk mime

Source: Internet
Author: User

If the apk file is placed under IIS, you need to add the mime of the APK, otherwise the following error will occur

You can add MIME mappings on IIS

. apk

Application/vnd.android

The following transfer is from: http://www.cnblogs.com/coolszy/archive/2012/04/27/2474279.html

As a result of open-source Android projects, there are more than n Android software market. In order to let us develop the software has more users, we need to release to n multi-market, after the software upgrade, we must also go to the Android Market update, to increase our workload. So we need to add Automatic Updates to our Android apps.

Now that we have Automatic updates, we first have to let our app know if there is a new version of the software, so we can place the configuration file on our website and store the version information of the software:

<update>    <version>2</version>    <name>baidu_xinwen_1.1.0</name>    < Url>http://gdown.baidu.com/data/wisegame/f98d235e39e29031/baiduxinwen.apk</url></update>

Here I am using an XML file that is easy to read. Because the content of the XML file is relatively small, the parsing of the file can be done in DOM mode:

<update>    <version>2</version>    <name>baidu_xinwen_1.1.0</name>    < Url>http://gdown.baidu.com/data/wisegame/f98d235e39e29031/baiduxinwen.apk</url></update>

public class Parsexmlservice
{
Public hashmap<string, string> parsexml (InputStream instream) throws Exception
{
hashmap<string, string> HashMap = new hashmap<string, string> ();

Instantiate a document builder factory
Documentbuilderfactory factory = Documentbuilderfactory.newinstance ();
Get a document builder from the document builder factory
Documentbuilder builder = Factory.newdocumentbuilder ();
Build a document instance from the document builder through a document
Document document = Builder.parse (instream);
Gets the XML file root node
Element root = Document.getdocumentelement ();
Get all child nodes
NodeList childNodes = Root.getchildnodes ();
for (int j = 0; J < Childnodes.getlength (); j + +)
{
Traversing child nodes
Node Childnode = (node) childnodes.item (j);
if (childnode.getnodetype () = = Node.element_node)
{
Element childelement = (element) Childnode;
Version number
if ("Version". Equals (Childelement.getnodename ()))
{
Hashmap.put ("Version", Childelement.getfirstchild (). Getnodevalue ());
}
Software name
else if (("Name". Equals (Childelement.getnodename ())))
{
Hashmap.put ("Name", Childelement.getfirstchild (). Getnodevalue ());
}
//
else if (("url". Equals (Childelement.getnodename ())))
{
Hashmap.put ("url", Childelement.getfirstchild (). Getnodevalue ());
}
}
}
return HASHMAP;
}
}

With the Parsexml () method, we can get the version, the file name, and the application on the server. Then we need to get the version information that we have applied to our phone:

/** * Get software version number *  * @param context * @return */private int Getversioncode (context context) {    int versioncode = 0;
   
    try    {        //Get software version number,        Versioncode = Context.getpackagemanager (). Getpackageinfo ("Com.szy.update", 0). Versioncode;    } catch (namenotfoundexception e)    {        e.printstacktrace ();    }    return versioncode;}
   

By this method we obtain the versioncode corresponding to Androidmanifest.xml under Android:versioncode. The Android:versioncode and Android:versionname two properties represent the version number, and the version name, respectively. Versioncode is an integer, and Versionname is a string. Because Versionname is for the user to see, not too easy to compare size, upgrade check, you can check versioncode. Compare the app version on the acquired phone with the version on the server, and the application will determine if the software needs to be updated.

Processing flow

Working with Code

Package Com.szy.update;import Java.io.file;import Java.io.fileoutputstream;import java.io.ioexception;import Java.io.inputstream;import Java.net.httpurlconnection;import Java.net.malformedurlexception;import Java.net.URL; Import Java.util.hashmap;import Android.app.alertdialog;import Android.app.dialog;import Android.app.alertdialog.builder;import Android.content.context;import Android.content.dialoginterface;import Android.content.intent;import Android.content.dialoginterface.onclicklistener;import Android.content.pm.packagemanager.namenotfoundexception;import Android.net.uri;import android.os.Environment; Import Android.os.handler;import android.os.message;import android.view.layoutinflater;import Android.view.View; Import Android.widget.progressbar;import android.widget.toast;/** * @author coolszy * @date 2012-4-26 * @blog/HTTP/    Blog.92coding.com */public class updatemanager{/* Download in */private static final int DOWNLOAD = 1; /* Download end */private static final int download_finish= 2;    /* Save parsed XML information */hashmap<string, string> mhashmap;    /* Download Save path */private String Msavepath;    /* Record the number of progress bars */private int progress;    /* Whether to cancel the update */private Boolean cancelupdate = false;    Private Context Mcontext;    /* Update progress bar */private ProgressBar mprogress;    Private Dialog Mdownloaddialog; Private Handler Mhandler = new Handler () {public void Handlemessage (Message msg) {switch (ms G.what) {//Downloading case DOWNLOAD://Setting progress bar position MPROGRESS.SETPR                Ogress (progress);            Break                Case Download_finish://Installation file installapk ();            Break            Default:break;    }        };    };    Public Updatemanager (Context context) {This.mcontext = context;            }/** * Detect software Update */public void checkupdate () {if (Isupdate ()) {//Show prompt dialog box SHownoticedialog ();        } else {Toast.maketext (Mcontext, R.string.soft_update_no, Toast.length_long). Show (); }}/** * Check if the software has an updated version * * @return */Private Boolean isupdate () {//Get current software version I        NT Versioncode = Getversioncode (Mcontext); Put the version.xml on the network and get the file information InputStream instream = ParseXmlService.class.getClassLoader (). getResourceAsStream ("ve        Rsion.xml "); Parse the XML file.        Because the XML file is small, parsing parsexmlservice service = new Parsexmlservice () using the DOM method;        try {mhashmap = Service.parsexml (instream);        } catch (Exception e) {e.printstacktrace ();            } if (null! = mhashmap) {int servicecode = integer.valueof (Mhashmap.get ("version"));            Version determines if (Servicecode > Versioncode) {return true;    }} return false; }/** * Get the SOFTWARE version number * * @param context * @return */private int Getversioncode (context context) {int versioncode = 0; try {//Gets the software version number, corresponding to Androidmanifest.xml under Android:versioncode Versioncode = Context.getpackagemanager (). Getp    Ackageinfo ("Com.szy.update", 0). Versioncode;    } catch (Namenotfoundexception e) {e.printstacktrace ();    } return Versioncode;} /** * Show Software Updates dialog box */private void Shownoticedialog () {//Construct dialog box Alertdialog.builder Builder = NE        W Builder (Mcontext);        Builder.settitle (R.string.soft_update_title);        Builder.setmessage (R.string.soft_update_info);             Update Builder.setpositivebutton (R.STRING.SOFT_UPDATE_UPDATEBTN, New Onclicklistener () {@Override                public void OnClick (Dialoginterface dialog, int which) {Dialog.dismiss ();            Show Download dialog box Showdownloaddialog ();        }        }); Update Builder.setnegativebutton later (R.string.soft_update_later, newOnclicklistener () {@Override public void OnClick (Dialoginterface dialog, int which)            {Dialog.dismiss ();        }        });        Dialog Noticedialog = Builder.create ();    Noticedialog.show (); /** * Show software Download dialog box */private void Showdownloaddialog () {//Construct software Download dialog box Alertdialog.builder b        Uilder = new Builder (mcontext);        Builder.settitle (r.string.soft_updating);        Add a progress bar to the download dialog final Layoutinflater inflater = Layoutinflater.from (Mcontext);        View v = inflater.inflate (r.layout.softupdate_progress, NULL);        Mprogress = (ProgressBar) V.findviewbyid (r.id.update_progress);        Builder.setview (v);            Cancel Update Builder.setnegativebutton (R.string.soft_update_cancel, New Onclicklistener () {@Override                public void OnClick (Dialoginterface dialog, int which) {Dialog.dismiss ();          Set Cancellation status      CancelUpdate = true;        }        });        Mdownloaddialog = Builder.create ();        Mdownloaddialog.show ();    Now document DOWNLOADAPK (); }/** * Download apk file */private void downloadapk () {//start new thread download software new Downloadapkthread (). Start (    ); /** * Download File thread * * @author coolszy * @date 2012-4-26 * @blog http://blog.92coding.com */priv            Ate class Downloadapkthread extends Thread {@Override public void run () {try {//Determines if the SD card exists and has read and write permissions if (Environment.getexternalstoragestate (). Equals (environment.me dia_mounted)) {//Get the path to the memory card String Sdpath = Environment.getexternalst                    Oragedirectory () + "/";                    Msavepath = Sdpath + "Download";                    URL url = new URL (mhashmap.get ("url")); Create connection HttpURLConnection conn = (Httpurlconnection) url.openconnection ();                    Conn.connect ();                    Get file size int length = Conn.getcontentlength ();                    Create an input stream inputstream is = Conn.getinputstream ();                    File File = new file (Msavepath);                    Determine if the file directory exists if (!file.exists ()) {File.mkdir ();                    } File Apkfile = new file (Msavepath, Mhashmap.get ("name"));                    FileOutputStream fos = new FileOutputStream (apkfile);                    int count = 0;                    Cache byte buf[] = new byte[1024];                        Write to file do {int numread = Is.read (BUF);                        Count + = Numread;                        Calculate the progress bar position progress = (int) ((float) count/length) * 100); Update progress                       Mhandler.sendemptymessage (DOWNLOAD); if (numread <= 0) {//download complete Mhandler.sende                            Mptymessage (Download_finish);                        Break                    }//Write file Fos.write (buf, 0, Numread);                    } while (!cancelupdate);//Click Cancel to stop the download.                    Fos.close ();                Is.close ();            }} catch (Malformedurlexception e) {e.printstacktrace ();            } catch (IOException e) {e.printstacktrace ();        }//Cancel Download dialog box showing Mdownloaddialog.dismiss ();    }    }; /** * Install apk file */private void installapk () {File Apkfile = new file (Msavepath, Mhashmap.get ("name"))        ;        if (!apkfile.exists ()) {return; }//install APK file via intent        Intent i = new Intent (Intent.action_view);        I.setdataandtype (Uri.parse ("file://" + apkfile.tostring ()), "application/vnd.android.package-archive");    Mcontext.startactivity (i); }}

Check if the emulator sdcard has a download file:

Android Auto Update + IIS7 add apk mime

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.