• AIDL实现进程间通信


    aidl可以看做binder的一个辅助接口,aidl让binder通信更加高效.首先包名右键新建aidl文件,在aidl接口中定义一个helloworld方法,然后点击build->makeProject让接口同步,然后新建服务,在服务中实现aidl接口,并自动生成helloworld方法,在onbind方法中返回.Stup对象,.Stup是封装在接口中的必须实现.在配置文件中定义过滤action,action可以随便定义,但是远程绑定此服务的类中要使用相同的action.然后新建一个modle,把第一个modle的包含aidl接口的包整个复制到这个modle      在oncreate方法中绑定远程服务.

    package com.january.spring;
    
    // Declare any non-default types here with import statements
    
    interface IMyAidlInterface {
        /**
         * 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);
                  void sayHelloWorld();
    }

    上面定义的接口和sayhelloworld方法

    public class MainActivity extends AppCompatActivity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            Button button = findViewById(R.id.button);
            startService(new Intent(this, MyService.class));
    
    
    
    
        }
        }

    上面是第一个modle的主方法

    public class MyService extends Service {
    IMyAidlInterface.Stub stub=new IMyAidlInterface.Stub() {
        @Override
        public void basicTypes(int anInt, long aLong, boolean aBoolean, float aFloat, double aDouble, String aString) throws RemoteException {
    
        }
    
        @Override
        public void sayHelloWorld() throws RemoteException {
    System.out.println("hello");
        }
    };
        public MyService() {
        }
    
        @Override
        public IBinder onBind(Intent intent) {
            return stub;
        }
    }

    上面是第一个Modle的服务

        <application
            android:allowBackup="true"
            android:icon="@mipmap/ic_launcher"
            android:label="@string/app_name"
            android:roundIcon="@mipmap/ic_launcher_round"
            android:supportsRtl="true"
            android:theme="@style/AppTheme">
            <service
                android:name=".MyService"
                android:enabled="true"
                android:exported="true">
                <intent-filter>
                    <action android:name="com.january.spring" />
                </intent-filter>
            </service>
    
    
            <activity android:name=".MainActivity">
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />
    
                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity>
        </application>

    上面是第一个modle的配置文件

    /////////////////

    public class MainActivity extends AppCompatActivity {
        private Button bind_btn;
        private IMyAidlInterface iMyAidlInterface;
        @Override
    
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            Intent intent = new Intent("com.january.spring");
            intent.setPackage("com.january.spring"); //Android5.0之后需要指定共享Service所在应用的应用包名,否则会抛异常
            bindService(intent, connection, BIND_AUTO_CREATE);
        }
    
        private ServiceConnection connection = new ServiceConnection() {
            @Override
            public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
               iMyAidlInterface = IMyAidlInterface.Stub.asInterface(iBinder);
    
                try {
    
                  iMyAidlInterface.sayHelloWorld();
    
                } catch (RemoteException e) {
                    e.printStackTrace();
                }
            }
    
            @Override
            public void onServiceDisconnected(ComponentName componentName) {
    
            }
        };
    
    
    }

    上面是第二个modle的主类,绑定了服务Android5.0之后需要指定共享Service所在应用的应用包名,否则会抛异常  ,先运行第一个modle开启服务,然后运行第二个modle绑定,输出hello证明绑定成功

    上面是从应用2传数据到应用1,如果从应用1传数据到应用2,只要在aidl接口文件里声明有返回值的方法就行了,比如要传Parcelable对象就定义方法MyParcelable getDate();在服务里实现接口方法时返回MyParcelable对象然后在应用2接收就行

  • 相关阅读:
    Sqlite中文排序
    关于java中实现在oracle数据库中实现对中文首字母进行排序的解决方案
    Java面试题全集(下)
    Java面试题全集(上)
    更新Svn客户端后,右键菜单中没有TortoiseSVN
    Maven项目报错:Failed to execute goal org.apache.maven.plugins:maven-clean-plugin:2.5:clean (default-clea
    华为机试题---1、字符串最后一个单词的长度
    ES6 类 继承
    JS 创建对象的几种方法(总结)
    80端口和8080端口的区别是什么
  • 原文地址:https://www.cnblogs.com/Ocean123123/p/10957164.html
Copyright © 2020-2023  润新知