在Android studio 实现远程服务(Service)AIDL的方法与在eclipse中实现有些不同,Android studio中自带了AIDL创建的方式,而eclipse中需要手动创建;
下面简单介绍实现远程服务(Service)AIDL的方法;
一、 创建服务工程应用项目 RemoteDemo
2. 创建 RemoteService 服务类
3. 创建AIDL文件, PublicBusiness 接口类
在创建之前在目录 build-->generated-->source-->aidl-->androidTest-->debug下面发现还没有任何文件
创建 PublicBusiness AIDL
创建后会出现如下 : 左边的main目录下出现aidl文件夹和PublicBusiness.aidl文件和包,将PublicBusiness.aidl文件中的basicTypes 方法删除
4. 在 PublicBusiness接口类中写入提供远程调用的方法,在确保PublicBusiness.aidl所在的包名与项目中默认的包名一致,如果一致,点击 Build-->Make Project(也可以直接点下图箭头指向的地方),生成相应的java文件。如果不一致,则改aidl的包名,改成一致,再点击生成,生成效果如图。
此时会发现在目录 build-->generated-->source-->aidl-->androidTest-->debug下面出现了编译的文件;
二、 创建使用远程服务中的工程应用项目 UseRemoteDemo工程:
1. 将 RemoteDemo中main目录下的aidl文件夹全部复制到 UseRemoteDemo工程 的main目录下即可;
三、下面是代码:
服务工程应用项目 RemoteDemo 代码:
注册服务:
<span style="font-family:KaiTi_GB2312;font-size:18px;"><service
android:name=".RemoteService"
android:enabled="true"
android:exported="true" >
<intent-filter>
<action android:name="com.example.RemoteService"/>
</intent-filter>
</service></span>
MainActivity.java
<span style="font-family:KaiTi_GB2312;font-size:18px;">package com.example.yu_longji.remotedemo;
import android.app.Activity;
import android.os.Bundle;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
</span>
RemoteService.java
<span style="font-family:KaiTi_GB2312;font-size:18px;">package com.example.yu_longji.remotedemo;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.os.RemoteException;
public class RemoteService extends Service {
@Override
public IBinder onBind(Intent intent) {
System.out.println("调用了bind方法");
return new XiaoMi();
}
@Override
public boolean onUnbind(Intent intent) {
System.out.println("调用了onUnbind()方法");
return super.onUnbind(intent);
}
@Override
public void onCreate() {
super.onCreate();
System.out.println("调用了onCreate()方法");
}
@Override
public void onDestroy() {
super.onDestroy();
System.out.println("调用了onDestroy()方法");
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
System.out.println("调用了onStartCommand()方法");
return super.onStartCommand(intent, flags, startId);
}
//中间人对象
class XiaoMi extends PublicBusiness.Stub{
@Override
public void qianXian() throws RemoteException {
// 调用服务的banzheng方法
RemoteService.this.banzheng();
}
}
public void banzheng(){
System.out.println("李局帮你来办证");
}
}
</span>
PublicBusiness.aidl 接口类
<span style="font-family:KaiTi_GB2312;font-size:18px;">// PublicBusiness.aidl
package com.example.yu_longji.remotedemo;
// Declare any non-default types here with import statements
interface PublicBusiness {
/**
* Demonstrates some basic types that you can use as parameters
* and return values in AIDL.
*/
void qianXian();
}
</span>
使用远程服务中的工程应用项目 UseRemoteDemo工程
布局代码:
<span style="font-family:KaiTi_GB2312;font-size:18px;"><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:gravity="center"
android:orientation="vertical"
tools:context=".MainActivity">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="click1"
android:text="启动远程服务" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="click2"
android:text="停止远程服务" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="click3"
android:text="绑定远程服务" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="click4"
android:text="解绑远程服务" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="click5"
android:text="远程办证" />
</LinearLayout>
</span>
MainActivity.java
<span style="font-family:KaiTi_GB2312;font-size:18px;">package com.example.yu_longji.useremotedemo;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
import android.view.View;
import com.example.yu_longji.remotedemo.PublicBusiness;
public class MainActivity extends Activity {
private MyserviceConn conn;
PublicBusiness pb;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
conn = new MyserviceConn();
}
//绑定远程服务
public void click3(View view){
Intent intent = new Intent();
intent.setAction("com.example.RemoteService");
bindService(intent, conn, BIND_AUTO_CREATE);
}
//解绑远程服务
public void click4(View view){
Intent intent = new Intent();
intent.setAction("com.example.RemoteService");
unbindService(conn);
}
//实现ServiceConnection接口
public class MyserviceConn implements ServiceConnection {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
// 使用aidl中自动生成的方法来强转
//把Ibinder中间人对象强转成publicbusiness
pb = PublicBusiness.Stub.asInterface(service);
}
@Override
public void onServiceDisconnected(ComponentName name) {
}
}
//启动远程服务
public void click1(View view){
Intent intent = new Intent();
intent.setAction("com.example.RemoteService");
startService(intent);
}
//停止远程服务
public void click2(View view){
Intent intent = new Intent();
intent.setAction("com.example.RemoteService");
stopService(intent);
}
//远程办证
public void click5(View view){
//调用远程服务的qianXian方法
try {
pb.qianXian();
} catch (RemoteException e) {
e.printStackTrace();
}
}
}
</span>
从RemoteDemo项目中复制的PublicBusiness.aidl文件内容不变;
运行后的结果: