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
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 }}
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);
iCmdExecutor.cancelAll(this);
Link
- Https://github.com/cheyiliu/AsyncCmdForAndroid
Complete Scenario: Command mode + asynchronous execution + cancellation mechanism