一、首先建立service进程。
1.创建aidl文件 (注意包名要与client进程中aidl包名相同)
2.aidl代码
package com.goway.test_aidl_service;
// Declare any non-default types here with import statements
interface StudentAidl {
/**
* Demonstrates some basic types that you can use as parameters
* and return values in AIDL.
*/
void basicTypes(int anInt, long aLong, boolean aBoolean, float aFloat,
double aDouble, String aString);
String getStudent(int no);
}
3、创建service,并实现aidl接口。
package com.goway.test_aidl_service;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.os.RemoteException;
import android.support.annotation.Nullable;
/**
* Created by Administrator on 2017/3/6.
*/
public class StudentSerivce extends Service {
private String[] studentNo = {"王1","2","王3","王4","王月4","宋6"};
private StudentAidl.Stub mBind= new StudentAidl.Stub() {
@Override
public void basicTypes(int anInt, long aLong, boolean aBoolean, float aFloat, double aDouble, String aString) throws RemoteException {
}
@Override
public String getStudent(int no) throws RemoteException {
int l = studentNo.length;
if(l<0) {
no = 0;
}
if(no>=l) {
no = l - 1;
}
return studentNo[no];
}
};
@Nullable
@Override
public IBinder onBind(Intent intent) {
return mBind;
}
}
4.在service端androidManifast中注册
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.goway.test_aidl_service" >
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme" >
<activity android:name=".MainActivity" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name=".StudentSerivce">
<intent-filter>
<action android:name="student.query"/>
<category android:name="android.intent.category.DEFAULT"></category>
</intent-filter>
</service>
</application>
</manifest>
二、client端代码。
1.创建aidl文件 (注意包名要与service进程中aidl包名相同)
2.aidl接口代码。
// StudentAidl.aidl
package com.goway.test_aidl_service;
// Declare any non-default types here with import statements
interface StudentAidl {
void basicTypes(int anInt, long aLong, boolean aBoolean, float aFloat,
double aDouble, String aString);
String getStudent(int no);
}
3.client 端mainactivity中的代码。
注意:要将隐式意图转为 显式意图
package com.goway.test_aidl_service;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.os.IBinder;
import android.os.RemoteException;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private ServiceConnectionImpl sci ;
private StudentAidl student;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent intent = new Intent();
intent.setAction("student.query");
Intent eintent = new Intent(getExplicitIntent(this,intent));
sci = new ServiceConnectionImpl();
bindService(eintent, sci, BIND_AUTO_CREATE);
}
public void queryOnClick(View v) {
EditText et_no_text = (EditText) findViewById(R.id.et_no_text);
String no = et_no_text.getText().toString();
String sname;
try {
sname = student.getStudent(Integer.parseInt(no));
TextView tv_no = (TextView) findViewById(R.id.tv_no);
tv_no.setText(sname);
} catch (NumberFormatException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (RemoteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
class ServiceConnectionImpl implements ServiceConnection {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
student = StudentAidl.Stub.asInterface(service);
}
@Override
public void onServiceDisconnected(ComponentName name) {
student = null;
}
}
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
if(sci != null) {
unbindService(sci);
}
}
/**
* 将隐式意图转为显式意图
* @param context
* @param implicitIntent
* @return
*/
public static Intent getExplicitIntent(Context context, Intent implicitIntent) {
// Retrieve all services that can match the given intent
PackageManager pm = context.getPackageManager();
List<ResolveInfo> resolveInfo = pm.queryIntentServices(implicitIntent, 0);
// Make sure only one match was found
if (resolveInfo == null || resolveInfo.size() != 1) {
return null;
}
// Get component info and create ComponentName
ResolveInfo serviceInfo = resolveInfo.get(0);
String packageName = serviceInfo.serviceInfo.packageName;
String className = serviceInfo.serviceInfo.name;
ComponentName component = new ComponentName(packageName, className);
// Create a new intent. Use the old one for extras and such reuse
Intent explicitIntent = new Intent(implicitIntent);
// Set the component to be explicit
explicitIntent.setComponent(component);
return explicitIntent;
}
}
4.client布局文件
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.goway.test_aidl_service.MainActivity">
<EditText
android:id="@+id/et_no_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="text"
/>
<Button
android:id="@+id/btn_query"
android:onClick="queryOnClick"
android:text="查询"
android:layout_below="@id/et_no_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/tv_no"
android:layout_below="@id/btn_query"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</RelativeLayout>
5.client 的 androidMinifast文件
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.goway.test_aidl_service" >
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme" >
<activity android:name=".MainActivity" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name=".StudentSerivce">
<intent-filter>
<action android:name="student.query"/>
<category android:name="android.intent.category.DEFAULT"></category>
</intent-filter>
</service>
</application>
</manifest>