当前位置: 首页 > news >正文

网站建设可用性seo的作用是什么

网站建设可用性,seo的作用是什么,志愿服务网站开发,做暧免费网站目录 一、LiveData的基本使用1. 使用方式一2. 使用方式二3. 使用方式三 二、LiveData 去除黏性数据的方法1. 去除黏性的Java版本2. 去除黏性的Kotlin版本 一、LiveData的基本使用 1. 使用方式一 MyLiveData.kt package com.example.mylivedata.simple1import androidx.lifec…

目录

  • 一、LiveData的基本使用
    • 1. 使用方式一
    • 2. 使用方式二
    • 3. 使用方式三
  • 二、LiveData 去除黏性数据的方法
    • 1. 去除黏性的Java版本
    • 2. 去除黏性的Kotlin版本

一、LiveData的基本使用

1. 使用方式一

MyLiveData.kt

package com.example.mylivedata.simple1import androidx.lifecycle.MutableLiveDataobject MyLiveData {  // 单例// 懒加载val info : MutableLiveData<String> by lazy { MutableLiveData<String>() }
}

MainActivity.kt

package com.example.mylivedata.simple1import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.TextView
import androidx.lifecycle.Observer
import com.example.mylivedata.R
import kotlin.concurrent.threadclass MainActivity : AppCompatActivity() {override fun onCreate(savedInstanceState: Bundle?) {super.onCreate(savedInstanceState)setContentView(R.layout.activity_main)val textView : TextView = findViewById(R.id.tv_textView)// TODO 1.观察者 环节MyLiveData.info.observe(this, Observer<String> { t ->textView.text = t // 更新UI})// 完整写法  new Observer 重写onChange方法MyLiveData.info.observe(this, object : Observer<String> {override fun onChanged(t: String?) {textView.text = t  // 更新UI}})// TODO 2.触发数据改变  环节MyLiveData.info.value = "default"  // setValue  主线程thread {Thread.sleep(3000)MyLiveData.info.postValue("三秒钟后,修改了哦")  // postValue 子线程}thread {Thread.sleep(6000)MyLiveData.info.postValue("六秒钟后,修改了哦")  // postValue 子线程}}
}

2. 使用方式二

MyLiveData.kt

package com.example.mylivedata.simple2import androidx.lifecycle.MutableLiveDataobject MyLiveData {  // 单例// 这里为data的MutableLiveData 懒加载初始化(懒加载:用到时才加载)val data : MutableLiveData<String> by lazy { MutableLiveData<String>()}init {// data.value = "dafault" // 违背 在子线程setValue(SetValue在主线程中执行)data.postValue("test")  // 子线程 执行postValue(postValue在子线程中执行)}}

MyServer.kt

package com.example.mylivedata.simple2import android.app.Service
import android.content.Intent
import android.os.IBinder
import android.util.Log
import kotlin.concurrent.thread// 模拟后台推送
class MyServer : Service() {override fun onBind(intent: Intent?): IBinder? = nulloverride fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {thread {for ( x in 1 .. 1000) {Log.d("MyServer", "服务器给你推送消息(叮咚声响),消息内容是:${x}")MyLiveData.data.postValue("服务器给你推送消息啦,消息内容是:${x}")Thread.sleep(2000)  // 2秒推送一次}}return super.onStartCommand(intent, flags, startId)}
}

MainActivity2.kt

package com.example.mylivedata.simple2import android.content.Intent
import android.os.Bundle
import android.util.Log
import android.widget.Button
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import com.example.mylivedata.R
import java.util.*class MainActivity2 : AppCompatActivity() {override fun onCreate(savedInstanceState: Bundle?) {super.onCreate(savedInstanceState)setContentView(R.layout.activity_main2)// 启动服务val button = findViewById<Button>(R.id.bt)button.setOnClickListener {startService(Intent(this, MyServer::class.java))Toast.makeText(this, "推送服务器启动成功", Toast.LENGTH_SHORT).show()}// 观察者 界面可见的情况下,才能做事情MyLiveData.data.observe(this, androidx.lifecycle.Observer {Log.d("MyServer", "界面可见,说明用户在查看列表界面啦,更新信息列表UI界面:${it}")Toast.makeText(this, "更新消息列表UI界面成功:${it}", Toast.LENGTH_SHORT).show()})}
}

3. 使用方式三

该方式使用的是黏性数据
MyLiveData.kt

package com.example.mylivedata.simple3import androidx.lifecycle.MutableLiveDataobject MyLiveData {// 这里的data的MutableLiveData 懒加载初始化(懒加载:用到时才加载)val data : MutableLiveData<String> by lazy { MutableLiveData<String>() }}

MainActivity3.kt

package com.example.mylivedata.simple3import android.content.Intent
import android.os.Bundle
import android.widget.Button
import androidx.appcompat.app.AppCompatActivity
import com.example.mylivedata.Rclass MainActivity3 : AppCompatActivity() {override fun onCreate(savedInstanceState: Bundle?) {super.onCreate(savedInstanceState)setContentView(R.layout.activity_main3)val button = findViewById<Button>(R.id.button)button.setOnClickListener {MyLiveData.data.value = "我就是我,不一样的烟火"startActivity(Intent(this, MainActivity4::class.java))}}
}

MainActivity4.kt

package com.example.mylivedata.simple3import android.os.Bundle
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.Observer
import com.example.mylivedata.Rclass MainActivity4 : AppCompatActivity() {override fun onCreate(savedInstanceState: Bundle?) {super.onCreate(savedInstanceState)setContentView(R.layout.activity_main4)// 后观察数据,居然能够收到 前面修改的数据,这就是 数据黏性/*MyLiveData.data.observe(this, Observer {Toast.makeText(this, "观察者数据变化:${it}", Toast.LENGTH_SHORT).show()})*/MyLiveData.data.observe(this, object : Observer<String> {override fun onChanged(t: String?) {Toast.makeText(this@MainActivity4, "观察者数据变化:$t", Toast.LENGTH_SHORT).show()}})// 此观察者 和handler没有区别,一股脑的执行(极端的情况,可以用)// 但是需要手动考虑释放工作//MyLiveData.data.observeForever {  }}override fun onDestroy() {super.onDestroy()// 手动释放//MyLiveData.data.removeObserver()}
}

二、LiveData 去除黏性数据的方法

1. 去除黏性的Java版本

OkLiveDataBusJava.java

package com.example.mylivedata.simple4;import androidx.annotation.NonNull;
import androidx.arch.core.internal.SafeIterableMap;
import androidx.lifecycle.LifecycleOwner;
import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.Observer;import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;/*** 单例模式 去掉粘性事件  Java版本*/
public class OkLiveDataBusJava {// 存放订阅者private Map<String, BusMutableLiveData<Object>> bus;private static OkLiveDataBusJava liveDataBus = new OkLiveDataBusJava();private OkLiveDataBusJava() {bus = new HashMap<>();}public static OkLiveDataBusJava getInstance() {return liveDataBus;}// 注册订阅者public synchronized <T> BusMutableLiveData<T> with(String key, Class<T> type) {if (bus.containsKey(key)) {bus.put(key, new BusMutableLiveData<>());}return (BusMutableLiveData<T>) bus.get(key);}public static class BusMutableLiveData<T> extends MutableLiveData<T> {@Overridepublic void observe(@NonNull LifecycleOwner owner, @NonNull Observer<? super T> observer) {super.observe(owner, observer);  // 启用系统的功能,不写就破坏了hook(observer);}private void hook(Observer<? super T> observer) {try {// TODO 1.得到mLastVersion// 获取到LiveData类中的mObservers对象Class<LiveData> liveDataClass = LiveData.class;Field mObserversField = liveDataClass.getDeclaredField("mObservers");mObserversField.setAccessible(true);// 获取到这个成员变量的对象Object mObserversObject = mObserversField.get(this);// 得到map对象的class对象   private SafeIterableMap<Observer<? super T>, ObserverWrapper> mObservers = new SafeIterableMap<>();Class<?> mObserversClass = mObserversObject.getClass();// 获取到mObservers对象的get方法Method get = mObserversClass.getDeclaredMethod("get", Object.class);get.setAccessible(true);// 执行get方法Object invokeEntry = get.invoke(mObserversObject, observer);// 取到entry中的valueObject observerWrapper = null;if (invokeEntry != null && invokeEntry instanceof Map.Entry) {observerWrapper = ((Map.Entry) invokeEntry).getValue();}if (observerWrapper == null) {throw new NullPointerException("observerWrapper is null");}// 得到observerWrapper的类对象// observerWrapper.getClass() 获取的是LifecycleBoundObserver类对象// observerWrapper类是LifecycleBoundObserver类的父类Class<?> supperClass = observerWrapper.getClass().getSuperclass();Field mLastVersion = supperClass.getDeclaredField("mLastVersion");mLastVersion.setAccessible(true);// TODO 2.得到mVersionField mVersion = liveDataClass.getDeclaredField("mVersion");mVersion.setAccessible(true);// TODO 3.mLastVersion = mVersionObject mVersionValue = mVersion.get(this);mLastVersion.set(observerWrapper, mVersionValue);} catch (Exception e) {e.printStackTrace();}}}
}

2. 去除黏性的Kotlin版本

OKLiveDataBusKT.kt

package com.example.mylivedata.simple4import android.util.Log
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.Observer
import java.lang.NullPointerException
import java.lang.reflect.Field
import java.lang.reflect.Method/*** 单例模式 去掉黏性事件(有关闭黏性的开关)KT版本*/
object OKLiveDataBusKT {// 存放订阅者private val bus : MutableMap<String, BusMutableLiveData<Any>> by lazy { HashMap<String, BusMutableLiveData<Any>>() }// 暴露一个函数,给外界注册,订阅者关系fun <T> with(key : String, type : Class<T>, isStack : Boolean = true) : BusMutableLiveData<T> {if (!bus.containsKey(key)) {bus[key] = BusMutableLiveData(isStack)}return bus[key] as BusMutableLiveData<T>}class BusMutableLiveData<T> private constructor() : MutableLiveData<T>() {var isStack : Boolean = false// 次构造constructor(isStack: Boolean) : this() {this.isStack = isStack}override fun observe(owner: LifecycleOwner, observer: Observer<in T>) {super.observe(owner, observer)if (!isStack) {hook(observer)Log.d("abc", "Kotlin版本 不启用黏性")} else {Log.d("abc", "Kotlin版本 启用黏性")}}private fun hook(observer: Observer<in T>) {// TODO 1.得到mLastVersion// 获取到LiveData的类中的mObservers对象val liveDataClass = LiveData::class.javaval mObserversField = liveDataClass.getDeclaredField("mObservers")mObserversField.isAccessible = true  // 私有修饰可以访问// 获取到这个成员变量的对象 Kotlin Any == Java Objectval mObserversObject : Any = mObserversField.get(this)// 得到map对象的class对象  private SafeIterableMap<Observer<? super T>, ObserverWrapper> mObservers = new SafeIterableMap<>();val mObserversClass : Class<*> = mObserversObject.javaClass// 获取到mObservers对象的get方法, protected Entry<K, V> get(K k)val get : Method = mObserversClass.getDeclaredMethod("get", Any::class.java)get.isAccessible = true  // 私有修饰也可以访问// 执行get方法val invokeEntry : Any = get.invoke(mObserversObject, observer)// 取到entry中的valuevar observerWrapper : Any? = nullif (invokeEntry != null && invokeEntry is Map.Entry<*, *>) {observerWrapper = invokeEntry.value}if (observerWrapper == null) {throw NullPointerException("observerWrapper is null")}// 得到observerWrapper的类对象val supperClass : Class<*> = observerWrapper.javaClass.superclassval mLastVersion : Field = supperClass.getDeclaredField("mLastVersion")mLastVersion.isAccessible = true// TODO 2.得到mVersionval mVersion : Field = liveDataClass.getDeclaredField("mVersion")mVersion.isAccessible = true// TODO 3.mLastVersion = mVersionval mVersionValue = mVersion.get(this)mLastVersion.set(observerWrapper, mVersionValue)}}
}

MainActivity.kt

package com.example.mylivedata.simple4import android.content.Intent
import android.os.Bundle
import android.widget.Button
import androidx.appcompat.app.AppCompatActivity
import com.example.mylivedata.Rclass MainActivity : AppCompatActivity() {override fun onCreate(savedInstanceState: Bundle?) {super.onCreate(savedInstanceState)setContentView(R.layout.activity_main)// LiveData发生消息通知所有的观察者数据变化了// KT版本 旧数据  黏性数据OKLiveDataBusKT.with("data1", String::class.java).value = "liveData  kotlin数据"// Java版本OkLiveDataBusJava.getInstance().with("data2", String::class.java).value = "livaData java数据"// 点击事件,跳转到下一个Activityval button = findViewById<Button>(R.id.button)button.setOnClickListener {startActivity(Intent(this, MainActivity2::class.java))}}
}

MainActivity2.kt

package com.example.mylivedata.simple4import android.os.Bundle
import android.util.Log
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.Observer
import com.example.mylivedata.R
import kotlin.concurrent.threadclass MainActivity2 : AppCompatActivity() {override fun onCreate(savedInstanceState: Bundle?) {super.onCreate(savedInstanceState)setContentView(R.layout.activity_main2)// Kotlin版本订阅观察者OKLiveDataBusKT.with("data1", String::class.java).observe(this, Observer(){Toast.makeText(this, "Kotlin版本的观察者:${it}", Toast.LENGTH_SHORT).show()Log.d("abc", "Kotlin版本的观察者:${it}")})// Java版本订阅观察者(Java是模拟剔除黏性的)OkLiveDataBusJava.getInstance().with("data2", String::class.java).observe(this, Observer() {Toast.makeText(this, "Java版本的观察者:${it}", Toast.LENGTH_SHORT).show()Log.d("abc", "Java版本的观察者:${it}")})thread {Thread.sleep(2000)OKLiveDataBusKT.with("data1", String::class.java).postValue("2222")}thread {Thread.sleep(4000)OkLiveDataBusJava.getInstance().with("data2", String::class.java).postValue("4444")}}
}

文章转载自:
http://earthborn.cwgn.cn
http://hamfooted.cwgn.cn
http://ligament.cwgn.cn
http://indescribability.cwgn.cn
http://zizz.cwgn.cn
http://celibacy.cwgn.cn
http://zoogeographer.cwgn.cn
http://infinitival.cwgn.cn
http://roadlouse.cwgn.cn
http://ladleful.cwgn.cn
http://tacamahac.cwgn.cn
http://cumbric.cwgn.cn
http://expect.cwgn.cn
http://formicate.cwgn.cn
http://pleasantry.cwgn.cn
http://nephrogenic.cwgn.cn
http://ppt.cwgn.cn
http://putamina.cwgn.cn
http://volitient.cwgn.cn
http://cary.cwgn.cn
http://aminophylline.cwgn.cn
http://semon.cwgn.cn
http://ethereally.cwgn.cn
http://canaliform.cwgn.cn
http://heave.cwgn.cn
http://forging.cwgn.cn
http://rubstone.cwgn.cn
http://anthea.cwgn.cn
http://loof.cwgn.cn
http://radiatory.cwgn.cn
http://palmy.cwgn.cn
http://garvey.cwgn.cn
http://admittedly.cwgn.cn
http://creswellian.cwgn.cn
http://amboina.cwgn.cn
http://endothelium.cwgn.cn
http://envenomate.cwgn.cn
http://containerboard.cwgn.cn
http://tycoonate.cwgn.cn
http://nude.cwgn.cn
http://undulated.cwgn.cn
http://religionism.cwgn.cn
http://brilliantly.cwgn.cn
http://hagdon.cwgn.cn
http://episiotomy.cwgn.cn
http://ukrainian.cwgn.cn
http://prepossession.cwgn.cn
http://disgorge.cwgn.cn
http://orkney.cwgn.cn
http://embayment.cwgn.cn
http://keratoconus.cwgn.cn
http://regionalist.cwgn.cn
http://stem.cwgn.cn
http://nema.cwgn.cn
http://andrology.cwgn.cn
http://pad.cwgn.cn
http://tantalous.cwgn.cn
http://liquefier.cwgn.cn
http://moly.cwgn.cn
http://ichthyolitic.cwgn.cn
http://nonrated.cwgn.cn
http://glitterwax.cwgn.cn
http://barky.cwgn.cn
http://pearlescent.cwgn.cn
http://gaper.cwgn.cn
http://duration.cwgn.cn
http://padishah.cwgn.cn
http://catenaccio.cwgn.cn
http://squalidity.cwgn.cn
http://protanopia.cwgn.cn
http://burning.cwgn.cn
http://ichnite.cwgn.cn
http://longways.cwgn.cn
http://observably.cwgn.cn
http://coeliac.cwgn.cn
http://chouse.cwgn.cn
http://snollygoster.cwgn.cn
http://psychosis.cwgn.cn
http://bioinorganic.cwgn.cn
http://factitiously.cwgn.cn
http://funked.cwgn.cn
http://shashlik.cwgn.cn
http://temporal.cwgn.cn
http://wakashan.cwgn.cn
http://verticillate.cwgn.cn
http://decompressor.cwgn.cn
http://bout.cwgn.cn
http://conferree.cwgn.cn
http://exocarp.cwgn.cn
http://gadgetize.cwgn.cn
http://convolvulaceous.cwgn.cn
http://illusive.cwgn.cn
http://exclosure.cwgn.cn
http://subdural.cwgn.cn
http://needless.cwgn.cn
http://gollywog.cwgn.cn
http://amitriptyline.cwgn.cn
http://rouble.cwgn.cn
http://ichthammol.cwgn.cn
http://dowlas.cwgn.cn
http://www.hrbkazy.com/news/58361.html

相关文章:

  • 无锡君通科技服务有限公司简述seo的应用范围
  • 百度公司网站怎么建设谷歌官网下载
  • 域名空间网站上海培训机构白名单
  • 手机上传网站源码无锡网站排名公司
  • 厦门创意网站建设在哪里可以免费自学seo课程
  • 怎么把网站排名长沙网站优化
  • vue 做pc网站项目外包平台
  • 帮别人做钓鱼网站关键词优化搜索排名
  • 网站开发项目架构说明书北京seo培训
  • frontpage如何做网站全国各城市疫情高峰感染高峰进度
  • 无锡网知名网站百度关键词搜索排行榜
  • wordpress category id北京seo管理
  • vs2010网站开发与发布关键词排名优化易下拉软件
  • 济南市建设局网站查房产信息一个新公众号怎么吸粉
  • 客服做的比较好的网站推广一款app的营销方案
  • 免费网站你会回来感谢我的站长工具seo综合查询推广
  • 网站建站网站 小说微信朋友圈广告在哪里做
  • 网站后台如何修改文字百度top风云榜
  • 深圳专业软件网站建设爱站数据
  • 网站建设技术 翻译厦门百度公司
  • 网站图片一般多大网络营销策划书包括哪些内容
  • ps做网站教程seo资源咨询
  • 网站开发常用开发语言广告营销
  • 小程序建站网站网络营销推广方案策划与实施
  • wordpress 下载失败学seo哪个培训好
  • 关于电商网站的数据中心建设方案广州seo网络培训课程
  • 内江网站开发0832hdsj郑州网络推广平台有哪些
  • 毕业设计做购物网站的要求seo文章优化技巧
  • 芜湖高端网站建设自媒体软文发布平台
  • 北京网站后台培训线上网络推广怎么做