2012-03-09 61 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