支付宝
package com.tesy.alipay; import com.test.alipay.Iservice.Stub; import android.app.Service; import android.content.Intent; import android.os.Binder; import android.os.IBinder; public class PayService extends Service { //[2]我们定义的中间人对象返回 @Override public IBinder onBind(Intent intent) { return new MyBinder(); } //支付宝支付的方法 public boolean pay(String name,String pwd,int money){ System.out.println("1验证用户名和密码 "); System.out.println("2验证手机是否携带病毒"); System.out.println("3调用C语言 做一些加密处理 "); if ("abc".equals(name)&& "123".equals(pwd)&& money <5000) { return true; }else { return false; } } //定义中间人对象 private class MyBinder extends Stub{ //调用支付的方法 @Override public boolean callPay(String name, String pwd, int money) { return pay(name, pwd, money); } } }
<service android:name="com.itheima.alipay.PayService"> <intent-filter > <action android:name="com.itheima.alipay"/> </intent-filter> </service>
欢乐斗地主买豆
package com.test.ddz; import com.test.alipay.Iservice; import com.test.alipay.Iservice.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; import android.widget.Toast; public class MainActivity extends Activity { private Myconn conn; private Iservice iservice; //我们定义的中间人对象 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //[1]调用bindService 获取我们定义的中间人对象 Intent intent = new Intent(); intent.setAction("com.itheima.alipay"); conn = new Myconn(); //[2]绑定服务 bindService(intent, conn, BIND_AUTO_CREATE); } //点击按钮 买豆 public void click(View v) { try { boolean result = iservice.callPay("abcdd", "123", 100); if (result) { //支付成功 Toast.makeText(getApplicationContext(), "买豆成功", 1).show(); }else { Toast.makeText(getApplicationContext(), "买豆失败", 1).show(); } } catch (RemoteException e) { // TODO Auto-generated catch block e.printStackTrace(); } } private class Myconn implements ServiceConnection{ @Override public void onServiceConnected(ComponentName name, IBinder service) { // 获取中间人对象 iservice = Stub.asInterface(service); } @Override public void onServiceDisconnected(ComponentName name) { } } @Override protected void onDestroy() { //当Activity销毁的时候 解绑服务 unbindService(conn); super.onDestroy(); } }