利用接口调用服务中特定的方法
2023-09-14 08:59:11 时间
利用接口调用服务中特定的方法,好处:是为了保护服务中的其他方法
public interface PayInterface {
public void interface_pay();
}
public class PayService extends Service {
class MyBinder extends Binder implements PayInterface{
@Override
public void interface_pay() {
pay();
}
}
@Override
public IBinder onBind(Intent intent) {
MyBinder binder = new MyBinder();
return binder;
}
@Override
public void onCreate() {
super.onCreate();
}
public void pay(){
System.out.println("支付服务");
}
}
public class MainActivity extends AppCompatActivity {
private Intent intent;
private MyServiceConnection serviceConnection;
private PayInterface payInterface;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
intent = new Intent(this,PayService.class);
serviceConnection = new MyServiceConnection();
}
public void bind(View view){
bindService(intent,serviceConnection,BIND_AUTO_CREATE);
}
public void pay(View view){
if(payInterface!=null){
payInterface.interface_pay();//调用支付功能。
}
}
class MyServiceConnection implements ServiceConnection{
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
payInterface = (PayInterface) service;
}
@Override
public void onServiceDisconnected(ComponentName name) {
}
}
@Override
protected void onDestroy() {
super.onDestroy();
unbindService(serviceConnection);
}
}
相关文章
- yii2 在控制器重写 beforeAction() 方法时,behaviors 失效、不可用的原因
- Spring钩子方法和钩子接口的使用详解
- 使用JDBCTemplate实现与Spring结合,方法公用 ——接口(BaseDao)
- jenkins : 多个job按顺序构建的实现方法-mutljob plugin
- Atitit 非结构化数据管理法 目录 1. 什么是非结构化数据?1 2. 对非结构化数据也即对全文数据的搜索主要有两种方法:2 2.1. 顺序扫描法(Serial Scanning):2 2
- 池与线程池 技术点 目录 1. 线程池作用: 提升性能1 2. 使用流程1 3. 线程与线程池的监控 jvisual1 4. 线程常用方法2 5. 线程池相关概念2 5.1. 队列
- Py之pyserial:Python的pyserial库的简介、安装、使用方法之详细攻略
- 【解决if else】还在用策略模式解决 if-else?Map+函数式接口方法才是YYDS!
- 【华为云技术分享】详解浏览器跨域的几种方法
- RecyclerView实现一个页面有多种item,每个item有多个view,并且可以让任意item的任意view自定义监听,通过接口方法进行触发操作
- Java泛型:泛型类、泛型接口和泛型方法
- 009-jdk1.8版本新特性一-展方法,Lambda表达式,函数式接口、方法引用构造引用
- Dart基础第13篇: 泛型、泛型方法 、泛型类、泛型接口
- Java面向对象编程(方法、类、对象、继承、修饰符、多态、抽象类、接口、形参、内部类)
- Go接口(Interface)的使用方法
- 24种提高ASP.NET Web 应用性能的方法