AsyncTask API 在 Android 11 中已弃用。有哪些替代方案?
2022-08-31 06:55:10
Google正在Android 11中弃用Android AsyncTask API,并建议改用。你可以在这里查看提交java.util.concurrent
*
* @deprecated Use the standard <code>java.util.concurrent</code> or
* <a href="https://developer.android.com/topic/libraries/architecture/coroutines">
* Kotlin concurrency utilities</a> instead.
*/
@Deprecated
public abstract class AsyncTask<Params, Progress, Result> {
如果您在Android中维护具有异步任务的旧代码库,则将来可能不得不对其进行更改。我的问题是,应该使用正确替换下面显示的代码片段。它是活动的静态内部类。我正在寻找一些可以工作的东西java.util.concurrent
minSdkVersion 16
private static class LongRunningTask extends AsyncTask<String, Void, MyPojo> {
private static final String TAG = MyActivity.LongRunningTask.class.getSimpleName();
private WeakReference<MyActivity> activityReference;
LongRunningTask(MyActivity context) {
activityReference = new WeakReference<>(context);
}
@Override
protected MyPojo doInBackground(String... params) {
// Some long running task
}
@Override
protected void onPostExecute(MyPojo data) {
MyActivity activity = activityReference.get();
activity.progressBar.setVisibility(View.GONE);
populateData(activity, data) ;
}
}