ContentProvider之线程测试

2019-12-17  本文已影响0人  钦_79f7

结果

除了 onCreate 运行在主线程,其他5个方法都运行在Binder线程池中。

代码

BookProvider

class BookProvider : ContentProvider() {
    companion object {
        const val TAG = "BookProvider"
    }

    override fun insert(uri: Uri?, values: ContentValues?): Uri {
        TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
    }

    override fun query(uri: Uri?, projection: Array<out String>?, selection: String?, selectionArgs: Array<out String>?, sortOrder: String?): Cursor? {
        Logs.d(TAG, "query, current thread is : ${Thread.currentThread().name}")
        return null
    }

    override fun onCreate(): Boolean {
        Logs.d(TAG, "onCreate, current thread is : ${Thread.currentThread().name}")
        return false
    }

    override fun update(uri: Uri?, values: ContentValues?, selection: String?, selectionArgs: Array<out String>?): Int {
        TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
    }

    override fun delete(uri: Uri?, selection: String?, selectionArgs: Array<out String>?): Int {
        TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
    }

    override fun getType(uri: Uri?): String? {
        Logs.d(TAG, "getType, current thread is : ${Thread.currentThread().name}")
        return null
    }
}

ProviderActivity

class ProviderActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_provider)
        setTitle(R.string.title_content_provider)

        val uri = Uri.parse("content://${BuildConfig.APPLICATION_ID}.book_provider")
        contentResolver.query(uri, null, null, null, null)
        contentResolver.query(uri, null, null, null, null)
        contentResolver.query(uri, null, null, null, null)
        contentResolver.query(uri, null, null, null, null)

    }
}

AndroidManifest

        <!-- 关于权限的声明,还可以分为读权限与写权限,外界应用在访问此provider的时候必须依次声明相应的读写权限,否则外界应用会异常终止 -->
        <provider
            android:name=".demo.provider.BookProvider"
            android:authorities="${applicationId}.book_provider"
            android:permission="com.qxb.PROVIDER"
            android:process=":provider" />
        <activity android:name=".demo.provider.ProviderActivity" />

测试运行线程结果

com.stone.testdemo:provider D/BookProvider: [ main: (BookProvider.kt:28) onCreate ] - onCreate, current thread is : main
com.stone.testdemo:provider D/BookProvider: [ Binder_1: (BookProvider.kt:23) query ] - query, current thread is : Binder_1
com.stone.testdemo:provider D/BookProvider: [ Binder_2: (BookProvider.kt:23) query ] - query, current thread is : Binder_2
com.stone.testdemo:provider D/BookProvider: [ Binder_3: (BookProvider.kt:23) query ] - query, current thread is : Binder_3
com.stone.testdemo:provider D/BookProvider: [ Binder_4: (BookProvider.kt:23) query ] - query, current thread is : Binder_4
上一篇 下一篇

猜你喜欢

热点阅读