Merge "Deprecate implicit Handler constructors"

am: 560c3ebe1e

Change-Id: I254f85c07743883c7d350a47750dad0745c47d52
This commit is contained in:
Charles Munger
2019-11-19 16:04:15 -08:00
committed by android-build-merger
2 changed files with 28 additions and 8 deletions

View File

@@ -34548,8 +34548,8 @@ package android.os {
} }
public class Handler { public class Handler {
ctor public Handler(); ctor @Deprecated public Handler();
ctor public Handler(@Nullable android.os.Handler.Callback); ctor @Deprecated public Handler(@Nullable android.os.Handler.Callback);
ctor public Handler(@NonNull android.os.Looper); ctor public Handler(@NonNull android.os.Looper);
ctor public Handler(@NonNull android.os.Looper, @Nullable android.os.Handler.Callback); ctor public Handler(@NonNull android.os.Looper, @Nullable android.os.Handler.Callback);
method @NonNull public static android.os.Handler createAsync(@NonNull android.os.Looper); method @NonNull public static android.os.Handler createAsync(@NonNull android.os.Looper);

View File

@@ -28,15 +28,14 @@ import java.lang.reflect.Modifier;
* A Handler allows you to send and process {@link Message} and Runnable * A Handler allows you to send and process {@link Message} and Runnable
* objects associated with a thread's {@link MessageQueue}. Each Handler * objects associated with a thread's {@link MessageQueue}. Each Handler
* instance is associated with a single thread and that thread's message * instance is associated with a single thread and that thread's message
* queue. When you create a new Handler, it is bound to the thread / * queue. When you create a new Handler it is bound to a {@link Looper}.
* message queue of the thread that is creating it -- from that point on, * It will deliver messages and runnables to that Looper's message
* it will deliver messages and runnables to that message queue and execute * queue and execute them on that Looper's thread.
* them as they come out of the message queue. *
*
* <p>There are two main uses for a Handler: (1) to schedule messages and * <p>There are two main uses for a Handler: (1) to schedule messages and
* runnables to be executed at some point in the future; and (2) to enqueue * runnables to be executed at some point in the future; and (2) to enqueue
* an action to be performed on a different thread than your own. * an action to be performed on a different thread than your own.
* *
* <p>Scheduling messages is accomplished with the * <p>Scheduling messages is accomplished with the
* {@link #post}, {@link #postAtTime(Runnable, long)}, * {@link #post}, {@link #postAtTime(Runnable, long)},
* {@link #postDelayed}, {@link #sendEmptyMessage}, * {@link #postDelayed}, {@link #sendEmptyMessage},
@@ -114,7 +113,18 @@ public class Handler {
* *
* If this thread does not have a looper, this handler won't be able to receive messages * If this thread does not have a looper, this handler won't be able to receive messages
* so an exception is thrown. * so an exception is thrown.
*
* @deprecated Implicitly choosing a Looper during Handler construction can lead to bugs
* where operations are silently lost (if the Handler is not expecting new tasks and quits),
* crashes (if a handler is sometimes created on a thread without a Looper active), or race
* conditions, where the thread a handler is associated with is not what the author
* anticipated. Instead, use an {@link java.util.concurrent.Executor} or specify the Looper
* explicitly, using {@link Looper#getMainLooper}, {link android.view.View#getHandler}, or
* similar. If the implicit thread local behavior is required for compatibility, use
* {@code new Handler(Looper.myLooper())} to make it clear to readers.
*
*/ */
@Deprecated
public Handler() { public Handler() {
this(null, false); this(null, false);
} }
@@ -128,7 +138,17 @@ public class Handler {
* so an exception is thrown. * so an exception is thrown.
* *
* @param callback The callback interface in which to handle messages, or null. * @param callback The callback interface in which to handle messages, or null.
*
* @deprecated Implicitly choosing a Looper during Handler construction can lead to bugs
* where operations are silently lost (if the Handler is not expecting new tasks and quits),
* crashes (if a handler is sometimes created on a thread without a Looper active), or race
* conditions, where the thread a handler is associated with is not what the author
* anticipated. Instead, use an {@link java.util.concurrent.Executor} or specify the Looper
* explicitly, using {@link Looper#getMainLooper}, {link android.view.View#getHandler}, or
* similar. If the implicit thread local behavior is required for compatibility, use
* {@code new Handler(Looper.myLooper(), callback)} to make it clear to readers.
*/ */
@Deprecated
public Handler(@Nullable Callback callback) { public Handler(@Nullable Callback callback) {
this(callback, false); this(callback, false);
} }