利用接口调用服务中特定的方法
2023-09-14 09:00:42 时间
利用接口调用服务中特定的方法,好处:是为了保护服务中的其他方法
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);
}
}
相关文章
- Go——方法和接口
- 超详细解读Java接口:模块通信协议以及默认方法和静态方法
- Linux实现文件行分割的方法(linux行分割文件)
- Hibernate Criteria接口 createCriteria方法:创建一个新的Criteria对象
- Hibernate Criteria接口 setProjection方法:设置查询的投影列
- Hibernate Query接口 uniqueResult方法:单值检索
- 查看MySQL版本:简洁方法(怎么查mysql版本)
- MySQL中遍历结果集的方法(mysql遍历结果集)
- 解密Oracle BDUMP:深入探索技术背景及运用方法(oraclebdump)
- VB通过ODBC连接访问Oracle数据库的方法(vb访问oracle)
- 解决mongodb服务无法启动的正确方法(mongodb服务无法启动)
- MySQL中使用CREATE SEQUENCE建立序列的步骤和方法(mysql上建序列怎么建)
- Oracle中实现数据行转列的方法(oracle中数据行转列)
- Oracle中复制一个表的简便方法(oracle中复制一个表)
- 在ASP中使用均速分页法提高分页速度的方法
- JavaScript调用Activex控件的事件的实现方法
- SQL重复记录问题的处理方法小结
- iisreset不支持此接口的多种解决方法分享
- java线程之使用Runnable接口创建线程的方法
- 解析二进制流接口应用实例pack、unpack、ord函数使用方法
- C#byte数组与Image相互转换的方法
- 应用程序操作NorFlash示例代码分享(norflash接口使用方法)
- 动态创建script在IE中缓存js文件时导致编码的解决方法
- JS实现同时搜索百度和必应的方法