2012-03-09 53 views
1

我想獲得一個在scala工作開發的android應用程序。 但我編譯android綁定服務與scala - 不能得到Binder.getService工作

[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

回答

0

我不知道我完全理解爲什麼你的getService看起來,因爲它看起來。爲什麼不這樣做的:

def getService: TestService = new TestService 

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

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

爲什麼你需要asInstanceOf在這裏?

編輯我認爲你必須像android文檔中那樣做。在伴侶對象中(就像你已經實現了它),你不能訪問這個

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

新的TestService會給我一個新的實例。據我瞭解,android服務將由android os啓動,而Binder是某種單身人士。我所嘗試的只是實現一個android服務,如下所述:http://developer.android.com/guide/topics/fundamentals/bound-services.html – kitingChris 2012-03-18 14:44:49

+0

你看到我的編輯了,現在能工作嗎? – Christian 2012-03-22 13:04:36

+0

nope沒有任何工作:( ,因爲有太少的時間,我回到了Java,並試圖嘲笑我的斯卡拉部分funktionality ... – kitingChris 2012-04-02 12:12:18