@MainThread注释没有错误

问题描述 投票:3回答:1

我有一个方法:

/**
 * Call event without a value
 */
@MainThread
public void call() {
    setValue(null);
}

我在这里称之为:

new Thread(new Runnable() {
    @Override
    public void run() {
        stopNavigation.call();
    }
}).start();

为什么没有抛出错误因为我从主线程外部进行此调用?我已经阅读了关于@MainThread注释的所有内容(来自android.support.annotation),但没有任何内容告诉我注释实际上做了什么。

android annotations
1个回答
2
投票

据我所知,当我们特定所有线程如enter image description here时,AndroidStudio仅显示有关线程的警告

以下是Activity中用于进行注释工作的示例

public class AActivity extends Activity {

    @MainThread
    protected void onCreate(Bundle savedInstanceState) {
        ...
        getData();
    }

    @AnyThread
    private void getData() {
        new Thread(new Runnable() {
            @WorkerThread // MUST DEFINE THREAD HERE
            @Override
            public void run() {
                updateUI(); // annotation error here
            }
        }).start();
    }

    @MainThread
    void updateUI() {

    }

    class A extends AsyncTask<Void, Void, Void> {
        @WorkerThread // MUST DEFINE THREAD HERE
        @Override
        protected Void doInBackground(Void... voids) {
            updateUI(); // annotation error here
            return null;
        }
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.