Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/199.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
带有scala的android绑定服务-无法使Binder.getService工作_Android_Scala_Service - Fatal编程技术网

带有scala的android绑定服务-无法使Binder.getService工作

带有scala的android绑定服务-无法使Binder.getService工作,android,scala,service,Android,Scala,Service,我正试图让一个用scala开发的android应用程序正常工作。 但是我在编译过程中遇到了一个错误 [ERROR] TestService.scala:49: error: type mismatch; [INFO] found : .services.TestService.type (with underlying type object .services.TestService) [INFO] required: .services.TestService 服务 c

我正试图让一个用scala开发的android应用程序正常工作。 但是我在编译过程中遇到了一个错误

[ERROR] TestService.scala:49: error: type mismatch;
[INFO]     found   : .services.TestService.type (with underlying type object .services.TestService)
[INFO]     required: .services.TestService
服务

class TestService extends Service
{
  val mBinder: IBinder = new TestService.TestServiceBinder
  val list  = List("Linux", "Android", "iOs", "WP")

  override def onCreate 
  {
    Toast.makeText(this, TAG + " created", Toast.LENGTH_LONG).show;
    Log.d(TAG, "onCreate");
  }

  override def onDestroy
  {
    Toast.makeText(this, TAG + " destroyed", Toast.LENGTH_LONG).show;
    Log.d(TAG, "onCreate");
  }

  override def onStart(intent: Intent, startid: Int)
  {
    Toast.makeText(this, TAG + " started", Toast.LENGTH_LONG).show;
    Log.d(TAG, "onStart");
  }

  override def onBind(intent: Intent): IBinder = mBinder

  def getList: List[String] = list

}

object TestService
{
  class TestServiceBinder extends Binder
  {
    def getService: TestService = TestService.this
  }
}
活动

class HomeActivity extends Activity with FindView
{

  private final val TAG = "HomeActivity"

  lazy val buttonTest: Button = findView[Button](R.id.testButton)

  private var mService: TestService = _
  private var mBound: Boolean = false

  val mConnection: ServiceConnection = new TestServiceConnection

  override def onCreate(savedInstanceState: Bundle)
  {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.main)

    buttonTest.setOnClickListener(ButtonTestOnClickListener)

    Log.i(TAG, "onCreate")
  }

  override def onStart
  {
    super.onStart
    val intent: Intent = new Intent(this, classOf[TestService])
    bindService(intent, mConnection, Context.BIND_AUTO_CREATE);
  }

  override def onStop
  {
    super.onStop
    if(mBound)
    {
      unbindService(mConnection)
      mBound = false
    }
  }

  object ButtonTestOnClickListener extends View.OnClickListener
  {
    def onClick(v: View)
    {
      if(mBound)
      {
        Toast.makeText(v.getContext, mService.getList.toString, Toast.LENGTH_LONG).show
      }
    }
  }

  class TestServiceConnection extends ServiceConnection
  {
    def onServiceConnected(className: ComponentName, service: IBinder)
    {
      val binder = (new TestService.TestServiceBinder).asInstanceOf[TestServiceBinder]
      mService = binder.getService
      mBound = true
    }

    def onServiceDisconnected(className: ComponentName)
    {
      mBound = false
    }
  }

}
我希望任何人都能帮助我,或者给我一个很好的教程,教我如何使用scala中的绑定服务。 谢谢你的帮助。 克里斯


编辑第49行是:
def getService:TestService=TestService。这

我不确定我是否完全理解您的getService为什么看起来是这样。为什么不这样做:

def getService: TestService = new TestService
我不明白的第二件事是下面这句话

val binder = (new TestService.TestServiceBinder).asInstanceOf[TestServiceBinder]
你为什么需要在这里安装

编辑我想你必须像android文档中那样做。在同伴对象中(就像您已经实现的那样),您无法访问

class LocalService extends Service {
   private final val localBinder = new LocalBinder()
   class LocalBinder extends Binder {
      def getService: LocalService = LocalService.this
  }
}

new TestService将为我提供一个新实例。据我所知,安卓服务将由安卓操作系统启动,而Binder是某种类型的singleton。我所尝试的只是实现一个android服务,正如这里所描述的:没有,也没有成功:(因为时间太少,我转而使用java,并试图模仿scala部分的功能性。。。