AIDL Demo演示

一.参考

  1. https://developer.android.com/guide/components/aidl

  2. https://android.googlesource.com/platform/development/+/master/samples/ApiDemos/src/com/example/android/apis/app/RemoteService.java

  3. Service Intent must be explicit的解决方案
    https://blog.csdn.net/pvpheroszw/article/details/77981553

  4. Binder invocation to an incorrect interface 异常分析
    https://blog.csdn.net/wangqiubo2010/article/details/72368294

二. Demo

  1. Service 端独立App
// IMyAidlInterface.aidl
package com.test.myapplication;

// 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 sayHello (String hello);
}
public class MyService extends Service {
    public MyService() {
    }

    @Override
    public IBinder onBind(Intent intent) {
        // TODO: Return the communication channel to the service.
        return mBinder;
    }

    private final IMyAidlInterface.Stub mBinder = new IMyAidlInterface.Stub() {
        @Override
        public void sayHello(String hello) throws RemoteException {
            if (hello != null) {
//                Toast.makeText(MyService.this, hello, Toast.LENGTH_LONG).show();
                Log.e(TAG, hello);
            }
        }
    };
}
<service
            android:name=".MyService"
            android:enabled="true"
            android:exported="true">
            <intent-filter>
                <action android:name="com.test.AIDLService"/>
            </intent-filter>

        </service>
  1. Client端独立App
// IMyAidlInterface.aidl
package com.test.myapplication;

// 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 sayHello (String hello);
}
public class MainActivity extends AppCompatActivity {

    private static final String ACTION_AIDL_SERVICE = "com.test.AIDLService";

    private static final String SERVICE_PACKAGE_NAME = "com.test.myapplication";

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

        Button button = findViewById(R.id.bind);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setAction(ACTION_AIDL_SERVICE);
                intent.setPackage(SERVICE_PACKAGE_NAME);
                bindService(intent, mConnection, Context.BIND_AUTO_CREATE);
            }
        });
    }

    private ServiceConnection mConnection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {

            IMyAidlInterface myAidlInterface = IMyAidlInterface.Stub.asInterface(service);

            try {
                myAidlInterface.sayHello("hello222");
            } catch (RemoteException e) {
                e.printStackTrace();
            }


        }

        @Override
        public void onServiceDisconnected(ComponentName name) {

        }
    };
}
上一篇:AIDL进程间通信


下一篇:Android中AIDL的使用