public void encryptAsync ( final String data , final Callback callback ) { if ( callback == null ) return ; new Thread ( new Runnable ( ) { @ Override public void run ( ) { try { String encrypt = encrypt ( data ) ; if ( encrypt == null ) { callback . onError ( new Exception ( "Encrypt return null, it normally occurs when you send a null data" ) ) ; } callback . onSuccess ( encrypt ) ; } catch ( Exception e ) { callback . onError ( e ) ; } } } ) . start ( ) ; }
This is a sugar method that calls encrypt method in background it is a good idea to use this one instead the default method because encryption can take several time and with this method the process occurs in a AsyncTask other advantage is the Callback with separated methods one for success and other for the exception