远程服务
为了使其他的应用程序也可以访问本应用程序提供的服务,Android系统采用了远程过程调用(Remote Procedure Call,RPC)方式来实现。与很多其他的基于RPC的解决方案一样,Android使用一种接口定义语言(Interface Definition Language,IDL)来公开服务的接口。我们知道4个Android应用程序组件中的3个(Activity、BroadcastReceiver和ContentProvider)都可以进行跨进程访问,另外一个Android应用程序组件Service同样可以。因此,可以将这种可以跨进程访问的服务称为AIDL(Android Interface Definition Language)服务。.
绑定远程服务
public class RemoveService extends Service {
@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return new MyBind();
}
@Override
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();
Log.e("remoteService", "远程服务创建了");
}
@Override
public void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
Log.e("remoteService", "远程服务销毁了");
}
private void sayHello() {
Log.e("remote", "hello");
}
public class MyBind extends IService.Stub {
public void callMethodInService() {
sayHello();
}
}
}
package com.example.remoteservice;
interface IService {
void callMethodInService();
}
我表示很无奈
public static abstract class Stub extends android.os.Binder implements com.example.remoteservice.IService
{
private static final java.lang.String DESCRIPTOR = "com.example.remoteservice.IService";
/** Construct the stub at attach it to the interface. */
public Stub()
{
this.attachInterface(this, DESCRIPTOR);
}
/**看到里面正有suub类,而且已经帮我们继承Binder并实现IService了~
最后在AndroidManifest.xml文件里为该服务定义一个action,方便远程调用该service。
<service android:name="com.example.remoteservice.RemoveService">
<intent-filter>
<action android:name="com.javy.remoteService"/>
</intent-filter>
</service>
<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:gravity="center_vertical"
tools:context="com.example.bindremoteservice.MainActivity" >
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/bind"
android:text="绑定远程服务"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/unbind"
android:text="解除绑定远程服务"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/call"
android:text="调用远程服务的方法"
/>
</LinearLayout>
布局很简单~也不用看了~
package com.example.bindremoteservice;
import com.example.remoteservice.IService;
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.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity implements OnClickListener {
private Button bind;
private Button unbind;
private Button call;
private MyConn conn;
private IService is;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}
private void initView() {
bind = (Button) findViewById(R.id.bind);
unbind = (Button) findViewById(R.id.unbind);
call = (Button) findViewById(R.id.call);
bind.setOnClickListener(this);
unbind.setOnClickListener(this);
call.setOnClickListener(this);
conn = new MyConn();
}
@Override
public void onClick(View v) {
if (v.equals(bind)) {
Intent intent = new Intent();
intent.setAction("com.javy.remoteService");
intent.setPackage("com.example.remoteservicedemo");
bindService(intent, conn, BIND_AUTO_CREATE);
}
if (v.equals(unbind)) {
unbindService(conn);
}
if (v.equals(call)) {
try {
is.callMethodInService();
} catch (RemoteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
private class MyConn implements ServiceConnection {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
Log.e("bindlog", "已绑定");
is = IService.Stub.asInterface(service);
}
@Override
public void onServiceDisconnected(ComponentName name) {
Log.e("", "已解绑");
}
}
}
嗯~挺长的代码~其实也很简单~忽略掉findViewById,忽略掉setOnclickListener剩下的代码也不多了。
if (v.equals(bind)) {
Intent intent = new Intent();
intent.setAction("com.javy.remoteService");
intent.setPackage("com.example.remoteservicedemo");
bindService(intent, conn, BIND_AUTO_CREATE);
}绑定的操作与绑定本地服务的操作差不多,不过也就是多了个setAction与setPackage。
is = IService.Stub.asInterface(service);
(留意刚才的代码哪些地方有log)现在笔者点击绑定远程服务:
5万+



被折叠的 条评论
为什么被折叠?



