在 Android 中, 我们通常需要 Activity 跟 Service 进行通讯, 很多人只知道 Activity 掉用 Service, 却不知道 Service 如何将数据返回给 Activity 其实 Service 返回数据给 Activity 有很多种方法, 1 广播 2 回调 3Activity 每隔几秒去取 Service 的方法等等现在我们只讨论第二种方法回调举个例子, 我们 Activity 里面有两个参数 a 和 b, 这时需要计算 a 和 b 的和, 但是不能在 Activity 里面计算需要在 Service 里面去计算这时计算完之后, 通过 Service 的回调, 将结果返回给 Activity 废话不多说, 直接上代码:
我们先定义一个 Service, 命名为 MyService 首先在 MyService 里面定义一个计算返回的接口当也可以在外面定义
- public interface OnAddCalculateListener{void onAddResultCallback(int value);
- }
然后写两个公开的方法, 一个是注册, 一个是反注册如下
- public void registenerOnAddCalculateListener(OnAddCalculateListener onAddCalculateListener){
- this.onAddCalculateListener=onAddCalculateListener;
- }
- public void unregistenerOnAddCalculateListener(){
- this.onAddCalculateListener = null;
- }
然后写计算方法, 计算结束之后, 通过回调的接口, 把值回调这时候, 如果有其他地方注册了 OnAddCalculateListener 这个回调监听, 便可以收到计算的结果
- public void startAddCalculate(int a,int b){
- if(onAddCalculateListener != null){
- int value = a + b;
- onAddCalculateListener.onAddResultCallback(value);
- }
- }
然后我们看到 Activity, 我们先绑定 MyService
- Intent intent = new Intent(this,MyService.class);
- bindService(intent,conn, Context.BIND_AUTO_CREATE);
然后创建一个 conn 这时如果绑定成功后, 会回调 onServiceConnected 方法
- private ServiceConnection conn = new ServiceConnection() {
- @Override
- public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
- myService = ((MyService.MyBinder) iBinder).getMyService();
- myService.registenerOnAddCalculateListener(onAddCalculateListener);
- }
- @Override
- public void onServiceDisconnected(ComponentName componentName) {
- }
- };
此时我们就可以取到 MyService 的实例, 并且开始注册计算方法返回的回调
- private OnAddCalculateListener onAddCalculateListener = new OnAddCalculateListener(){
- @Override
- public void onAddResultCallback(int value) {
- mTextView.setText(Integer.toString(value));
- }
- };
此时, 如果有回调便直接显示在 TextView 上面
以下是 MainActivity.java 的代码:
- package com.example.admin.myapplication;
- import android.content.ComponentName;
- import android.content.Context;
- import android.content.Intent;
- import android.content.ServiceConnection;
- import android.os.Bundle;
- import android.os.IBinder;
- import android.support.v7.app.AppCompatActivity;
- import android.view.View;
- import android.widget.Button;
- import android.widget.TextView;
- import com.example.admin.myapplication.MyService.OnAddCalculateListener;
- public class MainActivity extends AppCompatActivity implements View.OnClickListener {
- private Button mButton;
- private TextView mTextView;
- private MyService myService =null;
- private int a = 100;
- private int b = 30;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- Intent intent = new Intent(this,MyService.class);
- bindService(intent,conn, Context.BIND_AUTO_CREATE);
- mButton = (Button) findViewById(R.id.btn);
- mTextView = (TextView) findViewById(R.id.text_view);
- mButton.setOnClickListener(this);
- }
- private ServiceConnection conn = new ServiceConnection() {
- @Override
- public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
- myService = ((MyService.MyBinder) iBinder).getMyService();
- myService.registenerOnAddCalculateListener(onAddCalculateListener);
- }
- @Override
- public void onServiceDisconnected(ComponentName componentName) {
- }
- };
- private OnAddCalculateListener onAddCalculateListener = new OnAddCalculateListener(){
- @Override
- public void onAddResultCallback(int value) {
- mTextView.setText(Integer.toString(value));
- }
- };
- @Override
- public void onClick(View view) {
- switch (view.getId()){
- case R.id.btn:
- if(myService != null){
- myService.startAddCalculate(a,b);
- }
- break;
- }
- }
- @Override
- protected void onDestroy() {
- super.onDestroy();
- if(myService != null){
- myService.unregistenerOnAddCalculateListener();
- }
- }
- }
下面是 MyService.java 的代码:
- package com.example.admin.myapplication;
- import android.app.Service;
- import android.content.Intent;
- import android.os.Binder;
- import android.os.IBinder;
- public class MyService extends Service {
- private OnAddCalculateListener onAddCalculateListener;
- @Override
- public IBinder onBind(Intent intent) {
- // TODO: Return the communication channel to the service.
- return new MyBinder();
- }
- public class MyBinder extends Binder{
- public MyService getMyService(){
- return MyService.this;
- }
- }
- public void startAddCalculate(int a,int b){
- if(onAddCalculateListener != null){
- int value = a + b;
- onAddCalculateListener.onAddResultCallback(value);
- }
- }
- public interface OnAddCalculateListener{
- void onAddResultCallback(int value);
- }
- public void registenerOnAddCalculateListener(OnAddCalculateListener onAddCalculateListener){
- this.onAddCalculateListener=onAddCalculateListener;
- }
- public void unregistenerOnAddCalculateListener(){
- this.onAddCalculateListener = null;
- }
- }
来源: https://www.cnblogs.com/suicode/p/8685840.html