AIDL的简单Demo

本文通过一个具体的示例展示了如何使用Android Interface Definition Language (AIDL)实现Android应用程序之间的跨进程通信。示例包括客户端和服务端的代码实现,以及AIDL文件的定义。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

第一步创建一个aidl文件如图:


以下是客户端:

package com.example.k.aidldemo;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;
import android.os.Parcel;
import android.os.Parcelable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
int c=0;
    Button b,b1;
    String str;
private ServiceConnection connection = new ServiceConnection() {
    @Override
    public void onServiceConnected(ComponentName name, IBinder service) {
        IBookList i = IBookList.Stub.asInterface(service);
        try {
            i.getString(11);
            i.method();
            str = i.getValue();
        }catch(Exception e){

        }

    }

    @Override
    public void onServiceDisconnected(ComponentName name) {

    }
};
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        b = (Button) findViewById(R.id.button);
        b1 = (Button) findViewById(R.id.button2);
        b1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(MainActivity.this,"服务器返回的值为:"+str,Toast.LENGTH_SHORT).show();
            }
        });
        b.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this,MyService.class);
                bindService(intent,connection, Context.BIND_AUTO_CREATE);
            }
        });
    }
}

以下是服务端:

package com.example.k.aidldemo;

import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.os.RemoteException;
import android.support.annotation.Nullable;
import android.util.Log;
/**
 * Created by k on 2016/6/6.
 */
public class MyService extends Service {
    int b = 0;
    private Binder binder = new IBookList.Stub(){
        public void getString(int a) throws RemoteException{
            b = a;
        }
        public void method()throws RemoteException{
            Log.i("ok","服务的b="+b);
        }
        public String getValue() throws RemoteException{
            return "ok!ok!";
        }
    };
    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return binder;
    }
}

以下是aidl文件:

// IBookList.aidl
package com.example.k.aidldemo;
// Declare any non-default types here with import statements

interface IBookList {
void getString(in int a);
void method();
String getValue();
}

以上实现了,不同进程间的互相通信。



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值