欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android WorkManager使用

程序员文章站 2022-06-23 08:44:32
1.添加依赖api 'androidx.work:work-runtime:2.3.4'或者def work_version = "1.0.0-alpha11" // optional - Firebase JobDispatcher support api "android.arch.work:work-firebase:$work_version" // optional - Test helpers androidTestImplementation "and...

1.添加依赖

api 'androidx.work:work-runtime:2.3.4'

或者

def work_version = "1.0.0-alpha11"

    // optional - Firebase JobDispatcher support
    api "android.arch.work:work-firebase:$work_version"
    // optional - Test helpers
    androidTestImplementation "android.arch.work:work-testing:$work_version"
    api 'android.arch.work:work-runtime:1.0.1'

2.初始化
a. AndroidManifest.xml中:

<provider
            android:name="androidx.work.impl.WorkManagerInitializer"
            android:authorities="com.example.appdemo.workmanager-init"
            android:directBootAware="false"
            android:exported="false"
            android:multiprocess="true"
            tools:node="remove"
            tools:targetApi="n"/>

b. Application中:

 Configuration configuration = new Configuration.Builder()
                .setExecutor(Executors.newFixedThreadPool(8)) // 设置 Workmanager 的后台线程
                .setMinimumLoggingLevel(android.util.Log.INFO)
                .build();
        WorkManager.initialize(context, configuration);

3.常用方法:

WorkManager.getInstance().cancelAllWork();(取消所有Work

WorkManager.getInstance().pruneWork();(删除取消的Work

本文地址:https://blog.csdn.net/qq_32898021/article/details/110285764