2014-12-03 17:09 GMT+01:00 Daniel Skinner <dan...@dasa.cc>: > I'm still learning a few things about scala but I'm wondering if anyone > has insight on accessing java statics from scala in relation to the Android > API. This rarely comes up but here's two examples. > > From https://developer.android.com/training/scheduling/wakelock.html > > The support library has a WakefulBroadcastReceiver that contains two > static methods. If i create a scala class that extends this, I can't access > the static methods for starting a wakeful service and finishing it. >
What do you mean you “can’t access” them? You can imagine Java static methods as being roughly methods in a class companion object. You don’t inherit them, the way you wouldn’t inherit methods from a companion object as they don’t belong to the corresponding class, and they don’t operate on a particular instance. You need to access those methods by calling WakefulBroadcastReceiver.startWakefulService(…). In Java, being able to call them as if they were methods of your class is merely a convenience, they can’t access any of your non-static methods or fields anyway. . > > Likewise, > http://developer.android.com/reference/android/os/PowerManager.WakeLock.html > I can't declare a WakeLock type with something like `val wakeLock: WakeLock > = null`. > Again, what do you mean by “I can’t declare a WakeLock”? What happens if you do? I don’t see any reason you couldn’t declare one and assign it the null value (although an Option[WakeLock] would probably be best here). > > I have an intent service and worked around this with the following that I > access in onCreate and onDestroy (letting the type be inferred) > > lazy val (wakeLock, wifiLock) = { > val pm = > getSystemService(Context.POWER_SERVICE).asInstanceOf[PowerManager] > val wakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, > "ClientWakeLock") > wakeLock.acquire() > val wm = > getSystemService(Context.WIFI_SERVICE).asInstanceOf[WifiManager] > val wifiLock = wm.createWifiLock(WifiManager.WIFI_MODE_FULL, > "ClientWifiLock") > wifiLock.acquire() > (wakeLock, wifiLock) > } > > but I'm wondering if there's some other way so that I can declare the type. > What is wrong with the type (WakeLock, WifiLock)? Sam -- You received this message because you are subscribed to the Google Groups "scala-on-android" group. To unsubscribe from this group and stop receiving emails from it, send an email to scala-on-android+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.