Complete Scenario: Command mode + asynchronous execution + cancellation mechanism

Source: Internet
Author: User

Introduction
    • With the development of the project, the code of activity becomes more and more complicated and difficult to maintain. In this way, models such as MVP have been layered to achieve the effect of offloading the activity, thus reducing the coupling of business logic and improving maintainability. From another point of view, this paper makes use of command mode to achieve the goal of slimming activity. Using this scenario, the business logic can also be extracted and executed in the background, while providing a good cancellation mechanism.
Interface
    • Icmd interface, the main API is the EXE method, the return value is the bundle object, the other mtag,mcanceled is used to cancel the related
 Public Abstract  class iCmd {    Private Booleanmcanceled =false;PrivateObject Mtag; Public void Cancel() {mcanceled =true; } Public Boolean isCanceled() {returnmcanceled; } PublicObjectGettag() {returnMtag; } PublicICmdSettag(Object tag) {Mtag = tag;return  This; } Public AbstractBundleEXE()throwsException;}
    • Icmdlistener command Listener interface, OnPreExecute responsible for command start notification, OnPostExecute responsible for the command end notification and bring back the results
publicinterface iCmdListener {    publicvoidonPreExecute();//  public void onProgressUpdate(int progress);    publicvoidonPostExecute(Bundle resultBundle);}
    • Icmdexecutor command Executor, the following code lists only the main API, the specific implementation of the GitHub address
 Public  class icmdexecutor {//For canceling and specific tag-related cmd     Public Static void Cancelall(FinalObject tag)//asynchronously executes the command and executes a callback notification on the main thread Public Static void Exeasync(FinalICMD cmd,FinalIcmdlistener Listener, Object tag)//synchronously executes the command and executes a callback notification on the current thread Public Static void Exesync(iCmd cmd, Icmdlistener Listener, Object tag)}
Usage
    • Implement specific CMD
 Public  class cmdadd extends iCmd {    Private Static FinalString TAG ="Test";PrivateBigInteger A;PrivateBigInteger b; Public Cmdadd(BigInteger A, BigInteger b) { This. A = A; This. B = b; }@Override     PublicBundleEXE()throwsinterruptedexception {log.i (TAG, This+", EXE, tid="+ Thread.CurrentThread (). GetId ()); Thread.Sleep ( the);//Demo for long OperationBundle bundle =NewBundle (); Bundle.putstring ("Result", A.add (b). ToString ());returnBundle }}
    • Call this command
Icmdexecutor.exeasync (NewCmdadd (NewBigInteger ("123"),NewBigInteger ("12345678901234567890123456789012345678901234567890")),NewIcmdlistener () {@Override                     Public void OnPreExecute() {LOG.E (TAG,"OnPreExecute, tid="+ Thread.CurrentThread (). GetId ()); }@Override                     Public void OnPostExecute(Bundle Resultbundle) {LOG.E (TAG,"OnPostExecute, tid="+ Thread.CurrentThread (). GetId () +", resultbundle="+ Resultbundle);if(NULL! = Resultbundle) {String res = resultbundle.getstring ("Result");                        Tvaddresult.settext (RES); }                    }                }, This);
    • Cancel on activity exit
iCmdExecutor.cancelAll(this);
Link
    • Https://github.com/cheyiliu/AsyncCmdForAndroid

Complete Scenario: Command mode + asynchronous execution + cancellation mechanism

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.