簡體   English   中英

具有scala的android綁定服務-無法使Binder.getService工作

[英]android bound service with scala - cant get Binder.getService working

我正在嘗試讓在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.this

我不確定我是否完全理解您的getService看起來為何。 為什么不這樣做呢:

def getService: TestService = new TestService

我不明白的第二件事是以下行

val binder = (new TestService.TestServiceBinder).asInstanceOf[TestServiceBinder]

為什么在這里需要asInstanceOf?

編輯我認為你必須像在android文檔中那樣做。 在伴隨對象(如您已實現的對象)中,您無法訪問this

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM