AIDL_Service_跨进程通信

AIDL——进程间通信(必须用绑定服务)
android interface defination language
使用AIDL流程
1、在服务端,创建胶水类
2、在服务端,实现onBind
3、在客户端,Activity绑定服务端的Service(通过package和action)
4、在客户端,判断是否绑定成功
5、在客户端,绑定成功时,通过胶水获取来自服务端的AIDL接口对象
注意:服务端与客户端中的AIDL接口定义完全一致
服务端
AIDLService

public class AIDLService extends Service {

    // 第一步,创建胶水类
    // stub:存根。已经继承了Binder的类,必须实现AIDL接口的回调方法
//stub这个特殊的胶水必须回调方法
    public class MyBinder extends IMyAidlInterface.Stub {

        @Override
        public int plus(int plus01, int plus02) throws RemoteException {
            return plus01 + plus02;
        }
    }

    // 第二步,将胶水(含有一个回调方法的胶水)涂在Service表面
    @Override
    public IBinder onBind(Intent intent) {
        return new MyBinder();
    }
}

需要实现一个aidl接口,这个接口一般必须要在main文件夹下创建一个aidl文件夹严格区分大小写,再在此文件夹下创建一个.aidl格式的接口(File-new-下面的绿色文件里自己找).
aidl接口(当普通接口理解)
IMyAidlInterface.aidl

package org.mobiletrain.android24_aidlserver;

interface IMyAidlInterface {

    int plus(int plus01, int plus02);

}

这个接口写完之后必须Build-Clean Project一下,不然找不到这个接口
清单文件里给服务做标记,不然Intent跳转不过来

 <service
            android:name=".AIDLService"
            android:enabled="true"
            android:exported="true">

            <intent-filter>
                <action android:name="aidl_action"/>
            </intent-filter>

        </service>

客户端(Activity端)
Mianactivity

public class MainActivity extends AppCompatActivity implements ServiceConnection {

    private EditText plus01EditText;
    private EditText plus02EditText;

    // 来自服务端中Service里的AIDL接口对象
    private IMyAidlInterface aidlCallback;
    private TextView resultTextView;

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

        initView();
        bindAIDLService();
    }

    // 第三步,绑定其他进程的Service
    private void bindAIDLService() {
//隐式意图绑定别人家的服务
        Intent intent = new Intent("aidl_action");
        intent.setPackage("org.mobiletrain.android24_aidlserver");
//跳转到别人家的服务里去,高版本的时候必须注意设置包名
        boolean isBindingSuccessful = bindService(intent, this, Context.BIND_AUTO_CREATE);
        Toast.makeText(MainActivity.this, "是否绑定成功:" + isBindingSuccessful, Toast.LENGTH_SHORT).show();
    }

    private void initView() {
        plus01EditText = (EditText) findViewById(R.id.plus01_et);
        plus02EditText = (EditText) findViewById(R.id.plus02_et);
        resultTextView = (TextView) findViewById(R.id.result_tv);
    }

    // 第五步,执行其他进程中的加法运算
    public void plus(View view) {
        String plus01String = plus01EditText.getText().toString();
        String plus02String = plus02EditText.getText().toString();
        if (!TextUtils.isEmpty(plus01String) && !TextUtils.isEmpty(plus02String)) {
            int plus01 = Integer.parseInt(plus01String);
            int plus02 = Integer.parseInt(plus02String);
            try {
                int result = aidlCallback.plus(plus01, plus02);
                resultTextView.setText("计算结果:" + result);
            } catch (RemoteException e) {
                e.printStackTrace();
            }
        }
    }

    // 第四步,绑定成功时,获取Service表面的胶水(含有回调方法的胶水)
    @Override
    public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
//固定写法获取接口对象.
        aidlCallback = IMyAidlInterface.Stub.asInterface(iBinder);
    }

    @Override
    public void onServiceDisconnected(ComponentName componentName) {
    }
}

布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="org.mobiletrain.android24_aidlclient.MainActivity">

    <EditText
        android:id="@+id/plus01_et"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="加数1"
        android:inputType="number"/>

    <EditText
        android:id="@+id/plus02_et"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="加数2"
        android:inputType="number"/>

    <TextView
        android:id="@+id/result_tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="结果"/>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="加法运算"
        android:onClick="plus"/>

</LinearLayout>

写一个和上面那个一模一样一模一样的接口包都一样,对就是用上面的包名
IMyAidlInterface.aidl

package org.mobiletrain.android24_aidlserver;

interface IMyAidlInterface {

    int plus(int plus01, int plus02);

}
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 216,919评论 6 502
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 92,567评论 3 392
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 163,316评论 0 353
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,294评论 1 292
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,318评论 6 390
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,245评论 1 299
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,120评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,964评论 0 275
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,376评论 1 313
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,592评论 2 333
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,764评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,460评论 5 344
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,070评论 3 327
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,697评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,846评论 1 269
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,819评论 2 370
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,665评论 2 354

推荐阅读更多精彩内容