`

servicer

 
阅读更多
TestService类一定要继承Service

开启服务:
Intent i = new Intent(this, TestService.class); 
this.startService(i);


  /**
   * 绑定的意思是具有共同的生命周期
   */
    private void bindService() { 
        Intent i = new Intent(this, TestService.class); 
      //  绑定后 如果TestServiceHolder启动,随后在service就会启动,
        //而且在TestServiceHolder退出的时候service就会停止
         bindService(i, _connection, Context.BIND_AUTO_CREATE); 
         _isBound = true; 
    }

  private ServiceConnection _connection = new ServiceConnection() { 
   
        public void onServiceConnected(ComponentName className, IBinder service) {   
       
            _boundService = ((TestService.LocalBinder)service).getService(); // 得到绑定实例,以变解除绑定
             
          
        } 
 
        public void onServiceDisconnected(ComponentName className) { 
            // unexpectedly disconnected,we should never see this happen. 
            _boundService = null; 
         
        } 
    }; 
//解除绑定
   private void unbindService() { 
        if (_isBound) { 
            unbindService(_connection); 
            _isBound = false; 
        } 
    } 
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics