zoukankan      html  css  js  c++  java
  • Activity通过bindService启动Service后Activity和Service之间的通信!

    最近在看同一个程序中Service的两种驱动方式时,起以Bind启动然后可以进行ServiceActivity之间的相互通信。一直没看明白,在翻看SDK时发现一个例子,特别摘抄如下:

    这个时BindingService继承自Activity,然后通过点击按钮来启动Service

    public class BindingService extends Activity {
    
       
    
    private static final String TAG = "BindingService";
    
        // mService负责接收Service传过来的对象
    
    LocalService mService;
    
    boolean mBound = false;
    
    Button bindBtn;
    
        @Override
    
        public void onCreate(Bundle savedInstanceState) {
    
            super.onCreate(savedInstanceState);
    
            setContentView(R.layout.main);
    
            
    
            bindBtn = (Button) findViewById(R.id.bindBtn);
    
            bindBtn.setOnClickListener(new OnClickListener(){
    
    @Override
    
    public void onClick(View v) {
    
    // TODO Auto-generated method stub
    
    Log.i(TAG, "OnClick....");
    
    if(mBound) {
    
    int num = mService.getRandomNumber();
    
    Toast.makeText(BindingService.this, num+"", Toast.LENGTH_LONG).show();
    
    }
    
    }
    
             
    
            });
    
        }
    
    @Override
    
    protected void onStart() {
    
    super.onStart();
    
    // Bind to LocalService
    
    Intent intent = new Intent(this, LocalService.class);
    
    Log.i(TAG, "onStart.....onBind");
    
    bindService(intent, mConnection, Service.BIND_AUTO_CREATE);
    
    }
    
    @Override
    
    protected void onStop() {
    
    super.onStop();
    
    // UnBind from the service
    
    if(mBound) {
    
    unbindService(mConnection);
    
    mBound = false;
    
    }
    
    }
    
    // Defines callbacks for service binding, passed to bindService()
    
    private ServiceConnection mConnection = new ServiceConnection() {
    
    @Override
    
          // 在Service断开的时候调用这个函数
    
    public void onServiceDisconnected(ComponentName name) {
    
    // 另外说明在这里打log或者是使用Toast都不能显示,不知道是怎么回事
    
    mBound = false;
    
    }
    
    @Override
    
    // 在Service连接以后调用这个函数
    
    public void onServiceConnected(ComponentName name, IBinder service) {
    
    // we have bound to LocalService, cast the IBinder and get LocalService instance
    
    // 获得Service中的Binder对象
    
    // 另外说明在这里打log或者是使用Toast都不能显示,不知道是怎么回事
    
    LocalBinder binder = (LocalBinder) service;
    
    // 通过Binder对象获得Service对象,然后初始化mService
    
    mService = binder.getService();
    
    Log.i(TAG, "onServiceConnected.....");
    
    System.out.println("onServiceConnected.....");
    
    mBound = true;
    
    }
    
    };
    
    }
    
    public class LocalService extends Service {
    
    // Binder given to clients
    
    // 返回一个Binder对象
    
    private final IBinder mBinder = new LocalBinder();
    
    // Random number generator
    
    private final Random mGenerator = new Random();
    
    /***
    
     * Class used for the client Binder.Because we know this service always
    
     * runs in the same process as its clients
    
     */
    
    public class LocalBinder extends Binder {
    
    LocalService getService() {
    
    // return this instance of LocalService so clients can call public method
    
    return LocalService.this;
    
    }
    
    }
    
    @Override
    
    // 如果是通过startService()函数启动的时候,这个函数是不起作用的。
    
    // public void onServiceConnected(ComponentName name, IBinder service) 返回的Activity
    
    // IBinder service 对象
    
    public IBinder onBind(Intent intent) {
    
    // IBinder通信关键是利用Activity中的IBider对象获得service对象,然后调用service中的函数
    
    // 传给Activity一个Binder对象,通过这个Binder对象调用getService()获得Service对象
    
    return mBinder;
    
    }
    
    // method for clients
    
    public int getRandomNumber() {
    
    return mGenerator.nextInt(100);
    
    }
    
    }


     

  • 相关阅读:
    技术周刊 · 耿耿星河欲曙天 | SpaceX 上的前端架构;跨平台新选择;开源世界的新“大门”;用户推荐算法的敲门砖……
    说说SVG的feTurbulence滤镜
    机器学习进阶
    小程序与动画的故事
    技术周刊 · 迢迢山径峻 | Web 开发成长图谱;下一代前端构建技术;AI 应用下支离破碎的真实;不懂产品不会开发;虚拟货币是新时代的黄金
    从中断机制看 React Fiber 技术
    三分钟了解数字人民币
    凹凸技术揭秘:如何服务 toG 项目——数字人民币项目前端总结
    使用 Phaser3+Matter.js 实现“合成大西瓜”游戏
    痞子衡嵌入式:快速定位i.MXRT600板级设计ISP[2:0]启动模式引脚上电时序问题的方法
  • 原文地址:https://www.cnblogs.com/wuyida/p/6300664.html
Copyright © 2011-2022 走看看