Skip to content

Commit 73aefe6

Browse files
authored
[shared_preferences] Removed deprecated AsyncTask API (flutter#3481)
1 parent 7413abf commit 73aefe6

File tree

4 files changed

+29
-13
lines changed

4 files changed

+29
-13
lines changed

packages/shared_preferences/shared_preferences/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 2.0.1
2+
3+
* Removed deprecated [AsyncTask](https://developer.android.com/reference/android/os/AsyncTask) was deprecated in API level 30 ([#3481](https://github.com/flutter/plugins/pull/3481))
4+
15
## 2.0.0
26

37
* Migrate to null-safety.

packages/shared_preferences/shared_preferences/android/gradle/wrapper/gradle-wrapper.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@ distributionBase=GRADLE_USER_HOME
22
distributionPath=wrapper/dists
33
zipStoreBase=GRADLE_USER_HOME
44
zipStorePath=wrapper/dists
5-
distributionUrl=https\://services.gradle.org/distributions/gradle-5.1.1-all.zip
5+
distributionUrl=https\://services.gradle.org/distributions/gradle-6.7-all.zip

packages/shared_preferences/shared_preferences/android/src/main/java/io/flutter/plugins/sharedpreferences/MethodCallHandlerImpl.java

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66

77
import android.content.Context;
88
import android.content.SharedPreferences;
9-
import android.os.AsyncTask;
9+
import android.os.Handler;
10+
import android.os.Looper;
1011
import android.util.Base64;
1112
import io.flutter.plugin.common.MethodCall;
1213
import io.flutter.plugin.common.MethodChannel;
@@ -21,6 +22,10 @@
2122
import java.util.List;
2223
import java.util.Map;
2324
import java.util.Set;
25+
import java.util.concurrent.ExecutorService;
26+
import java.util.concurrent.SynchronousQueue;
27+
import java.util.concurrent.ThreadPoolExecutor;
28+
import java.util.concurrent.TimeUnit;
2429

2530
/**
2631
* Implementation of the {@link MethodChannel.MethodCallHandler} for the plugin. It is also
@@ -118,17 +123,24 @@ public void onMethodCall(MethodCall call, MethodChannel.Result result) {
118123

119124
private void commitAsync(
120125
final SharedPreferences.Editor editor, final MethodChannel.Result result) {
121-
new AsyncTask<Void, Void, Boolean>() {
122-
@Override
123-
protected Boolean doInBackground(Void... voids) {
124-
return editor.commit();
125-
}
126+
final ExecutorService executor =
127+
new ThreadPoolExecutor(0, 1, 30L, TimeUnit.SECONDS, new SynchronousQueue<Runnable>());
128+
final Handler handler = new Handler(Looper.getMainLooper());
126129

127-
@Override
128-
protected void onPostExecute(Boolean value) {
129-
result.success(value);
130-
}
131-
}.execute();
130+
executor.execute(
131+
new Runnable() {
132+
@Override
133+
public void run() {
134+
final boolean response = editor.commit();
135+
handler.post(
136+
new Runnable() {
137+
@Override
138+
public void run() {
139+
result.success(response);
140+
}
141+
});
142+
}
143+
});
132144
}
133145

134146
private List<String> decodeList(String encodedList) throws IOException {

packages/shared_preferences/shared_preferences/pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ name: shared_preferences
22
description: Flutter plugin for reading and writing simple key-value pairs.
33
Wraps NSUserDefaults on iOS and SharedPreferences on Android.
44
homepage: https://github.com/flutter/plugins/tree/master/packages/shared_preferences/shared_preferences
5-
version: 2.0.0
5+
version: 2.0.1
66

77
flutter:
88
plugin:

0 commit comments

Comments
 (0)