Kotlin写一个基类Application
程序员文章站
2022-06-24 19:16:22
最近又开始了Kotlin的学习,今天写一个BaseActivity,包括公共的context,然后kotlin获取包名,进程名,版本名称class myApplication: Application(){ //声明公共变量和java中public static一样 companion object{ var context: Context ?= null } override fun onCreate() { super.onCrea...
最近又开始了Kotlin的学习,今天写一个BaseActivity,包括公共的context,然后kotlin获取包名,进程名,版本名称
class myApplication: Application(){ //声明公共变量和java中public static一样 companion object{ var context: Context ?= null } override fun onCreate() { super.onCreate() context = this //获取包名 var packageName = this.packageName //获取进程名 var processName = getProcessName(android.os.Process.myPid()) //获取版本名称 var versionName = getVersionName() Log.e("motejia",packageName+"========"+processName+"========="+versionName) } fun getVersionName() : String{ var packageManager : PackageManager = packageManager var info = packageManager.getPackageInfo(packageName,0) return info.versionName } fun getProcessName(pid : Int) : String{ var bufferReader :BufferedReader ?= null bufferReader = BufferedReader(FileReader("/proc/" + pid + "/cmdline")) var processName = bufferReader.readLine() if(!TextUtils.isEmpty(processName)){ processName = packageName.trim() } bufferReader.close() return processName } }
本文地址:https://blog.csdn.net/Motejia/article/details/108578547
上一篇: C++ 右值引用