MessageQueue方法:postSyncBarrier、r

2022-02-22  本文已影响0人  緦菍亭芷

测试同步屏障

1、ThreadHandler.kt

package com.maotai.testapplication

import android.os.*
import com.yh.base.lib.log.LogUtils

/**
 *  $
 * @date: 2022/2/22 5:13 下午
 * @author: zengbobo
 */
object ThreadHandler {
    private var mHandler: Handler? = null
    fun main() {
        Thread {
            Looper.prepare()
            mHandler = MyHandler(Looper.myLooper()!!)
            Looper.loop()
        }.start()
    }

    class MyHandler(looper: Looper) : Handler(looper) {
        override fun handleMessage(msg: Message) {
            super.handleMessage(msg)
            LogUtils.i("ThreadHandler handleMessage $msg")
        }
    }

    fun sendEmptyMessage() {
        mHandler?.sendEmptyMessage(1)
    }

    fun sendAsyncMessage() {
        mHandler?.obtainMessage(2)?.apply {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP_MR1) {
                isAsynchronous = true
            }
            mHandler?.sendMessage(this)
        }
    }

    private var mTraversalBarrier:Int = 0
    fun postSyncBarrier() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            val queue = mHandler?.looper?.queue
            var postSyncBarrier =
                queue?.javaClass?.getDeclaredMethod("postSyncBarrier", Long::class.java)//
            postSyncBarrier?.isAccessible = true
            mTraversalBarrier =
                (postSyncBarrier?.invoke(queue, SystemClock.uptimeMillis()+10000) as? Int)?:0//
        }
    }

    //removeSyncBarrier方法参数需要使用postSyncBarrier方法的返回值(mTraversalBarrier)
    fun removeSyncBarrier() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            val queue = mHandler?.looper?.queue
            var removeSyncBarrier =
                queue?.javaClass?.getDeclaredMethod("removeSyncBarrier", Int::class.java)
            removeSyncBarrier?.isAccessible = true
            removeSyncBarrier?.invoke(queue,mTraversalBarrier)
        }
    }
}

2、LooperActivity.kt

package com.maotai.testapplication

import android.os.Bundle
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity

class LooperActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        ThreadHandler.main()
        findViewById<TextView>(R.id.tv1)?.apply {
            text ="sendMessage"
            setOnClickListener {
                ThreadHandler.sendEmptyMessage()
            }
        }
        findViewById<TextView>(R.id.tv2)?.apply {
            text ="sendAsyncMessage"
            setOnClickListener {
                ThreadHandler.sendAsyncMessage()
            }
        }
        findViewById<TextView>(R.id.tv3)?.apply {
            text ="postSyncBarrier"
            setOnClickListener {
                ThreadHandler.postSyncBarrier()
            }
        }

        findViewById<TextView>(R.id.tv4)?.apply {
            text ="removeSyncBarrier"
            setOnClickListener {
                ThreadHandler.removeSyncBarrier()
            }
        }
    }
}

3、activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/tv1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:padding="20dp"
        android:text=" FirstActivity"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/tv2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:padding="20dp"
        android:text=" LayoutActivity"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/tv3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:padding="20dp"
        android:text=" NormalActivity"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/tv4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:padding="20dp"
        android:text=" LooperActivity"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</LinearLayout>
上一篇下一篇

猜你喜欢

热点阅读