aidl的应用场景
2024-10-19 21:12:38
支付宝
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();
} }
最新文章
- 解决echsop兼容jquery(transport.js的冲突)的问题
- qt 自动产生 Guid 方法
- View的缩放操作--CGAffineTransformMakeScale:
- Linux环境下oracle创建和删除表空间及用户
- Malloc碎碎念
- 解决screen Cannot open your terminal &#39;/dev/pts/1&#39;问题
- 一些有用的javascript实例分析(一)
- 解决jmeter请求不成功或者报403错误
- Hangfire使用ApplicationInsigts监控
- VC++6.0连接MySQL数据库(MySQL API)
- c/c++ 表达式求值
- eos 创建两对的公钥和私钥, 钱包,交易所转账到主网,主网到交易所
- Match-----Correlation-----find_ncc_model_exposure
- 硬盘img镜像备份工具
- Chapter 1 -- UsingAndAvoidingNull
- 如何将mysql表结构导出成Excel格式的(并带备注)另附转为word表格的方法
- 如何查看Ext自带的API和示例
- pshell远程连接服务器
- poj 2992 Divisors (素数打表+阶乘因子求解)
- Gulp插件使用技巧