• Android中Activity和Service的数据通讯


    在Android中,我们通常需要Activity跟Service进行通讯,很多人只知道Activity掉用Service,却不知道Service如何将数据返回给Activity。其实Service返回数据给Activity有很多种方法,1、广播。2、回调。3、Activity每隔几秒去取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;
    }
    }


  • 相关阅读:
    1001.A+B Format(20)
    大一下学期的自我目标
    re模块3
    re模块2
    re模块
    configParser模块
    logging模块
    hashlib模块
    sys模块
    isinstance函数
  • 原文地址:https://www.cnblogs.com/suicode/p/8685840.html
Copyright © 2020-2023  润新知