AIDL实例

几句话:
1.此实例实际上就是一个求和计算器;
2.客户端是位于前台的Activity,用于数据的输入输出;
3.服务器端是在后台的Service,用于数据的计算;
4.两者处于不同的进程中,通过AIDL进行数据交流。

开始code:
1.首先创建AIDL:
在Android Studio的app层右键新建AIDL文件,命名为:ICalculate,此时会自动生成aidl的包,包中有ICalculate.aidl文件。代码如下:

// ICalculate.aidl
package com.example.ubuntu.aidlcalculatedemo;

// Declare any non-default types here with import statements

interface ICalculate {
    /**
     * Demonstrates some basic types that you can use as parameters
     * and return values in AIDL.
     */
   double doCalculate(double a,double b);
}

代码很简单,定义了一个方法doCalculate(double a , double b)。

  1. 创建后台Service:
    主要代码是重写doCalculate方法,并返回一个Binder对象给调用者,代码如下:
package com.example.ubuntu.aidlcalculatedemo;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.os.RemoteException;

public class CalculateService extends Service {

    private ICalculate.Stub mBinder = new ICalculate.Stub() {
        @Override
        public double doCalculate(double a, double b) throws RemoteException {
            double result = a + b;
            return result;
        }
    };

    @Override
    public IBinder onBind(Intent intent) {
        return mBinder;
    }
}
  1. 主Activity的业务逻辑:
    绑定服务,获取到Binder对象,通过该对象来调用AIDL的方法doCalculate。代码如下:
package com.example.ubuntu.aidlcalculatedemo;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;
import android.os.RemoteException;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    private EditText num1, num2;
    private TextView resultText;
    private Button calculate;
    private ICalculate iCalculate;

    private ServiceConnection serviceConnection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            iCalculate = ICalculate.Stub.asInterface(service);
        }
        @Override
        public void onServiceDisconnected(ComponentName name) {
            iCalculate = null;
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        num1 = (EditText) findViewById(R.id.num1);
        num2 = (EditText) findViewById(R.id.num2);
        resultText = (TextView) findViewById(R.id.result_text);
        calculate = (Button) findViewById(R.id.result);

        Intent intent = new Intent(MainActivity.this, CalculateService.class);
        bindService(intent, serviceConnection, BIND_AUTO_CREATE);

        calculate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //点击button,隐藏软键盘
                InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);

                double a = Double.parseDouble(num1.getText().toString());
                double b = Double.parseDouble(num2.getText().toString());
                try {
                    double result = iCalculate.doCalculate(a, b);//调用接口
                    resultText.setText("计算结果:" + result);
                } catch (RemoteException e) {
                    e.printStackTrace();
                }
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unbindService(serviceConnection);
    }
}

最后,Manifest.xml文件中按如下配置:




    
        
            
                

                
            
        
        
    


这样我们就完成了跨进程调用,收工!效果图如下:

AIDL实例_第1张图片
Screenshot_20170821-160126.png

你可能感兴趣的:(AIDL实例)