zoukankan      html  css  js  c++  java
  • 利用接口调用服务中特定的方法

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

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

     

  • 相关阅读:
    一小时学会前端工程化
    lodash学习资料
    关于《冬天时我喜欢靠近温暖的事》这首歌 (民谣在路上)
    往后余生(简单的歌词分享)
    如果觉得活的很累不妨进来看看(生活应该简简单单)
    《大学》全文及白话翻译
    原型设计模式 Prototype
    解释器模式 Interpreter
    copy on write,代理模式
    ado.net
  • 原文地址:https://www.cnblogs.com/anni-qianqian/p/5402442.html
Copyright © 2011-2022 走看看