AIDL使用场景:
1.需要IPC ;2,需要并发
AIDL文件的代码:
客户端与服务端要完全相同,包括包名
// ICalcAIDL.aidl
package com.whhos.aidlservice;
// Declare any non-default types here with import statements
interface CalAIDL {
/**
* Demonstrates some basic types that you can use as parameters
* and return values in AIDL.
*/
int add(int x , int y);
}
服务端Service的代码:
package com.whhos.aidlservice;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.os.RemoteException;
public class AidlService extends Service {
private CalAIDL.Stub mBinder = new CalAIDL.Stub() {
@Override
public int add(int x, int y) throws RemoteException {
return x+y*2;
}
};
@Override
public IBinder onBind(Intent intent) {
// TODO: Return the communication channel to the service.
return mBinder;
}
}
服务端mainfest中Service注册的代码:
<service
android:name=".AidlService"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="com.whhos.calservice2"></action>
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</service>
客户端Activity的代码:
package com.whhos.aidlclient;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import com.whhos.aidlservice.CalAIDL;
public class MainActivity extends AppCompatActivity {
private Button btCal;
private EditText etNum1;
private EditText etNum2;
private TextView tvResult;
private CalAIDL serAidl;
ServiceConnection sc = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
serAidl = CalAIDL.Stub.asInterface(service);
}
@Override
public void onServiceDisconnected(ComponentName name) {
serAidl = null;
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btCal =(Button) findViewById(R.id.cal);
etNum1 = (EditText) findViewById(R.id.et_num1);
etNum2 = (EditText) findViewById(R.id.et_num2);
tvResult = (TextView) findViewById(R.id.tv_result);
Intent intent = new Intent();
intent.setPackage("com.whhos.aidlservice");
intent.setAction("com.whhos.calservice2");
bindService(intent,sc, Context.BIND_AUTO_CREATE);
btCal.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int num1 =Integer.parseInt(etNum1.getText().toString().trim()) ;
int num2 =Integer.parseInt(etNum2.getText().toString().trim()) ;
int result = 0;
try {
result = serAidl.add(num1,num2);
} catch (RemoteException e) {
e.printStackTrace();
}
tvResult.setText(""+result);
}
});
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
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"
>
<EditText
android:id="@+id/et_num1"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<EditText
android:id="@+id/et_num2"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/tv_result"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!" />
<Button
android:id="@+id/cal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="计算"/>
</LinearLayout>