Download Project updates with xutils3.0 _android

Source: Internet
Author: User
Tags file upload stub throwable

Xutils Introduction

Xutils contains a lot of practical Android tools.

Xutils support for large file uploads, more comprehensive HTTP request protocol support (10 predicates), with more flexible ORM, more event annotation support and no confusion impact ...

XUITLS Minimum compatible Android 2.2 (API Level 8)

At present, there are four main modules of Xutils:

Dbutils module:

Android in the ORM Framework, a line of code can be used to increase the deletion of the search;
Support transactions, closed by default;
You can customize the table name, column name, foreign key, uniqueness constraint, NOT NULL constraint, check constraint, etc. (Please note the table name and column name when you need to be confused);
Support binding foreign key, save the entity when the foreign key associated entity automatically save or update;
Automatic loading of foreign key associated entities, support delay loading;
Support chain expression query, more intuitive query semantics, refer to the following introduction or examples in Sample.

Viewutils module:

The IOC framework in Android, full annotation mode can be used for UI, resources and event binding;
The new event binding method, which can still work properly after being confused with the obfuscation tool;
The 20 common event bindings are currently supported, see Viewcommoneventlistener Classes and Package com.lidroid.xutils.view.annotation.event.

Httputils module:

Support synchronous, asynchronous mode of request;
Support large file upload, upload large files will not oom;
Support Get,post,put,move,copy,delete,head,options,trace,connect request;
Download supports 301/302 redirects to support setting whether to rename downloaded files according to Content-disposition;
Requests that return text content (by default only get requests are enabled) support caching, which sets the default expiration time and the expiration time for the current request.

Bitmaputils module:

When loading the bitmap, it is not necessary to consider the phenomenon of the image dislocation appearing during the bitmap loading process and the Oom and the Android container sliding quickly.
Support to load network pictures and local pictures;
Memory management uses LRU algorithm to better manage bitmap memory;
Configurable line Cheng number of threads, cache size, cache path, load display animation, etc...

So many of the above are for the following steps to pave the way, below into the theme:

The writing example is relatively simple, is uses the xutils3.0 to carry on the downloading project update

1. First through the network request, to determine whether the version to update

2. To update, pop a pop-up window, I used the system with the dialog, will download the version number and download the content prompts to show

3. When the user clicks the download, starts to download, the download displays a Level progress bar

4. After the download completes, the progress bar disappears, calls the system installs APK

The following is the Xutils jar package that the code uses is version 3.3.32.

