第一步:创建一个远程服务,如我的RemoteService.java (记得在文件清单中声明service)
package com.itheima.remoteservice;
import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
public class RemoteService extends Service {
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
public void banzheng(){
System.out.println("李局帮你来办证");
}
}
其中public void banzheng()是我将要对其进行访问的方法,那么接下来如何访问呢?
第二步:在服务里边创建一个内部类,让其继承Binder,这样我们就可以new一个内部类对象,让其通过public IBinder onBind()方法将其返回给调用服务者;
与此同时,在内部类中,构造我们自己的方法,然后在方法内调用服务中的方法public void banzheng(),改造之后的结果如下图所示:
package com.itheima.remoteservice;
import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
public class RemoteService extends Service {
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return new furong();
}
class furong extends Binder{
public void qianXian() {
// TODO Auto-generated method stub
banzheng();
}
}
public void banzheng(){
System.out.println("李局帮你来办证");
}
}
第三步:把内部类里边的方法抽象出来
我们之后,若我们直接这样把中间对象IBinder传递过去,尽管是能传递过去,
但是在服务调用者那边,还是没法把其强转过来,因为创建服务和启动服务是在两个应用里边;
所以仍需要对其进行进一步改造,也就是:创建一个接口,把内部类里边的方法抽象出来放在我们新定义的接口里边,
然后让内部类实现这个接口;如我的接口及服务中的内部类如下:
服务中的内部类:
package com.itheima.remoteservice;
import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
public class RemoteService extends Service {
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return new furong();
}
class furong extends Binder implements PublicBusiness{
public void qianXian() {
// TODO Auto-generated method stub
banzheng();
}
}
public void banzheng(){
System.out.println("李局帮你来办证");
}
}
另外新定义一个接口:
package com.itheima.remoteservice;
public interface PublicBusiness {
void qianXian();
}
第四步:找到接口文件的目录,然后将接口的后缀名修改为 .aidl 然后刷新工程,不然看不到修改之后的效果
修改之前的效果:
修改之后的效果:
修改之后,我们看到,很多地方都出现了错误,别急,我们按照错误提示慢慢修改:
先将接口的权限修饰符public去掉,呈现的效果如下:
package com.itheima.remoteservice;
interface PublicBusiness {
void qianXian();
}
与此同时,我们看到,左侧工程的gen包下自动生成了PublicBusiness.java
进入gen包下的PublicBusiness.java的类中,我们可以看到如下一行:
第五步:修改RemoteService.java文件,这个文件就是我们的服务
通过第四步,我们得知,Stub既继承了Binder又实现了PublicBusiness接口,这样很容易,让我们想到,我们的内部类的实现
class furong extends Binder implements PublicBusiness
因此,我们让furong内部类直接去继承Stub,其实际效果如下:
package com.itheima.remoteservice;
import com.itheima.remoteservice.PublicBusiness.Stub;
import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
public class RemoteService extends Service {
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return new furong();
}
class furong extends Stub{
public void qianXian() {
// TODO Auto-generated method stub
banzheng();
}
}
public void banzheng(){
System.out.println("李局帮你来办证");
}
}
第六步:创建启动服务的调用者
我们知道,我们的服务虽然构建好,但是,需要有人来启动,所以,我们这步的任务就是创建一个服务调用者,如:
package com.itheima.runremote;
import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.view.Menu;
import android.view.View;
public class MainActivity extends Activity {
private MyserviceConn conn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
conn = new MyserviceConn();
}
public void click3(View v){
//绑定远程服务
Intent intent = new Intent();
intent.setAction("com.itheima.remote");
bindService(intent, conn, BIND_AUTO_CREATE);
}
public void click4(View v){
//解绑远程服务
unbindService(conn);
}
class MyserviceConn implements ServiceConnection{
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
}
@Override
public void onServiceDisconnected(ComponentName name) {
// TODO Auto-generated method stub
}
}
}
注:在这里,我是通过点击按钮来让触发器绑定和解绑的;
第七步:访问服务里边的方法
先在调用服务的工程里边创建一个工程,需同服务里边的包名一样
然后将RemoteService.aidl文件拷贝到创建的这个包名下,如:
最后就是在public void onServiceConnected()方法里,把Ibinder中间人对象强转成publicbusiness
然后调用publicbusiness里边的方法qianXian(),这样就达到了访问服务里边banzheng()方法的目的;
启动远程服务的示例如下:
package com.itheima.runremote;
import com.itheima.remoteservice.PublicBusiness;
import com.itheima.remoteservice.PublicBusiness.Stub;
import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.view.Menu;
import android.view.View;
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 click(View v){
//启动远程服务
Intent intent = new Intent();
intent.setAction("com.itheima.remote");
startService(intent);
}
public void click2(View v){
//停止远程服务
Intent intent = new Intent();
intent.setAction("com.itheima.remote");
stopService(intent);
}
public void click3(View v){
Intent intent = new Intent();
intent.setAction("com.itheima.remote");
bindService(intent, conn, BIND_AUTO_CREATE);
}
public void click4(View v){
unbindService(conn);
}
class MyserviceConn implements ServiceConnection{
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
//把Ibinder中间人对象强转成publicbusiness
pb = Stub.asInterface(service);
}
@Override
public void onServiceDisconnected(ComponentName name) {
// TODO Auto-generated method stub
}
}
public void click5(View v){
try {
pb.qianXian();
} catch (RemoteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
注:以上就是通过AIDL访问服务中的方法,以下是其整个代码,以便大家参考:
远程服务端:
MainActivity.java
package com.itheima.remoteservice;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
RemoteService.java
package com.itheima.remoteservice;
import com.itheima.remoteservice.PublicBusiness.Stub;
import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
public class RemoteService extends Service {
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return new furong();
}
class furong extends Stub{
public void qianXian() {
// TODO Auto-generated method stub
banzheng();
}
}
public void banzheng(){
System.out.println("李局帮你来办证");
}
}
PublicBusiness.aidl
package com.itheima.remoteservice;
interface PublicBusiness {
void qianXian();
}
布局文件如下:
<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
</RelativeLayout>
清单文件如下:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.itheima.remoteservice"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.itheima.remoteservice.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name="com.itheima.remoteservice.RemoteService">
<intent-filter >
<action android:name="com.itheima.remote"/>
</intent-filter>
</service>
</application>
</manifest>
启动远程服务端:
MainActivity.java
package com.itheima.runremote;
import com.itheima.remoteservice.PublicBusiness;
import com.itheima.remoteservice.PublicBusiness.Stub;
import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.view.Menu;
import android.view.View;
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 click(View v){
//启动远程服务
Intent intent = new Intent();
intent.setAction("com.itheima.remote");
startService(intent);
}
public void click2(View v){
//停止远程服务
Intent intent = new Intent();
intent.setAction("com.itheima.remote");
stopService(intent);
}
public void click3(View v){
Intent intent = new Intent();
intent.setAction("com.itheima.remote");
bindService(intent, conn, BIND_AUTO_CREATE);
}
public void click4(View v){
unbindService(conn);
}
class MyserviceConn implements ServiceConnection{
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
//把Ibinder中间人对象强转成publicbusiness
pb = Stub.asInterface(service);
}
@Override
public void onServiceDisconnected(ComponentName name) {
// TODO Auto-generated method stub
}
}
public void click5(View v){
try {
pb.qianXian();
} catch (RemoteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
PublicBusiness.aibl
package com.itheima.remoteservice;
interface PublicBusiness {
void qianXian();
}
layout布局文件如下:
<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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity"
android:orientation="vertical"
>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="启动远程服务"
android:onClick="click"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="停止远程服务"
android:onClick="click2"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="绑定远程服务"
android:onClick="click3"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="解绑远程服务"
android:onClick="click4"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="远程办证"
android:onClick="click5"
/>
</LinearLayout>
清单文件如下:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.itheima.runremote"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.itheima.runremote.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>