一。服务端和客户端的中介
1.创建一个aidl文件
2.代码
1 2 3 4 5 6 7 8 9 10 11 |
/** 一。 * 在客户端创建一个和服务端一样的,黄牛模板所在的目录 * 把服务端的aidl黄牛模板拷贝进来 */ package com.cuiweiyou.myservice; interface IScalper { void callMethodInService(int money); } |
二。创建服务器
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
/** * 二。创建一个服务 * 铁倒部。内部养着一个投机倒把者 */ public class RailroadService extends Service { /** 三。铁倒部的基本功能 */ private void methodInService(){ Toast.makeText(this, "铁倒部让我卖给你票", 0).show(); } /** 四。铁倒部根据模板创建的标准黄牛 * 继承自/gen/IScaplper/Stub类 */ private class Scalper extends IScalper.Stub{ /** 五。实现倒票的基本功能 */ public void callMethodInService(int money){ // 车票的实际价格 if(money>=50){ methodInService(); /** 六。通过黄牛实现铁倒部的功能 */ }else{ Toast.makeText(getApplicationContext(), "多准备点钱。", 0).show(); } } } /** 七。有人调用服务,买票时,提供黄牛 */ @Override public IBinder onBind(Intent intent) { System.out.println("有人想买票"); return new Scalper(); } } |
三。注册服务
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
<?xml version="1.0" encoding="utf-8"?> <manifest > <application > <activity android:name="com.cuiweiyou.myservice.MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <!-- 八。注册对外(远程)服务 --> <service android:name="com.cuiweiyou.myservice.RailroadService" > <intent-filter> <!-- 对外远程服务名称 --> <action android:name="com.cuiweiyou.myservice.remoteRailroadService" /> </intent-filter> </service> </application> </manifest> |
四。客户端的中介
五。远程用户
1.布局
2.主程
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
public class MainActivity extends ActionBarActivity { /** 二。声明远程服务连接器 **/ private MyConn conn; /** 三。声明一个驻外办公的黄牛 * /gen/其在国内时的办公室/IScalper黄牛模板类 **/ private IScalper scalper; /** 远程服务连接器 **/ private class MyConn implements ServiceConnection{ /** 连接成功时,得到驻外办公的黄牛 **/ @Override public void onServiceConnected(ComponentName name, IBinder service) { // 黄牛附体 scalper = IScalper.Stub.asInterface(service); } @Override public void onServiceDisconnected(ComponentName name) { System.out.println("什么情况"); } } /** * 四。绑定远程服务 */ public void bind(View view){ Intent intent = new Intent(); // 1.调用服务意图:连接的服务是服务端提供的远程服务名 intent.setAction("com.cuiweiyou.myservice.remoteRailroadService"); // 2.服务连接器 conn = new MyConn(); // 3.绑定服务 bindService(intent, conn, BIND_AUTO_CREATE); } /** 调用服务的功能 * 绑定成功以后 **/ public void call(View view){ try { // 通过黄牛远程调用铁倒部卖票 scalper.callMethodInService(10); } catch (RemoteException e) { e.printStackTrace(); } } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } @Override protected void onDestroy() { // 本客户端销毁,即解除和服务的关系 unbindService(conn); super.onDestroy(); } } |
声明
本文由崔维友 威格灵 cuiweiyou vigiles cuiweiyou 原创,转载请注明出处:http://www.gaohaiyan.com/302.html
承接App定制、企业web站点、办公系统软件 设计开发,外包项目,毕设