• 利用接口调用服务中特定的方法


    利用接口调用服务中特定的方法,好处:是为了保护服务中的其他方法

    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);
        }
    }

     

  • 相关阅读:
    前端开发小结(持续更新)
    UDP 通讯及内部分析(合集)
    困扰我三天的问题
    Clang Format Style Options (.clang-format 配置文件文档)
    关于共享库的那些事儿
    如何在VMWare的NAT模式下使用traceroute(解析vmnat的行为)
    Linux网络配置
    Ansible Ad-Hoc命令集
    Ansible基础使用
    Ansible部署及配置介绍
  • 原文地址:https://www.cnblogs.com/anni-qianqian/p/5402442.html
Copyright © 2020-2023  润新知