回调函数
来源:互联网 发布:怎样在淘宝店上架商品 编辑:程序博客网 时间:2024/04/30 05:56
写一个Button,点击后触发计算结果。
public class MainActivity extends Activity implements View.OnClickListener { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button btn = (Button) findViewById(R.id.btn); btn.setOnClickListener(this); } @Override public void onClick(View v) { //new一个服务器对象 Service service = new Service(); //new一个客户端对象,并把服务器对象的引用传过去,表示要用这个传递过去的服务器进行计算。 Client client = new Client(service); //传递需要计算的数据 client.SendMessage("23", "52"); }}
接口
public interface MyInterface { void Result(int result);}
服务器
public class Service { /** * 接收传递过来的数据进行计算。 * @param myInterface 传递过来的接口 * @param result1 传递过来的数据 * @param result2 传递过来的数据 */ public void Add(MyInterface myInterface,String result1,String result2){ System.out.println("帮我算算"+result1+"+"+result2); //服务器进行计算,得出结果。 int c = Integer.valueOf(result1)+Integer.valueOf(result2); //模仿计算时间 SystemClock.sleep(1000); //服务器计算得出结果后,告诉调用的人。 myInterface.Result(c); }}
客户端
public class Client implements MyInterface { //Service类的引用 private Service service; /** * 把传递过来的Service的类变量赋值给Client中的类变量 * @param service */ public Client(Service service) { this.service = service; } /** * 重写接口中的方法,输出结果。 */ @Override public void Result(int result) { System.out.println("结果是:"+result); } //传递信息的方法 public void SendMessage(String Number1,String Number2){ /** * 服务类调用自身的方法,把接口或者实现接口的类、还有需要处理的信息传递过去。 * 传递过去的接口是用于等会用来回调Result方法的。 */ service.Add(Client.this,Number1,Number2); }}
整个工作流程:在Activity界面,new一个Service,然后在new Client的时候把new出来的service传递过去,意思就是说明要用这个new出来的服务器进行计算服务,然后Client调用SendMessage方法,把要计算的数据传递过去。
public void SendMessage(String Number1,String Number2){ /** * 服务类调用自身的方法,把接口或者实现接口的类、还有需要处理的信息传递过去。 * 传递过去的接口是用于等会用来回调Result方法的。 */ service.Add(Client.this,Number1,Number2); }
看上面的方法,传递过来的数据,又被传入了service的Add方法中,同时被传过去的是实现接口的类,即Client。这样做是为了等会在Service中用传递过去的Client回调Client类中的Result方法。
public class Service { /** * 接收传递过来的数据进行计算。 * @param myInterface 传递过来的接口 * @param result1 传递过来的数据 * @param result2 传递过来的数据 */ public void Add(MyInterface myInterface,String result1,String result2){ System.out.println("帮我算算"+result1+"+"+result2); //服务器进行计算,得出结果。 int c = Integer.valueOf(result1)+Integer.valueOf(result2); //模仿计算时间 SystemClock.sleep(1000); //服务器计算得出结果后,告诉调用的人。 myInterface.Result(c); }}
可以看到传递过来的接口调用了Result方法。又因为Client实现了接口并重写了接口的方法,所以。
/** * 重写接口中的方法,输出结果。 */ @Override public void Result(int result) { System.out.println("结果是:"+result); }
结果是。
08-10 17:53:16.668 30047-30047/com.example.invokedbackfunction I/System.out: 帮我算算23+5208-10 17:53:17.669 30047-30047/com.example.invokedbackfunction I/System.out: 结果是:7508-10 17:53:17.669 30047-30047/com.example.invokedbackfunction I/Choreographer: Skipped 58 frames! The application may be doing too much work on its main thread.
0 0
- .NET 回调函数
- 回调函数
- 回调函数
- 回调函数概念
- 回调函数
- 回调函数
- 回调函数
- 回调函数 相关
- delphi 回调函数
- 回调函数
- 回调函数
- 回调函数学习
- 回调函数
- 关于回调函数
- 透析回调函数
- 回调函数
- 什么是回调函数
- 回调函数
- gsoap入门:C/C++代码生成及编译
- Capacity Scheduler 队列设置
- MYSQL数据库优化
- java nio包主要的类
- 使用WakeLock使Android应用程序保持后台唤醒
- 回调函数
- JSP的内置对象-----request对象
- linux 定时 crontab
- 解决Centos7不能联网且ifconfig出现command not found
- nncq----检测人员 详细页修改版本(完成20%)
- html静态页面转angular动态页面
- redis的通配符和批量删除Key
- ORACLE通过SQL语句创建表空间和用户并授权
- 单选按钮radio点击已选中的,取消选中