双进程/两个app之间使用AIDL

首先第一个APP 做AIDL的接口及服务,第二个APP再去绑定服务,然后使用AIDL接口中的方法(绑定第一个APP的服务的时候必须先打开第一个APP在后台运行,否则无法绑定) 这里主要是讲 两个应用之间使用AIDL 不会细讲如何创建AIDL的过程及约束。建立在已经过学习过AIDL的朋友之上学习的。

第一个APP:

1、创建一个AIDL文件 ICompute.aidl 然后 声明一个add方法 
        // ICompute.aidl
         package com.example.vvv.myaidl1;
         // Declare any non-default types here with import statements
         interface ICompute {
            int add(int a,int b);
         } 
2、创建一个服务供第二个APP,然后实现AIDL方法,并以Binder的形式返回给客户端。	
	public class ComputeServer extends Service {
	    @Nullable
	    @Override
  	  public IBinder onBind(Intent intent) {
  	      return binder;
 	   }

 	   private IBinder binder = new ICompute.Stub() {
   	     @Override
   	     public int add(int a, int b) throws RemoteException {
     	       return a + b;
  	      }
  	  };
	}
这个时候远程服务的代码已经写完了,等待着另外一个进程/APP来启动服务并拿到该接口,然后调用该接口的方法,实现功能,这里是两个数相加
	

第二个APP:

1、绑定服务,然后拿到接口ICompute实例,然后调用该add的方法
	
public class MainActivity extends AppCompatActivity {
    Button button;
    ICompute compute;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        button = (Button) findViewById(R.id.btn);
        final Intent intent = new Intent();
        intent.setComponent(new ComponentName("com.example.vvv.myaidl1", "com.example.vvv.myaidl1.ComputeServer"));
        bindService(intent, serviceConnection, Context.BIND_AUTO_CREATE);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (null != compute) {
                    int sum = 0;
                    try {
                        sum = compute.add(1, 2);
                        Toast.makeText(MainActivity.this, sum + "--", Toast.LENGTH_LONG).show();
                    } catch (RemoteException e) {
                        e.printStackTrace();
                    }
                }
            }
        });
    }

    private ServiceConnection serviceConnection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            compute = ICompute.Stub.asInterface(service);
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
        }
    };

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unbindService(serviceConnection);
    }
}
这时候两个APP的远程调用 已经完成了 。布局什么的比较简单这里代码就不贴出来了。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
1. 创建aidl文件 在app/src/main/aidl目录下创建一个名为IHelloWorld.aidl的文件,内容如下: ``` package com.example.helloworld; interface IHelloWorld { String sayHello(); void setHello(String hello); } ``` 2. 实现aidl接口 在app/src/main/java目录下创建一个名为HelloWorldService的Java类,实现IHelloWorld接口,代码如下: ``` package com.example.helloworld; import android.app.Service; import android.content.Intent; import android.os.IBinder; import android.os.RemoteException; public class HelloWorldService extends Service { private String mHello = "Hello World!"; private final IHelloWorld.Stub mBinder = new IHelloWorld.Stub() { @Override public String sayHello() throws RemoteException { return mHello; } @Override public void setHello(String hello) throws RemoteException { mHello = hello; } }; @Override public IBinder onBind(Intent intent) { return mBinder; } } ``` 3. 注册服务 在app/src/main/AndroidManifest.xml文件中注册服务,代码如下: ``` <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.helloworld"> <application android:allowBackup="true" android:label="@string/app_name" android:icon="@mipmap/ic_launcher" android:theme="@style/AppTheme"> <service android:name=".HelloWorldService" android:exported="true"> <intent-filter> <action android:name="com.example.helloworld.IHelloWorld" /> </intent-filter> </service> </application> </manifest> ``` 4. 调用aidl接口 在app/src/main/java目录下创建一个名为MainActivity的Java类,代码如下: ``` package com.example.helloworld; import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle; import android.os.IBinder; import android.os.RemoteException; import android.util.Log; public class MainActivity extends AppCompatActivity { private static final String TAG = "MainActivity"; private IHelloWorld mHelloWorld; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); IBinder binder = getService("com.example.helloworld.IHelloWorld"); mHelloWorld = IHelloWorld.Stub.asInterface(binder); try { String hello = mHelloWorld.sayHello(); Log.d(TAG, "sayHello: " + hello); mHelloWorld.setHello("Hello Android!"); hello = mHelloWorld.sayHello(); Log.d(TAG, "sayHello: " + hello); } catch (RemoteException e) { e.printStackTrace(); } } private IBinder getService(String serviceName) { IBinder binder = null; Intent intent = new Intent(serviceName); intent.setPackage(getPackageName()); ServiceConnection conn = new ServiceConnection() { @Override public void onServiceConnected(ComponentName name, IBinder service) { binder = service; } @Override public void onServiceDisconnected(ComponentName name) { } }; bindService(intent, conn, Context.BIND_AUTO_CREATE); return binder; } } ``` 运行app,查看Logcat中的输出信息,即可看到调用aidl接口的结果。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值