当前位置: 移动技术网 > 移动技术>移动开发>Android > Kotlin入门(33)运用扩展属性

Kotlin入门(33)运用扩展属性

2018年11月10日  | 移动技术网移动技术  | 我要评论

进行app开发的时候,使用震动器要在androidmanifest.xml中加上如下权限:

    <!-- 震动 -->
    <uses-permission android:name="android.permission.vibrate" />

 

让手机震动的功能用到了震动器vibrator类,而震动器对象从系统服务vibrator_service获得,实现该功能的代码很简单,即便用java书写也只有以下两行代码:

    vibrator vibrator = (vibrator) getsystemservice(context.vibrator_service);
    vibrator.vibrate(3000);

 

两行代码看起来真没什么好简化的了,因为转换成kotlin也要下面的两行代码:

    //常规做法:从系统服务中获取震动器对象
    val vibrator = getsystemservice(context.vibrator_service) as vibrator
    vibrator.vibrate(3000)

 

虽然获取震动器的代码并不多,但是这真的真的很难记忆,首先开发者要调用getsystemservice一把,接着绞尽脑汁才能想起该服务的名称是vibrator_service,最后再强制将类型转换为vibrator。其中又是大写子母又是小写字母还有大小写混合,对于英文不溜的朋友来说,这简直是个灾难。如果只要一个琅琅上口的单词就能代表震动器,那势必为开发者省去了背诵专业英语单词的麻烦。然而两行代码还能怎么优化?倘若改造成工具类获取震动器对象,也不见得一定省事。
不过kotlin可不会善罢甘休,相反是迎难而上,因为它坐拥扩展函数这个法宝,之前我们多次见识了扩展函数的威力,比如提示窗的toast、提醒对话框的alert等等。当然获取震动器对象也能按照扩展函数来改造,比如给context添加一个扩展函数getvibrator,则该扩展函数的kotlin代码示例如下:

//获取震动器
fun context.getvibrator() : vibrator {
    return getsystemservice(context.vibrator_service) as vibrator
}

接着回到activity页面代码,实现震动功能只需下面的一行代码了:

    //利用扩展函数获得震动器对象
    getvibrator().vibrate(3000)

 

以上代码固然简化了,却仍然不是最简单的写法,看看getvibrator()方法,前面有get后面有括号,都是碍手碍脚的家伙。可去掉括号就不是函数了,而变成了属性,难不成kotlin啥时多了个扩展属性的用法?其实kotlin还真的可以实现扩展属性的功能,关键是要利用扩展函数进行移花接木,只要在kt文件中声明一个context类的新属性,同时定义该属性的get方法(get方法为扩展函数)。如此一来,外部访问该扩展属性之时,编译器会自动调用该属性的get方法,从而通过扩展函数间接实现了扩展属性。接下来依旧以震动器为例,看看如何使用kotlin代码声明扩展属性vibrator:

//获取震动器
//利用扩展函数实现扩展属性,在activity代码中即可直接使用vibrator
val context.vibrator : vibrator
        get() = getsystemservice(context.vibrator_service) as vibrator

 

现在回到activity代码,如下所示只要通过vibrator就能访问震动器的方法了:

    //利用扩展函数实现扩展属性,直接使用vibrator即可指代震动器对象
    vibrator.vibrate(3000)

 

当然要想正常访问自定义的扩展函数和扩展属性,需要在活动代码头部加上以下的导入语句:

import com.example.custom.util.vibrator

  

除了震动器之外,其它从系统服务获得对象的管理器也能照此办理,譬如通知管理器notificationmanager,按照之前的调用方式是下面的kotlin代码:

    val notifymgr = getsystemservice(context.notification_service) as notificationmanager
    notifymgr.notify(r.string.app_name, notify)

 

显然通知管理器对象的获取代码更冗长,接下来将其改造为扩展属性的方式,则相应的context扩展代码如下所示:

//获取通知管理器
//试试在activity代码中调用“notifier.notify(r.string.app_name, notify)”
val context.notifier: notificationmanager
    get() = getsystemservice(context.notification_service) as notificationmanager

 

然后原来通知管理器的两行代码便缩减为下面的一行代码了:

    notifier.notify(r.string.app_name, notify)

  

举一反三,剩下的来自系统服务的管理器统统运用扩展属性,能够更好地方便将来的开发工作。下面是几个常用管理器的扩展属性实现代码例子:

//获取下载管理器
val context.downloader: downloadmanager
    get() = getsystemservice(context.download_service) as downloadmanager
//获取定位管理器
val context.locator: locationmanager
    get() = getsystemservice(context.location_service) as locationmanager
//获取连接管理器
val context.connector: connectivitymanager
    get() = getsystemservice(context.connectivity_service) as connectivitymanager
//获取电话管理器
val context.telephone: telephonymanager
    get() = getsystemservice(context.telephony_service) as telephonymanager
//获取无线管理器
val context.wifi: wifimanager
    get() = getsystemservice(context.wifi_service) as wifimanager
//获取闹钟管理器
val context.alarm: alarmmanager
    get() = getsystemservice(context.alarm_service) as alarmmanager
//获取音频管理器
val context.audio: audiomanager
    get() = getsystemservice(context.audio_service) as audiomanager

  

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网