android(41)(支付宝服务和捕鱼达人)

1.支付宝服务:
package com.itheima.alipay;
interface ISafePay{
    boolean callPay(long time,String pwd,double money);
}
文件名:ISafePay.aidl
public class SafePayService extends Service {

    @Override
    public IBinder onBind(Intent intent) {
        System.out.println("服务被绑定 onbind");
        return new MyBinder();
    }
    /** * 安全支付的方法 */
    private boolean pay(long time,String pwd,double money){
        if("123".equals(pwd)){
            return true;
        }else{
            return false;
        }
    }

    private class MyBinder extends ISafePay.Stub{
        /** * 调用安全支付的逻辑 */
        @Override
        public boolean callPay(long time, String pwd, double money)
                throws RemoteException {
            return pay(time, pwd, money);
        }

    }

    @Override
    public void onCreate() {
        System.out.println("oncreate支付宝服务被创建,一直在后台运行,检查手机的安全状态");
        super.onCreate();
    }
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        System.out.println("服务onstart");
        return super.onStartCommand(intent, flags, startId);
    }

    @Override
    public boolean onUnbind(Intent intent) {
        System.out.println("onunbind");
        return super.onUnbind(intent);

    }
    @Override
    public void onDestroy() {
        System.out.println("ondestory支付宝服务被销毁");
        super.onDestroy();
    }

}
2.捕鱼达人:
利用支付宝服务
package com.itheima.alipay;
interface ISafePay{
    boolean callPay(long time,String pwd,double money);
}
文件名:
ISafePay.aidl
业务逻辑:
public class MainActivity extends Activity {
    private ISafePay iSafePay;
    private MyConn conn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
// Intent intent = new Intent();
// intent.setAction("com.itheima.alipay");
// startService(intent);
        //保证服务长期后台运行。

    }

    public void start(View view){
        Intent intent = new Intent();
        intent.setAction("com.itheima.alipay");
        startService(intent);
    }

    public void stop(View view){
        Intent intent = new Intent();
        intent.setAction("com.itheima.alipay");
        stopService(intent);
    }
    public void bind(View view){
        Intent intent = new Intent();
        intent.setAction("com.itheima.alipay");
        conn = new MyConn();
        bindService(intent, conn, BIND_AUTO_CREATE);//异步的操作
    }
    public void unbind(View view){
        unbindService(conn);
    }



    public void click(View view){
        Intent intent = new Intent();
        intent.setAction("com.itheima.alipay");
        conn = new MyConn();
        bindService(intent, conn, BIND_AUTO_CREATE);//异步的操作
        //绑定服务调用服务的方法。

    }

    private class MyConn implements ServiceConnection{
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            iSafePay = ISafePay.Stub.asInterface(service);
            try {
                boolean result = iSafePay.callPay(System.currentTimeMillis(), "123", 3.52f);
                if(result){
                    Toast.makeText(getApplicationContext(), "支付成功,获取大炮弹", 0).show();
                }else{
                    Toast.makeText(getApplicationContext(), "支付失败,请重试", 0).show();
                }
// unbindService(conn);
// conn = null;
            } catch (RemoteException e) {
                e.printStackTrace();
            }
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {

        }
    }
}

你可能感兴趣的:(支付宝,捕鱼达人)