Android AIDL demo

为使应用程序之间能够彼此通信,Android提供了IPC (Inter Process Communication,进程间通信)的一种独特实现: AIDL (Android Interface Definition Language, Android接口定义语言)。

Android AIDL demo_第1张图片
1。新建项目App和AppHolder
2。App中新建Service

package com.louis.imoocaidl;

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

public class MyService extends Service {
    public MyService() {
    }

    @Override
    public IBinder onBind(Intent intent) {
        // TODO: Return the communication channel to the service.
       // throw new UnsupportedOperationException("Not yet implemented");
       return iBinder;
    }

    private  IBinder iBinder= new IMyAidlInterface.Stub() {
        @Override
        public int plus(int numA, int numB) throws RemoteException {
            Log.i("louis","louis==xx==numA:"+numA+"numB:"+numB);
            return numA+numB;
        }
    };
}

3。App中新建aidl,然后AppHolder中新建相同包名,复制aidl过来

// IMyAidlInterface.aidl
package com.louis.imoocaidl;

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

interface IMyAidlInterface {
 int plus(int numA,int numB);
}

4。appholder=》activity

package com.louis.appholder;

import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.louis.imoocaidl.IMyAidlInterface;

public class MainActivity extends AppCompatActivity {

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

        initViews();
        bindSer();
    }

    private void initViews() {
      final EditText et1= (EditText) findViewById(R.id.num1);
        final EditText et2= (EditText) findViewById(R.id.num2);
        final TextView result = (TextView) findViewById(R.id.result);
        Button btn_plus= (Button) findViewById(R.id.btn_plus);
        btn_plus.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //
                int a = Integer.parseInt(et1.getText().toString());
                int b = Integer.parseInt(et2.getText().toString());
                int plusResult = 0;
                try {
                    plusResult = im.plus(a, b);
                } catch (RemoteException e) {
                    e.printStackTrace();
                }
                result.setText(String.valueOf(plusResult));
            }
        });
    }

    private void bindSer() {
        Intent intent=new Intent();
        intent.setComponent(new ComponentName("com.louis.imoocaidl", "com.louis.imoocaidl.MyService"));//5.0以后必须显示绑定
        ServiceConnection conn=new ServiceConnection() {
            @Override
            public void onServiceConnected(ComponentName name, IBinder service) {
                im= IMyAidlInterface.Stub.asInterface(service);
            }

            @Override
            public void onServiceDisconnected(ComponentName name) {
                im=null;
            }
        };
        bindService(intent,conn,BIND_AUTO_CREATE);
    }
}

5。MainActivity的布局xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" app:layout_behavior="@string/appbar_scrolling_view_behavior" tools:context="com.louis.appholder.MainActivity" tools:showIn="@layout/activity_main">
<EditText  android:id="@+id/num1" android:layout_width="wrap_content" android:layout_height="wrap_content" />
    <TextView  android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="+"/>
    <EditText  android:id="@+id/num2" android:layout_width="wrap_content" android:layout_height="wrap_content" />
    <TextView  android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="="/>
    <TextView  android:id="@+id/result" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="结果"/>
    <Button  android:id="@+id/btn_plus" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="计算"/>

</LinearLayout>

6,结果
Android AIDL demo_第2张图片

DEMO:http://download.csdn.net/detail/richiezhu/9445103

你可能感兴趣的:(android,aidl,aidl)