AIDL进程通信+application

AIDL 简介

AIDL,全称是Android Interface Define Language,即安卓接口定义语言,可以实现安卓设备中进程之间的通信(Inter Process Communication, IPC)。

步骤

服务端

在main文件夹下创建,新建adil文件记得重建。
在这里插入图片描述

在adil文件内添加自己需要的方法,用来处理数据。

interface Aidl {
//抽象方法
int add (int a,int b);
    /**
     * Demonstrates some basic types that you can use as parameters
     * and return values in AIDL.
     */
    void basicTypes(int anInt, long aLong, boolean aBoolean, float aFloat,
            double aDouble, String aString);
}

自定义一个IBinder,引出adil中的Stub对象,自动重写adil文件中的方法。
在Service的绑定onBind方法中返回binder。
在服务端创建Service类:

public class MyService extends Service {

    public MyService() {
    }

    IBinder binder=new Aidl.Stub() {
        @Override
        public int add(int a, int b) throws RemoteException {
            return a+b;
        }

        @Override
        public void basicTypes(int anInt, long aLong, boolean aBoolean, float aFloat, double aDouble, String aString) throws RemoteException {

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

在清单文件中注册,并向外提供出去。
exoirted 设置为true , 保证能被其他应用调用。

 <!--把service提供出去,起个名-->
        <service android:name=".MyService"
            android:enabled="true"
            android:exported="true"
            >
            <intent-filter>
                <action android:name="ki"/>
            </intent-filter>
        </service>

客户端

在客户端要求和服务端一样的adil文件,可以复制到客户端。

客户端绑定服务:

public class MainActivity extends AppCompatActivity {



    Aidl aidl;

    ServiceConnection connection=new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
            aidl=Aidl.Stub.asInterface(iBinder);

            int num=0;
            try {
                num=aidl.add(2,1);
            } catch (RemoteException e) {
                e.printStackTrace();
            }
            Toast.makeText(MainActivity.this, " "+num, Toast.LENGTH_SHORT).show();


        }

        @Override
        public void onServiceDisconnected(ComponentName componentName) {
            //防止溢出,清空接口
            aidl=null;
        }
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Intent intent = new Intent();
        intent.setAction("ki");
        intent.setPackage("com.example.day16_1");//服务端的包名
        bindService(intent,connection, Service.BIND_AUTO_CREATE);

    }
}

Application

自定义一个类,继承application:

public class MyApplaction extends Application {

    String name="aaa";
    
    @Override
    public void onCreate() {
        super.onCreate();
        Log.i("TAG", "onCreate: 创建执行");
    }

    @Override
    public void onTerminate() {
        Log.i("TAG", "onTerminate: 程序终止时执行");
        super.onTerminate();
    }

    @Override
    public void onLowMemory() {
        Log.i("TAG", "onLowMemory: 低内存时执行");
        super.onLowMemory();
    }

    @Override
    public void onTrimMemory(int level) {
        Log.i("TAG", "onTrimMemory: 程序在内存清理时执行");
        super.onTrimMemory(level);
    }

    @Override
    public void onConfigurationChanged( Configuration newConfig) {
        Log.i("TAG", "onConfigurationChanged: 设备发生变化时调用");
        super.onConfigurationChanged(newConfig);
    }
}

清单文件注册:

android:name=".MyApplaction"

MainActivity:

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

        textView=findViewById(R.id.textview);

        textView.setText(((MyApplaction) getApplication()).name);
        textView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
          		 //将application内的字符串修改
                ((MyApplaction) getApplication()).name="bbb";
                Intent intent = new Intent(MainActivity.this, Main2Activity.class);
                startActivity(intent);
            }
        });

    }

Main2Activity:

 protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        textView=findViewById(R.id.textview2);
        textView.setText(((MyApplaction)getApplication()).name);

    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值