Regarding this line:

    val pm = 
getSystemService(Context.POWER_SERVICE).asInstanceOf[PowerManager]

I want to share a neat abstraction I wrote around it:

https://github.com/pfn/android-common/blob/master/src/main/scala/AndroidConversions.scala#L176

Basically, this lets you do:

context.systemService[PowerManager] and get a PowerManager object directly, 
no cast, no need to use the 'POWER_MANAGER' constant.

android-common is just a set of small utilities I use across all my 
projects (there are implicits for converting functions to android callback 
types, but they're incomplete and I don't like them. -- I would really like 
to use scala-sam instead, but intellij has no support, and it's hidden 
under -Xexperimental in 2.11)

The library can be used directly, or just copy-paste the parts you want.

On Wednesday, December 3, 2014 8:09:40 AM UTC-8, Daniel Skinner wrote:
>
> 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.
>
> 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`.
>
> 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.
>

-- 
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.

Reply via email to