WorkHelper : Processing class
package com.customlistener; import android.location.Location; import android.util.Log; /** * @author piyush * Does data processing. */ public class WorkHelper { private HelperResult helperResult; private String tag="WorkHelper",resultString; private int randomNumber=-1,limit=1000; public WorkHelper(HelperResult helperResult) { this.helperResult=helperResult; } public void doProcessing() { //generate number between 0 to 1000 randomNumber=(int)(Math.random()*limit); Log.d(tag,"doProcessing:"+randomNumber); resultString="New RandomNumber is : "+randomNumber; //post data back to MainActivity helperResult.getResult(resultString); } public static abstract class HelperResult{ public abstract void getResult(String resultString); } }
MainActivity : UI Class
package com.customlistener; import com.customlistener.WorkHelper.HelperResult; import android.app.Activity; import android.app.ProgressDialog; import android.content.Context; import android.os.Bundle; import android.util.Log; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.ProgressBar; import android.widget.TextView; /** * Main Activity * @author piyush */ public class MainActivity extends Activity { private String tag="HelperResult",resultStr=""; private Button startButton; private WorkHelper workHelper; private Context context; private TextView textViewData; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); context=this; workHelper=new WorkHelper(helperResult); startButton=(Button)findViewById(R.id.button1); textViewData=(TextView)findViewById(R.id.textDetail); startButton.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { //generate random number workHelper.doProcessing(); } }); } /** * Create Instance of HelperResult Class * and Implement getResult(). */ HelperResult helperResult=new HelperResult() { @Override public void getResult(String resultString) { Log.d(tag,"in MainActivity"); Log.d(tag,"Result Received :"+resultString); resultStr+=resultString+"\n"; textViewData.setText(resultStr); } }; }Download Source Code