Is it possibl开发者_Go百科e to annotate method to run code in UiThread?
runOnUiThread(new Runnable() {
public void run() {
//my code
}});
looks too complex to use it often.
Look at the AndroidAnnotations project. They use a annotations. As far as i know it is what you want. It uses compile time code generation http://code.google.com/p/androidannotations/
Note:
According to the documentation the @UiThread
annotation only denotes that the given class/method should run on the UI thread. It does NOT mean using this annotation will call your method on the UI thread.
Edit: this answer is not relevant any more. You can annotate with @UiThread
.
According to Google IO 2015, @UiThread
is the annotation you are looking for. There will be actually four new:
http://robovm.com/google-io-summary-whats-new-in-android-development-tools/ (under New Support Annotations).
Sadly, they are not live yet. When they will be live, they will pop up in here: https://developer.android.com/reference/android/support/annotation/package-summary.html
@UiThreadTest seems to work now. I'm sure it will be deprecated for the real thing soon, so caveat emptor.
精彩评论