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


     

  • 相关阅读:
    4500-X验证镜像完整性
    激活4500-X RTU license
    Nexus:hardware type changed to No-Transceiver
    Bug搬运工-CSCvf74866:Webauth scaling improvements
    win10无法登陆SSG进行WEB UI管理
    FTD vs FMC
    转:思科3750交换机堆叠技术配置向导
    ROM, RAM, NVRAM and Flash Memory on Cisco Routers
    RADIUS Authentication with WPA2-Enterprise
    BUG搬运工:CSCun88303-CIMC Memory Leak : Can't SSH/HTTP to CIMC
  • 原文地址:https://www.cnblogs.com/wuyida/p/6300664.html
Copyright © 2020-2023  润新知