AIDL创建
-
新建Aidldemo项目
-
选中APP-右键-new-AIDL-AIDL file
会出现一个aidl目录,但是注意此时还没有生成对应的Java文件,如下图所示
-
点击Build-Clean Project(Rebuild 也可以),aidl对应的java文件就会出现了
写个加法计算器
-
更改aidl目录下的aidl文件如下:
interface IAdditionService {
int add(in int x, in int y);
}
-
在MainActivity同级目录下,创建一个Service
更改代码如下:
public class AdditionService extends Service {
public AdditionService() {
}
@Override
public IBinder onBind(Intent intent) {
return mBinder;
}
IAdditionService.Stub mBinder = new IAdditionService.Stub() {
@Override
public int add(int x, int y) throws RemoteException {
return x + y;
}
};
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
-
写个简单的布局,输入两个加数,点击按钮相加,并输出结果
```
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="example.guanhang.aidldemo.MainActivity"
tools:showIn="@layout/activity_main">
<EditText
android:id="@+id/et_num1"
android:layout_width="180dp"
android:layout_gravity="center"
android:layout_height="wrap_content"
/>
<EditText
android:id="@+id/et_num2"
android:layout_width="180dp"
android:layout_gravity="center"
android:layout_height="wrap_content"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="add"
android:text="相加"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="相加结果为:"
/>
<TextView
android:id="@+id/tv"
android:layout_weight="2"
android:layout_width="0dp"
android:layout_height="wrap_content"
/>
</LinearLayout>
</LinearLayout>
```
public class MainActivity extends AppCompatActivity {
IAdditionService mService;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
Intent intentService = new Intent(this,AdditionService.class);
bindService(intentService, mConnection, Context.BIND_AUTO_CREATE);
}
ServiceConnection mConnection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
mService = IAdditionService.Stub.asInterface(service);
}
@Override
public void onServiceDisconnected(ComponentName name) {
}
};
public void add(View view) throws RemoteException {
EditText et1 = (EditText) findViewById(R.id.et_num1);
EditText et2 = (EditText) findViewById(R.id.et_num2);
TextView tv = (TextView) findViewById(R.id.tv);
int x = Integer.parseInt(et1.getText().toString());
int y = Integer.parseInt(et2.getText().toString());
int result = mService.add(x, y);
tv.setText(result +"");
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
-
结果
新建另一个项目,来启动这个远程服务
新建StartAdd 项目,在该项目启动Aidldemo中的加法服务,布局照抄上面那个项目
不过这个项目因为没有Service的类,所以要在之前的那个项目中,加上隐式启动服务的过滤器
注意:该项目新建的aidl要和之前项目的aidl包名以及文件内容要一模一样,所以这时不能右键新建AIDL file了(因为,这样默认的包名是当前项目的包名,和之前的aidl的文件的package是不符的)。因此要:
- 新建一个包,名字和上面项目Aidl所在的包名一致
- 把上面那个项目的aidl文件直接拷到这个包里面
- Clean Project ,自动生成相应的java文件
修改MainActivity:
public class MainActivity extends AppCompatActivity {
IAdditionService mService;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
Intent intent = new Intent();
intent.setAction("com.guanhang.add");
bindService(intent, mConnection, Context.BIND_AUTO_CREATE);
}
ServiceConnection mConnection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
mService = IAdditionService.Stub.asInterface(service);
}
@Override
public void onServiceDisconnected(ComponentName name) {
}
};
public void add(View view) throws RemoteException {
EditText et1 = (EditText) findViewById(R.id.et_num1);
EditText et2 = (EditText) findViewById(R.id.et_num2);
TextView tv = (TextView) findViewById(R.id.tv);
int x = Integer.parseInt(et1.getText().toString());
int y = Integer.parseInt(et2.getText().toString());
int result = mService.add(x, y);
tv.setText(result +"");
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
运行结果: