安卓IPC(进程间通信)之AIDL基本使用

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_35189116/article/details/72673745
1.客服端
public class MyService extends Service {
@Nullable
@Override
public IBinder onBind(Intent intent) {
return new MyBinder();
}

class MyBinder extends IMyAidlInterface.Stub {

@Override
public int add(int a, int b) throws RemoteException {
return a + b;
}
}

}
/**
* 步骤:
* 1.创建AIDL文件夹,选中工程,右键单击,new->folder->aidl folder
* 2.创建AIDL文件,修改其中的方法(new->aidl->aidl file)
* 3.rebuild projec,检查app->build->generated->source->aidl->debug->包名->XXX
* 4.创建Service,创建内部类MyBinder,实现Stub
* 5.注册Service,注意添加exported和action属性
*/
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
服务端
public class MyService extends Service {
@Nullable
@Override
public IBinder onBind(Intent intent) {
return new MyBinder();
}

class MyBinder extends IMyAidlInterface.Stub {

@Override
public int add(int a, int b) throws RemoteException {
return a + b;
}
}

}
/**
* 步骤:
* 1.创建AIDL文件夹,选中工程,右键单击,new->folder->aidl folder
* 2.创建AIDL文件,修改其中的方法(new->aidl->aidl file)
* 3.rebuild projec,检查app->build->generated->source->aidl->debug->包名->XXX
* 4.创建Service,创建内部类MyBinder,实现Stub
* 5.注册Service,注意添加exported和action属性
*/
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}

猜你喜欢

转载自blog.csdn.net/qq_35189116/article/details/72673745