How to call function with callbacks in Java like I do it in C#? -
i'm pretty new java need write c# code (this hand-typed prototype illustrate need)
private void parentfunc() { var worker = new workerclass() worker.dowork(e => console.write("progress" + e)); } public class workerclass() { public method dowork(action<int> callback) { (int i=1; i<1000; i++) callback.invoke(i); } } little explanation. i'm using asynctask in android , calling outside processor classed them signal can publishprogress. prefer not put interface on asynctask
since closures not supported yet have use interface , anonymous inner class.
private void parentfunc { workerclass worker = new workerclass(); worker.dowork(new callback<integer>() { public void invoke(integer arg) { system.out.println("progress" + arg); } }); } public class workerclass { public dowork(callback<integer> callback) { (int i=1; i<1000; i++) callback.invoke(i); } } public interface callback<t> { public void invoke(t arg); }
Comments
Post a Comment