android远程服务

来源:互联网 发布:淘宝上买手机靠谱吗? 编辑:程序博客网 时间:2024/05/16 19:36

客户端和被调用实现之间是通过代理模式实现的,代理类和被代理类实现同一个接口Ibinder接口。

1、服务端实现:
在android创建一个文件LoginService 继承 Service

public class QQLoginService extends Service {    class MyIBinder extends Binder implements QQLongsertfase{        @Override        public boolean login(String number, String pwd) {               if("subijun".equals(number)&&"123".equals(pwd)){              return true;            }            return false;        }//        public boolean login(String number,String pwd){//            if("subijun".equals(number)&&"123".equals(pwd)){//              return true;//            }//            return false;//        }    }    @Nullable    @Override    public IBinder onBind(Intent intent) {        return new MyIBinder();    }}实例化一个接口 ServiceConnection 进行连接服务。在onBind方法里面返回我们定义的接口实例

ServiceConnection connection=new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
Log.i(“test”,”绑定成功”);
// myBinde = (QQLoginService.MyIBinder) service;
qqLongsertfase = (QQLongsertfase)service;
}

    @Override    public void onServiceDisconnected(ComponentName name) {    }};
在Manifest文件里面进行注册

2、客户端实现:客户端的代码  两个文本输入框、一个按钮点击按钮调用远程服务端的方法执行加法操作,然后在文本控件中显示。
<EditText    android:layout_width="match_parent"    android:layout_height="wrap_content"    android:text="subijun"    android:id="@+id/EdiTtext_main_number"    /><EditText    android:layout_width="match_parent"    android:layout_height="wrap_content"    android:text="123"    android:id="@+id/EdiTtext_main_pwd"    /><Button    android:layout_width="match_parent"    android:layout_height="wrap_content"    android:text="登录"    android:onClick="login"    />

“`

0 0