您的位置:首页 > 移动开发 > Android开发

Android Service 调用远程服务中的方法

2017-03-18 21:25 344 查看

项目简介:

该项目是掉调用远程服务中的方法

详细介绍:

在 Rservice_C_远程服务 项目中,有一个BossService服务,该服务中有business(),要求在 Rservice_C_启动远程服务 该项目中启动BossService服务,然后调用business方法。

如何启动远程服务:

(1)在 Rservice_C_远程服务创建一个内部类PublicRelations,该类中创建一个方法调用BossService中的方法

(2)把内部类PublicRelations需要对外开放的方法抽取成一个接口PublicBusiness.java,然后直接把PublicBusiness.java该文件后缀名改为aidl。其中aidl为android interface definition language,这是android中专门用于进程之间数据的调用

(3)把PublicBusiness.aidl连同所在的包一起复制的 Rservice_C_启动远程服务项目 中

(4)在Rservice_C_启动远程服务项目 中把IBinder强转成PublicBusines对象Stub.asInterface(service);

注意:

1.在调用远程服务之前,被调用的服务一定要先部署上,否则会导致无法找到服务的错误

2.在调用远程服务的方法之前,需要先启动远程服务,然后在绑定,在调用方法

步骤:

1.创建一个名为 Rservice_C_远程服务 的Android项目,在该项目中创建一个java类,继承Service:

public class BossService extends Service {

@Override
public IBinder onBind(Intent intent) {
// 返回内部类
return new PublicRelations();
}

@Override
public void onCreate() {
super.onCreate();
Log.i("HHH", "service创建");
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.i("HHH", "service开启");
return super.onStartCommand(intent, flags, startId);
}

@Override
public boolean onUnbind(Intent intent) {
Log.i("HHH", "service解除绑定");
return super.onUnbind(intent);
}

@Override
public void onDestroy() {
Log.i("HHH", "service销毁");
super.onDestroy();
}

// 谈生意
public void business() {
Log.i("HHH", "谈谈生意");
}

/**
* 1.该中间人(PublicRelations)为远程服务所用
* 2.首先创建一个PublicBusiness接口,让中间人类实现该接口
* 3.然后直接把该接口后缀名改为.aidl
* 4.PublicBusiness.aidl会自动在gen目录下生成一个PublicBusiness.java文件
* 5.让PublicRelations 继承stub 6.在远程服务的应用中把PublicBusiness.aidl粘贴上,要把包一起复制,
*/
class PublicRelations extends Stub {

@Override
public void businessCooperation() {
Log.i("HHH", "调用中间人方法");
business();
}

}

}


该Service要在清单文件中注册:

<service android:name="hhh.exercise.rservice_c_service.BossService" >
<intent-filter>
<action android:name="hhh.exercise.Boss" />
</intent-filter>
</service>


2.创建另一个Android项目,名为:Rservice_C_调用远程服务。首先把Rservice_C_远程服务 中的PublicBusiness.aidl文件直接复制到Src文件夹下,要连同包名一起复制,否则会出错。

2.1创建布局文件,在布局文件中放几个按钮,用于演示服务的启动,绑定,调用服务中的方法:

<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"
tools:context="hhh.exercise.rservice_c_start.MainActivity" >

<Button
style="@style/whc"
android:onClick="start"
android:text="启动服务"
android:textColor="#00ff66" />

<Button
style="@style/whc"
android:onClick="onclick"
android:text="绑定并调用远程服务中的方法"
android:textColor="#990022" />

<Button
style="@style/whc"
android:onClick="unbind"
android:text="解除绑定"
android:textColor="#990022" />
<Button
style="@style/whc"
android:onClick="end"
android:text="停止服务"
android:textColor="#1f9f66" />

</LinearLayout>


其中style=”@style/whc”样式是在style.xml定义的:

<style name="whc">
<item name="android:layout_width">match_parent</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:textSize">30sp</item>
</style>


2.2在MainActivity中写入一下代码:

public class MainActivity extends Activity {

private PublicBusiness pb;
private MyServiceConnection conn;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}

// 启动服务
public void start(View view) {
Intent intent = new Intent();
intent.setAction("hhh.exercise.Boss");
startService(intent);
}

// 停止服务
public void end(View view) {
Intent intent = new Intent();
intent.setAction("hhh.exercise.Boss");
stopService(intent);
}

// 解绑服务
public void unbind(View view) {
unbindService(conn);
}

// 绑定服务并调用服务中的方法
public void onclick(View view) {
Intent intent = new Intent();
intent.setAction("hhh.exercise.Boss");
conn = new MyServiceConnection();
bindService(intent, conn, BIND_AUTO_CREATE);

try {
pb.businessCooperation();
} catch (Exception e) {
Log.i("HHH", "出现错误");
e.printStackTrace();
}
}

class MyServiceConnection implements ServiceConnection {

@Override
public void onServiceConnected(ComponentName name, IBinder service) {
// 把IBinder强转成PublicBusines对象
pb = Stub.asInterface(service);

}

@Override
public void onServiceDisconnected(ComponentName name) {
}
}
}


最后,一定要先把 Rservice_C_远程服务项目部署上,启动一下,然后再部署Rservice_C_调用远程服务项目,否则会发现点击按钮时不会有任何效果的,因为找不到Service。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
相关文章推荐