Introduction to xUtils
xUtils contains many practical android tools.
xUtils supports large file uploads, more comprehensive http request protocol support (10 predicates), has more flexible ORM, more event annotation support and is not affected by confusion...
xUitls minimum compatible with Android 2.2 (api level 8)
Currently, xUtils has four main modules:
DbUtils module:
For the orm framework in Android, you can add, delete, modify and check it in one line of code;
Support transactions, closed by default;
You can annotate custom table names, column names, foreign keys, uniqueness constraints, NOT NULL constraints, CHECK constraints, etc. (please annotate table names and column names when you need to be confused);
Support binding of foreign keys, and the foreign key-associated entities are automatically saved or updated when saving them;
Automatically load foreign key associated entities, supporting delayed loading;
Supports chain expression query, and more intuitive query semantics, refer to the introduction below or examples in sample.
ViewUtils module:
The IOC framework in Android can bind UI, resource and event with complete annotation method;
The new event binding method can still work normally after obfuscation using obfuscation tools;
Currently, 20 commonly used event bindings are supported, see the ViewCommonEventListener class and package.
HttpUtils module:
Supports synchronous and asynchronous requests;
Supports uploading of large files, and uploading large files will not be oom;
Supports GET, POST, PUT, MOVE, COPY, DELETE, HEAD, OPTIONS, TRACE, CONNECT requests;
The download supports 301/302 redirection, and supports setting whether to rename the downloaded file according to Content-Disposition;
Requests that return text content (only GET requests are enabled by default) support caching, and can set the default expiration time and the expiration time for the current request.
BitmapUtils module:
When loading a bitmap, you don’t need to consider the image misalignment when the oom and Android containers slide quickly during the bitmap loading process;
Supports loading of network pictures and local pictures;
Memory management uses the lru algorithm to better manage bitmap memory;
It can configure the number of thread loading threads, cache size, cache path, load display animation, etc.
All that said above is to lay the groundwork for the following text, and let’s get to the topic:
The example I wrote is relatively simple, using xutils3.0 to download and update the project
1. First, use the network request to determine whether the version needs to be updated.
2. If you want to update, a pop-up window pops up. I use the Dialog that comes with the system, and display the downloaded version number and downloaded content prompts.
3. When the user clicks to download, start downloading, and displays a horizontal progress bar when downloading.
4. After the download is completed, the progress bar disappears and call the system to install apk
The following is the code. The jar package of xutils used is version 3.3.32
package ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; /** * 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 (savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(.activity_login); ll_update = (Button) findViewById(.btn_login); ll_update.setOnClickListener(new () { @Override public void onClick(View arg0) { // TODO Auto-generated method stub checkUpdate(); } }); try { PackageInfo packageInfo = getPackageManager().getPackageInfo( getPackageName(), 0); nowVersion = ; } catch (NameNotFoundException e) { // TODO Auto-generated catch block (); } } /** * Download update, */ protected void checkUpdate() { // TODO Auto-generated method stub proDialogShow(, "Inquiring..."); RequestParams params = new RequestParams("url"); ().get(params, new <String>() { @Override public void onCancelled(CancelledException arg0) { // TODO Auto-generated method stub } @Override public void onError(Throwable arg0, boolean arg1) { // TODO Auto-generated method stub PDialogHide(); ("Prompt network error"); } @Override public void onFinished() { // TODO Auto-generated method stub } @Override public void onSuccess(String arg0) { // TODO Auto-generated method stub PDialogHide(); try { JSONObject object = new JSONObject(arg0); boolean success = ("succee"); if (success) { String desc = ("desc"); String downloadurl = ("downloadurl"); String versionname = ("versionname"); if ((versionname)) { ("The current version is the latest, no update is required"); } else { // Different, an update prompt dialog box pops upsetUpDialog(versionname, downloadurl, desc); } } } catch (JSONException e) { // TODO Auto-generated catch block (); } } }); } /** * * @param versionname * Name of the version in the address * @param downloadurl * Address of the download package * @param desc * Version description */ protected void setUpDialog(String versionname, final String downloadurl, String desc) { // TODO Auto-generated method stub AlertDialog dialog = new (this).setCancelable(false) .setTitle("download" + versionname + "Version").setMessage(desc) .setNegativeButton("Cancel", null) .setPositiveButton("download", new () { @Override public void onClick(DialogInterface arg0, int arg1) { // TODO Auto-generated method stub setDownLoad(downloadurl); } }).create(); (); } /** * Download package * * @param downloadurl * Downloaded url * */ @SuppressLint("SdCardPath") protected void setDownLoad(String downloadurl) { // TODO Auto-generated method stub RequestParams params = new RequestParams(downloadurl); (true);//Download breakpoint("/mnt/sdcard/"); ().get(params, new <File>() { @Override 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 && ()){ (); } ("Prompt update failed"); } @Override public void onFinished() { // TODO Auto-generated method stub } @Override public void onSuccess(File arg0) { // TODO Auto-generated method stub if(progressDialog!=null && ()){ (); } Intent intent = new Intent(Intent.ACTION_VIEW); (Intent.FLAG_ACTIVITY_NEW_TASK); ((new File(Environment .getExternalStorageDirectory(), "")), "application/-archive"); startActivity(intent); } @Override public void onLoading(long arg0, long arg1, boolean arg2) { // TODO Auto-generated method stub ((int)arg0); ((int)arg1); } @Override public void onStarted() { // TODO Auto-generated method stub ("Start Download"); progressDialog = new ProgressDialog(); (ProgressDialog.STYLE_HORIZONTAL);//Set to horizontal to make bar("Downloading..."); (0); (); } @Override public void onWaiting() { // TODO Auto-generated method stub } }); } private void proDialogShow(Context context, String msg) { pDialog = new ProgressDialog(context); (msg); // (false); (); } private void PDialogHide() { try { if (pDialog != null && ()) { (); } } catch (Exception e) { (); } } }
The above is what the editor introduced to you to use xutils 3.0 for downloading project updates. I hope it will be helpful to everyone. If you have any questions, please leave me a message and the editor will reply to everyone in time. Thank you very much for your support for my website!