Kotlin中定义静态类和静态方法
* 整个静态类:
* object StaticUtil{
* fun method()
* ...
* }
* }
* 声明为 object 的类自动生产INSTANCE静态对象,类似Java中的单例. 在其他类通过StaticUtil.其方法.
* eg: H5AppUtils.INSTANCE.addTestH5App(appListJson);
*
* 类中的部分静态方法
* class StaticUtils {
*
* companion object {
* // 包裹范围内 属于静态方法
* fun method()
* ...
* }
* }
* 注意: 类中的 companion object 表示部分方法为静态,与静态类声明事冲突的,不能共存;
* 即, 整个静态类中不能在重复定义静态方法
直接看代码示例
package com.hulk.h5
import android.content.Intent
import com.mobile.emm.debugger.Log
import org.json.JSONArray
import org.json.JSONException
import org.json.JSONObject
/**
* H5工具类
* @author: zhanghao
* @Time: 2021-04-21 19:43
*/
object H5AppUtils {
private val TAG = "H5AppUtils"
/**
* TS浏览器的查看Action
*/
val ACTION_VIEW = "com.android.intent.action.VIEW"
/**
* 接收网页浏览器启动的类型
*/
val CATEGORY_BROWSABLE = "android.intent.category.BROWSABLE"
/**
* 默认类型
*/
val CATEGORY_DEFAULT = "android.intent.category.DEFAULT"
val EXTRA_IS_H5_APP = "isH5App"
val EXTRA_SSO = "sso"
val EXTRA_SSO_APP_ID = "ssoAppId"
val H5_TEST_HTML = "/h5_test/jsoc.html"
val H5_TEST_JS = "/h5_test/ocjs.js"
val DATA_DIR_NAME = "h5"
val ADD_H5_TEST_ENABLED = true
/**
* 根据H5应用信息,得到intent
* @param info
* @return
*/
fun getH5Intent(info: H5AppInfo?): Intent? {
if (info == null) {
return null
}
if (!info.isValid()) {
Log.w(TAG, "getH5Intent: Invalid info: $info")
return null
}
val h5Intent = Intent()
//pendingIntent.setClassName(context.getPackageName(), BROWSER_ACTIVITY);
//pendingIntent.setAction(Intent.ACTION_VIEW);
h5Intent.action = ACTION_VIEW
h5Intent.addCategory(CATEGORY_BROWSABLE)
h5Intent.addCategory(CATEGORY_DEFAULT)
h5Intent.putExtra(EXTRA_IS_H5_APP, true)
h5Intent.putExtra(EXTRA_SSO, info.isSso)
h5Intent.putExtra(EXTRA_SSO_APP_ID, info.ssoAppId)
val uri = info.getUri()
h5Intent.data = uri
return h5Intent
}
//类中的 companion object 表示部分方法为静态,与静态类声明事冲突的,不能共存
//companion object {
fun getTestAssetH5App(): H5AppInfo {
var url = "http://android_asset/h5_test/jsoc2.html";
val info = H5AppInfo("", url)
info.summary = "H5 media API Test"
return info;
}
//}
fun getTestH5AppJson(): JSONObject? {
val info: H5AppInfo = getTestAssetH5App()
val appJson = JSONObject()
try {
appJson.put("appId", "66666")
appJson.put("appName", info.title)
appJson.put("link", info.url)
appJson.put("appSummary", info.summary)
return appJson
} catch (e: JSONException) {
Log.e(H5AppUtils.TAG, "getH5TestAppJson failed: $e", e)
}
return null
}
/**
* 在服务器返回应用市场的应用列表中增加
*/
fun addTestH5App(appListJson: JSONObject?): Boolean {
if (!ADD_H5_TEST_ENABLED) {
return false;
}
if (appListJson == null) {
return false;
}
try {
val h5Test: JSONObject? = getTestH5AppJson();
if (h5Test == null) {
return false;
}
/*
val errno: Int = responseJson.optInt("errno");
if (errno != 0) {
android.util.Log.w(TAG, "addH5TestApp: force put Ok to responseJson.")
responseJson.put("errno", 0)
}
*/
var data: JSONArray = appListJson.optJSONArray("data");
if (data == null) {
Log.w(TAG, "addH5TestApp: data is null in responseJson.");
data = JSONArray();
appListJson.put("data", data);
}
data.put(h5Test);
Log.w(TAG, "addH5TestApp: $data")
} catch (e: JSONException) {
Log.e(TAG, "addTestH5App failed: $e", e)
}
return false;
}
}