Thoughts:
When entering the program, first send a request to the background server. The server sends the Android Application version number (updated version) in the current server together with the download URL. compare the updated version with the local version. If the updated version is different, update the version. After the application is downloaded, use the system program to open the downloaded version. because the application size is only about 2 MB, no breakpoint transmission is used.
Note:
The updated version of the app on the server must be the same as the signature used by the app on your mobile phone. (otherwise, after downloading and installing the app, only the information displayed after opening and completing the app is displayed, not opened, so your program update is still unsuccessful)
/* Download the APK from the Network */
Private void begintodownloadapk (){
If (ishassdcard ){
APK = httputils. getsdfileobject (launchactivity. This, apkname );
Try {
URL url = new URL (URLs );
Try {
Httpurlconnection conn = (httpurlconnection) URL
. Openconnection ();
Inputstream is = conn. getinputstream ();
Fileoutputstream Fos = new fileoutputstream (APK );
Byte [] buffer = new byte [1024];
Conn. Connect ();
Int temp
=-1;
Int I =
1;
Int download
= 0;
Int average
= (INT) (size
/Counter );
If (conn. getresponsecode ()> =
(400 ){
Log. V ("tag", "Download APK out time ");
} Else {
While (temp =
Is. Read (buffer ))! =
-1 ){
FOS. Write (buffer, 0, temp );
FOS. Flush ();
Download =
Download +
Temp;
If (download> =
(Average *
I )){
Handler. sendemptymessage (get_download );
I ++;
}
}
}
Conn. Disconnect ();
FOS. Close ();
Is. Close ();
Downfinish ();
} Catch (ioexception e ){
// Todo auto-generated Catch Block
Handler. sendemptymessage (get_fail );
E. printstacktrace ();
}
} Catch (malformedurlexception e ){
// Todo auto-generated Catch Block
E. printstacktrace ();
}
}
}
/* Start the installer after the download is complete */
Private void openfile (){
// Todo auto-generated method stub
Log. D ("tag", APK. getabsolutepath ());
Intent intent = new intent ();
Intent. addflags (intent. flag_activity_new_task );
Intent. setaction (Android. content. Intent. action_view );
Intent. setdataandtype (URI. fromfile (APK ),
"Application/vnd. Android. Package-Archive ");
Startactivity (intent );
Updatedownloadfinishpref ();
}
Run the program, compile an APK file, and create your own private key. Keep your private key and add the same private key to future versions, in this way, you can update the version with the same private key as the current version.
Create a private key:
Right-click the project name in ecplise, click Android tools, and then export Android application. The following screen is displayed.
Click Next
Because it is the first to create a private key. Select the second key, then select a place to place the private key, enter the password, and then click Next
Fill in the information according to your actual situation, and click Next to continue, and a private key has been created. at this time, your APK application (version 1.0) has been exported to a directory, you can put this application in the server.
After N years, your application has been updated. You should first run it and then add the same private key to your current application. as follows:
In ecplise, right-click the project name, click Android tools, and then export Android Application. When this screen is displayed, select an existing private key, that is, the first option.
Enter Password
Enter the password you entered last time and press next. In this way, your APK application (Version 2.0) is already in your directory, you can add version 2.0 to the server to replace your previous version 1.0.
In this way, the user's mobile phone is using version 1.0. by checking that your version is 2.0, and then downloading it, You can automatically install the program. No other problems will occur.
If a package parsing error occurs:
It turns out that your package is not completed yet. verification Method: You can manually run to the/sdcard download directory and install this APK manually. If it fails, your APK is not completed. if the package is successfully downloaded, there is a parsing error after the APK package is downloaded. When you manually run to the SD card for installation, the package has been downloaded, at this point, you cannot figure out why the package Parsing is incorrect.
If the last step of the download and automatic update installation is displayed, only the "finish" button is disabled.
Your private key is incorrect. The current version is not the same as the downloaded version, so it cannot be opened. The installation is also a white installation.