Android server and client design APK upgrade

Source: Internet
Author: User

Http://blog.csdn.net/muyu114/article/details/6623509

Server Design:
There should be many design methods. here is one of my methods:
A. Create a folder under the server project to store the APK installation file:
B. Create a resource file named apkversion. properties under SRC. The attributes are defined as follows:
View plaincopy to clipboardprint?
Apkversion = 1 saved version
Apksize = 550kb
Apkpath = http: // xx8080/srv/APK/demo.apk upgrade File
Apkversion = 1 saved version
Apksize = 550kb
Apkpath = http: // xx8080/srv/APK/demo.apk upgrade File
C. Define a servlet to obtain information in the resource:
Definition class: updateapkservlet. Java
View plaincopy to clipboardprint?
// Obtain resource file information
Static {
Properties PPT = new properties ();
Try {
PPT. Load (updateapkservlet. Class. getresourceasstream ("/apkversion. properties "));
Apkversion = PPT. getproperty ("apkversion ");
Apksize = PPT. getproperty ("apksize ");
Apkpath = PPT. getproperty ("apkpath ");}
Catch (exception e ){
E. printstacktrace ();
}
}
// Obtain resource file information
Static {
Properties PPT = new properties ();
Try {
PPT. Load (updateapkservlet. Class. getresourceasstream ("/apkversion. properties "));
Apkversion = PPT. getproperty ("apkversion ");
Apksize = PPT. getproperty ("apksize ");
Apkpath = PPT. getproperty ("apkpath ");}
Catch (exception e ){
E. printstacktrace ();
}
}
Obtain the resource, and then generate a JSON string to return to the client for processing. Note: When the Client Version is updated, the server only needs to copy the APK file to the APK directory and update the information in the apkversion. properties file. Remember.
Client design:
1. The client first obtains the server version information (obtained through HTTP ).
2. See the following code for how to obtain the version information of the local client:
View plaincopy to clipboardprint?
/**
* Obtain the version information of the local application.
* @ Return
*/
Private int getapkversion (){
// Determine the APK version
Int sdcardversion = 0;
String apkfilepath = "sdcard/demo.apk"; // installation package path
Packagemanager PM = getpackagemanager ();

Packageinfo info = PM. getpackagearchiveinfo (apkfilepath, packagemanager. get_activities );
If (info! = NULL ){
Sdcardversion = info. versioncode; // obtain the version information.
Log. V (TAG, "version =" + sdcardversion );
}
Return sdcardversion;
}
/**
* Obtain the version information of the local application.
* @ Return
*/
Private int getapkversion (){
// Determine the APK version
Int sdcardversion = 0;
String apkfilepath = "sdcard/demo.apk"; // installation package path
Packagemanager PM = getpackagemanager ();

Packageinfo info = PM. getpackagearchiveinfo (apkfilepath, packagemanager. get_activities );
If (info! = NULL ){
Sdcardversion = info. versioncode; // obtain the version information.
Log. V (TAG, "version =" + sdcardversion );
}
Return sdcardversion;
}
3. Compare versions. if the version is the same, update is not performed. Here, we will insert the Client Version settings: The Client Version is set in androidmanifest. in the XML file, there are two attributes that can be used to set version information. Android: versioncode = "1" version number Android: versionname = "1.1" version name must correspond to the server.
4. required permission settings
View plaincopy to clipboardprint?
Sdcard access permission: Uses-Permission Android: Name = "android. Permission. write_external_storage"
Network access permission: Uses-Permission Android: Name = "android. Permission. Internet"
Sdcard access permission: Uses-Permission Android: Name = "android. Permission. write_external_storage"
Network access permission: Uses-Permission Android: Name = "android. Permission. Internet"
5. Update and install. When you click an application, check for updates. For more information about the code, see:
// Prompt in the pop-up box
View plaincopy to clipboardprint?
Public handler = new handler (){
Public void handlemessage (Message MSG ){
Super. handlemessage (MSG );
Dialog dialog = new alertdialog. Builder (mainactivity. This). settitle ("system update"). setmessage ("new version found, please update! ")
// Set content
. Setpositivebutton ("OK", // set the OK button
New dialoginterface. onclicklistener (){
@ Override
Public void onclick (dialoginterface dialog, int which ){
Pbar = new progressdialog (mainactivity. This );
Pbar. settitle ("downloading ");
Pbar. setmessage ("Please wait ...");
Pbar. setprogressstyle (progressdialog. style_spinner );
Downfile (apkpath );
}
}). Setnegativebutton ("cancel", new dialoginterface. onclicklistener (){
Public void onclick (dialoginterface dialog, int whichbutton ){
// Click "cancel ".
}
}). Create (); // create
// Display dialog box
Dialog. Show ();
}
};
Public handler = new handler (){
Public void handlemessage (Message MSG ){
Super. handlemessage (MSG );
Dialog dialog = new alertdialog. Builder (mainactivity. This). settitle ("system update"). setmessage ("new version found, please update! ")
// Set content
. Setpositivebutton ("OK", // set the OK button
New dialoginterface. onclicklistener (){
@ Override
Public void onclick (dialoginterface dialog, int which ){
Pbar = new progressdialog (mainactivity. This );
Pbar. settitle ("downloading ");
Pbar. setmessage ("Please wait ...");
Pbar. setprogressstyle (progressdialog. style_spinner );
Downfile (apkpath );
}
}). Setnegativebutton ("cancel", new dialoginterface. onclicklistener (){
Public void onclick (dialoginterface dialog, int whichbutton ){
// Click "cancel ".
}
}). Create (); // create
// Display dialog box
Dialog. Show ();
}
};
// Download
View plaincopy to clipboardprint?
/**
* Download APK file by URL
* @ Param URL
*/
Public void downfile (final string URL ){
Pbar. Show ();
New thread (){
Public void run (){
Httpclient client = new defaulthttpclient ();
// Params [0] indicates the URL of the connection.
Httpget = new httpget (URL );
Httpresponse response;
Try {
Response = client.exe cute (get );
Httpentity entity = response. getentity ();
Long length = entity. getcontentlength ();
Inputstream is = entity. getcontent ();
Fileoutputstream = NULL;
If (is! = NULL ){
File file = new file (environment. getexternalstoragedirectory (), "demo.apk ");
Fileoutputstream = new fileoutputstream (File );
Byte [] Buf = new byte [1, 1024];
Int CH =-1;
Int COUNT = 0;
While (CH = is. Read (BUF ))! =-1 ){
// Baos. Write (BUF, 0, CH );
Fileoutputstream. Write (BUF, 0, CH );
Count + = CH;
If (length> 0 ){
}
}
}
Fileoutputstream. Flush ();
If (fileoutputstream! = NULL ){
Fileoutputstream. Close ();
}
Down ();
} Catch (clientprotocolexception e ){
E. printstacktrace ();
} Catch (ioexception e ){
E. printstacktrace ();
}
}
}. Start ();
}
Public void down (){
Handler. Post (New runnable (){
Public void run (){
Pbar. Cancel ();
Update ();
}
});
}
/**
* Download APK file by URL
* @ Param URL
*/
Public void downfile (final string URL ){
Pbar. Show ();
New thread (){
Public void run (){
Httpclient client = new defaulthttpclient ();
// Params [0] indicates the URL of the connection.
Httpget = new httpget (URL );
Httpresponse response;
Try {
Response = client.exe cute (get );
Httpentity entity = response. getentity ();
Long length = entity. getcontentlength ();
Inputstream is = entity. getcontent ();
Fileoutputstream = NULL;
If (is! = NULL ){
File file = new file (environment. getexternalstoragedirectory (), "demo.apk ");
Fileoutputstream = new fileoutputstream (File );
Byte [] Buf = new byte [1, 1024];
Int CH =-1;
Int COUNT = 0;
While (CH = is. Read (BUF ))! =-1 ){
// Baos. Write (BUF, 0, CH );
Fileoutputstream. Write (BUF, 0, CH );
Count + = CH;
If (length> 0 ){
}
}
}
Fileoutputstream. Flush ();
If (fileoutputstream! = NULL ){
Fileoutputstream. Close ();
}
Down ();
} Catch (clientprotocolexception e ){
E. printstacktrace ();
} Catch (ioexception e ){
E. printstacktrace ();
}
}
}. Start ();
}
Public void down (){
Handler. Post (New runnable (){
Public void run (){
Pbar. Cancel ();
Update ();
}
});
}
// Update and upgrade
View plaincopy to clipboardprint?
Public void Update (){
Intent intent = new intent (intent. action_view );
Intent. setdataandtype (URI. fromfile (new file ("/sdcard/demo.apk"), "application/vnd. Android. Package-Archive ");
Startactivity (intent );
}
Public void Update (){
Intent intent = new intent (intent. action_view );
Intent. setdataandtype (URI. fromfile (new file ("/sdcard/demo.apk"), "application/vnd. Android. Package-Archive ");
Startactivity (intent );
}

Related Article

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.