一个AIDL案例

服务端代码:

生成的aidl文件:

interface IMyAidlInterface {

    String getUserName();

    String getPassword();

}

自定义一个服务:

public class LzyService extends Service {

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return myBinder;
    }

    public final IMyAidlInterface.Stub myBinder = new IMyAidlInterface.Stub() {
        @Override
        public String getUserName() throws RemoteException {
            return "lzy";
        }

        @Override
        public String getPassword() throws RemoteException {
            return "nb666plus";
        }
    };


}

清单文件注册服务:

<service android:name=".LzyService">

            <intent-filter>
                <action android:name="lzy.server"/>
            </intent-filter>

        </service>

启动服务:

public class MainActivity extends AppCompatActivity {

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

        startService(new Intent(this,LzyService.class));


    }



}

客户端代码:

先把服务端aidl文件复制一份到客户端:

public class MainActivity extends AppCompatActivity {

    private String password;
    private String userName;

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

        Intent intent = new Intent();
        intent.setAction("lzy.server");
        intent.setPackage("com.example.michael.ipc"); //兼容5.0
        bindService(intent,serviceConnection,BIND_AUTO_CREATE);
        Button btnUserName = findViewById(R.id.btn_userName);
        Button btnPsw = findViewById(R.id.btn_psw);
        btnUserName.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(getApplicationContext(), userName,Toast.LENGTH_SHORT).show();

            }
        });
        btnPsw.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(getApplicationContext(), password,Toast.LENGTH_SHORT).show();
            }
        });
    }

    private ServiceConnection serviceConnection = new ServiceConnection() {

        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            IMyAidlInterface iMyAidlInterface = IMyAidlInterface.Stub.asInterface(service);
            try {
                userName = iMyAidlInterface.getUserName();
                password = iMyAidlInterface.getPassword();

            } catch (RemoteException e) {
                e.printStackTrace();
            }

        }

        @Override
        public void onServiceDisconnected(ComponentName name) {

        }
    };



}

布局文件简单写几个按钮:

<?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:orientation="vertical"
    tools:context="com.example.michael.pic2.MainActivity">

    <Button
        android:id="@+id/btn_userName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="用户名"
        />

    <Button
        android:id="@+id/btn_psw"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="密码"
        />

</LinearLayout>

效果演示:

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
好的,以下是一个简单的 Android AIDL 示例代码: 首先,需要定义一个 AIDL 接口文件,例如 MyService.aidl: ``` package com.example.myservice; interface MyService { String getMessage(); } ``` 然后在服务端实现这个接口,例如 MyService.java: ``` package com.example.myservice; import android.app.Service; import android.content.Intent; import android.os.IBinder; public class MyService extends Service { private final MyServiceImpl mImpl = new MyServiceImpl(); private static class MyServiceImpl extends IMyService.Stub { @Override public String getMessage() { return "Hello from MyService!"; } } @Override public IBinder onBind(Intent intent) { return mImpl; } } ``` 最后,在客户端绑定服务并调用接口方法,例如 MainActivity.java: ``` package com.example.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.widget.TextView; import androidx.appcompat.app.AppCompatActivity; import com.example.myservice.MyService; public class MainActivity extends AppCompatActivity { private MyService mService; private boolean mBound = false; private final ServiceConnection mConnection = new ServiceConnection() { @Override public void onServiceConnected(ComponentName name, IBinder service) { mService = MyService.Stub.asInterface(service); mBound = true; // 调用 MyService 中定义的接口方法 try { String message = mService.getMessage(); TextView textView = findViewById(R.id.text_view); textView.setText(message); } catch (RemoteException e) { e.printStackTrace(); } } @Override public void onServiceDisconnected(ComponentName name) { mService = null; mBound = false; } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // 绑定 MyService Intent intent = new Intent(); intent.setComponent(new ComponentName("com.example.myservice", "com.example.myservice.MyService")); bindService(intent, mConnection, Context.BIND_AUTO_CREATE); } @Override protected void onDestroy() { super.onDestroy(); if (mBound) { unbindService(mConnection); mBound = false; } } } ``` 鉴于这只是一个简单的示例,实际的应用场景可能会更加复杂。需要注意的是,AIDL 接口中定义的方法的参数和返回值必须是支持跨进程传输的类型,例如基本类型、String、Parcelable 等。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

AD钙奶-lalala

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值