ListenableWorker 中的執行緒

在某些情況下,您可能需要提供自訂執行緒策略。適用對象 例如,您可能需要處理以回呼為基礎的非同步作業。 WorkManager 支援以下用途: ListenableWorkerListenableWorker 是最基本的工作站 API; Worker, CoroutineWorkerRxWorker 都來自這個類別。A 罩杯 只有在工作開始和停止及離開時,ListenableWorker 才會發出信號 要完全由您決定系統會在主要主機上叫用啟動工作信號 所以一定要前往

抽象方法 ListenableWorker.startWork()敬上 會傳回ListenableFuture Result。A 罩杯 ListenableFuture 是輕量的介面,也就是提供以下服務的 Future 附加事件監聽器及傳播例外狀況的功能在 startWork 方法,應傳回 ListenableFuture, 會在作業完成後,根據作業的 Result 進行設定。您可以建立 ListenableFuture 執行個體,以下列兩種方式之一:

  1. 如果您使用的是 Guava,請使用 ListeningExecutorService
  2. 否則,請加入 councurrent-futures敬上 ,然後使用 CallbackToFutureAdapter

如要根據非同步回呼執行某些工作,您必須 就像這樣:

Kotlin

class CallbackWorker(
        context: Context,
        params: WorkerParameters
) : ListenableWorker(context, params) {
    override fun startWork(): ListenableFuture<Result> {
        return CallbackToFutureAdapter.getFuture { completer ->
            val callback = object : Callback {
                var successes = 0

                override fun onFailure(call: Call, e: IOException) {
                    completer.setException(e)
                }

                override fun onResponse(call: Call, response: Response) {
                    successes++
                    if (successes == 100) {
                        completer.set(Result.success())
                    }
                }
            }

            repeat(100) {
                downloadAsynchronously("https://example.com", callback)
            }

            callback
        }
    }
}

Java

public class CallbackWorker extends ListenableWorker {

    public CallbackWorker(Context context, WorkerParameters params) {
        super(context, params);
    }

    @NonNull
    @Override
    public ListenableFuture<Result> startWork() {
        return CallbackToFutureAdapter.getFuture(completer -> {
            Callback callback = new Callback() {
                int successes = 0;

                @Override
                public void onFailure(Call call, IOException e) {
                    completer.setException(e);
                }

                @Override
                public void onResponse(Call call, Response response) {
                    successes++;
                    if (successes == 100) {
                        completer.set(Result.success());
                    }
                }
            };

            for (int i = 0; i < 100; i++) {
                downloadAsynchronously("https://www.example.com", callback);
            }
            return callback;
        });
    }
}

如果您的工作是 停止? 在工作期間,ListenableWorkerListenableFuture 一律會取消 預期會停止使用 CallbackToFutureAdapter,您只需新增 取消接聽器,如下所示:

Kotlin

class CallbackWorker(
        context: Context,
        params: WorkerParameters
) : ListenableWorker(context, params) {
    override fun startWork(): ListenableFuture<Result> {
        return CallbackToFutureAdapter.getFuture { completer ->
            val callback = object : Callback {
                var successes = 0

                override fun onFailure(call: Call, e: IOException) {
                    completer.setException(e)
                }

                override fun onResponse(call: Call, response: Response) {
                    ++successes
                    if (successes == 100) {
                        completer.set(Result.success())
                    }
                }
            }

 completer.addCancellationListener(cancelDownloadsRunnable, executor)

            repeat(100) {
                downloadAsynchronously("https://example.com", callback)
            }

            callback
        }
    }
}

Java

public class CallbackWorker extends ListenableWorker {

    public CallbackWorker(Context context, WorkerParameters params) {
        super(context, params);
    }

    @NonNull
    @Override
    public ListenableFuture<Result> startWork() {
        return CallbackToFutureAdapter.getFuture(completer -> {
            Callback callback = new Callback() {
                int successes = 0;

                @Override
                public void onFailure(Call call, IOException e) {
                    completer.setException(e);
                }

                @Override
                public void onResponse(Call call, Response response) {
                    ++successes;
                    if (successes == 100) {
                        completer.set(Result.success());
                    }
                }
            };

            completer.addCancellationListener(cancelDownloadsRunnable, executor);

            for (int i = 0; i < 100; ++i) {
                downloadAsynchronously("https://www.example.com", callback);
            }
            return callback;
        });
    }
}

以其他程序執行 ReadableWorker

您也可以使用 ListenableWorker 導入 RemoteListenableWorker,將工作站繫結至特定程序。

RemoteListenableWorker 會繫結至特定程序,其中包含兩個額外的引數,並在建立作業要求時做為輸入資料的一部分:ARGUMENT_CLASS_NAMEARGUMENT_PACKAGE_NAME

下列範例說明建立與特定程序繫結的作業要求:

Kotlin

val PACKAGE_NAME = "com.example.background.multiprocess"

val serviceName = RemoteWorkerService::class.java.name
val componentName = ComponentName(PACKAGE_NAME, serviceName)

val data: Data = Data.Builder()
   .putString(ARGUMENT_PACKAGE_NAME, componentName.packageName)
   .putString(ARGUMENT_CLASS_NAME, componentName.className)
   .build()

return OneTimeWorkRequest.Builder(ExampleRemoteListenableWorker::class.java)
   .setInputData(data)
   .build()

Java

String PACKAGE_NAME = "com.example.background.multiprocess";

String serviceName = RemoteWorkerService.class.getName();
ComponentName componentName = new ComponentName(PACKAGE_NAME, serviceName);

Data data = new Data.Builder()
        .putString(ARGUMENT_PACKAGE_NAME, componentName.getPackageName())
        .putString(ARGUMENT_CLASS_NAME, componentName.getClassName())
        .build();

return new OneTimeWorkRequest.Builder(ExampleRemoteListenableWorker.class)
        .setInputData(data)
        .build();

針對每個 RemoteWorkerService,您必須在 AndroidManifest.xml 檔案中新增服務定義:

<manifest ... >
    <service
            android:name="androidx.work.multiprocess.RemoteWorkerService"
            android:exported="false"
            android:process=":worker1" />

        <service
            android:name=".RemoteWorkerService2"
            android:exported="false"
            android:process=":worker2" />
    ...
</manifest>

範例