Package com.item.weixin;
Import Java.io.File;
Import org.json.JSONException;
Import Org.json.JSONObject;
Import org.xutils.x;
Import Org.xutils.common.Callback;
Import Org.xutils.http.RequestParams;
Import Android.annotation.SuppressLint;
Import android.app.Activity;
Import Android.app.AlertDialog;
Import Android.app.ProgressDialog;
Import Android.content.Context;
Import Android.content.DialogInterface;
Import android.content.Intent;
Import Android.content.pm.PackageInfo;
Import android.content.pm.PackageManager.NameNotFoundException;
Import Android.net.Uri;
Import Android.os.Bundle;
Import android.os.Environment;
Import Android.view.View;
Import Android.view.Window;
Import Android.widget.Button;
/** * Set xutils download * * @author Administrator */public class Loginactivity extends activity {private Button ll_update;
Private ProgressDialog Pdialog;
Private String nowversion;
Private ProgressDialog ProgressDialog; @Override protected void OnCreate (Bundle savedinstancestate) {//TODO Auto-generated method Stub super.oncreate (savedinstancestate);
Requestwindowfeature (Window.feature_no_title);
Setcontentview (R.layout.activity_login);
Ll_update = (Button) Findviewbyid (R.id.btn_login); Ll_update.setonclicklistener (New View.onclicklistener () {@Override public void OnClick (View arg0) {//TODO Auto-generat
Ed method Stub checkupdate ();
}
}); try {packageinfo packageinfo = Getpackagemanager (). Getpackageinfo (Getpackagename (), 0); nowversion =
Packageinfo.versionname;
catch (Namenotfoundexception e) {//TODO auto-generated catch block E.printstacktrace ();}} /** * Download UPDATE, */protected void Checkupdate () {//TODO auto-generated Method Stub prodialogshow (Loginactivity.this, "querying.")
.");
Requestparams params = new requestparams ("url"); X.http (). Get (params, new callback.commoncallback<string> () {@Override the public void oncancelled ( Cancelledexception arg0) {//TODO auto-generated method stub} @Override public void OnError (Throwable arg0, Boolean arg1 ) {//TODO Auto-generated method Stub pdialoghide ();
SYSTEM.OUT.PRINTLN ("Hint network error"); 
@Override public void onfinished () {//TODO auto-generated a stub} @Override public void onsuccess (String arg0) {
TODO auto-generated Method Stub pdialoghide (); try {Jsonobject object = new Jsonobject (ARG0); Boolean success = Object.getboolean ("Succee"); if (success) {String desc
= object.getstring ("desc");
String DownloadURL = object.getstring ("DownloadURL");
String versionname = object.getstring ("Versionname"); if (Nowversion.equals (Versionname)) {System.out.println ("current version is up to date, not with new"), or else {//different, pop-up update prompt dialog Setupdialog (
Versionname, DownloadURL, DESC);
(Jsonexception e) {//TODO auto-generated catch block E.printstacktrace ();}}
}); /** * * * @param versionname * Address version of the name * @param downloadurl * Download the address of the package * @param desc * Version of the description * * protected void Setupdialog (String versionname, final string downloadurl, String desc) {//TODO auto-generated Method Stub Alertdialog Dialog = new Alertdialog.builder (tHis). Setcancelable (False). Settitle ("Download" + versionname + "version"). Setmessage (DESC). Setnegativebutton ("Cancel", null). Setpositivebutton ("Download", new Dialoginterface.onclicklistener () {@Override public void OnClick (dialoginterface arg0, int ARG1) {//TODO auto-generated Method Stub setdownload (DownloadURL);}}).
Create ();
Dialog.show (); /** * Download Package * * * * @param downloadurl * Download URL */@SuppressLint ("Sdcardpath") protected void Setdownload (String downloadu RL) {//TODO auto-generated method stub requestparams params = new Requestparams (downloadurl); Params.setautorename (True)
;//Breakpoint Download Params.setsavefilepath ("/mnt/sdcard/demo.apk"); X.http (). Get (params, new callback.progresscallback<file> () {@Override the public void oncancelled ( Cancelledexception arg0) {//TODO auto-generated method stub} @Override public void OnError (Throwable arg0, Boolean arg1
{//TODO auto-generated Method stub if (Progressdialog!=null && progressdialog.isshowing ()) {
Progressdialog.dismiss (); } System.out.prinTLN ("Prompt for update failed"); @Override public void onfinished () {//TODO auto-generated a stub} @Override public void onsuccess (File arg0) {/
/TODO auto-generated Method stub if (Progressdialog!=null && progressdialog.isshowing ()) {
Progressdialog.dismiss ();
} Intent Intent = new Intent (Intent.action_view);
Intent.setflags (Intent.flag_activity_new_task); Intent.setdataandtype (uri.fromfile new File (Environment. getExternalStorageDirectory (), demo.apk)), application
/vnd.android.package-archive ");
StartActivity (Intent); @Override public void onloading (long arg0, Long arg1, Boolean arg2) {//TODO auto-generated the method stub progressdialog.
Setmax ((int) arg0);
Progressdialog.setprogress ((int) arg1); @Override public void onstarted () {//TODO auto-generated Method Stub System.out.println ("Start Download"); ProgressDialog = new
ProgressDialog (Loginactivity.this); Progressdialog.setprogressstyle (Progressdialog.style_horizontal),//set to horizontal bar progressdialog.setmessage ("Downloading ...
"); Progressdialog.setpRogress (0);
Progressdialog.show ();
@Override public void onwaiting () {//TODO auto-generated Method stub}}); private void Prodialogshow (context context, String msg) {pdialog = new ProgressDialog (context); Pdialog.setmessage (msg)
;
Pdialog.setcancelable (FALSE);
Pdialog.show (); private void Pdialoghide () {try {if (Pdialog!= null && pdialog.isshowing ()) {Pdialog.dismiss ();}} catch (E
Xception e) {e.printstacktrace ();}} }

The above is a small series to introduce you to download the project with xutils3.0, I hope to help you, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!

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.