1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
package safeluck.drive.evaluation.viewmodels;
 
 
import android.util.Log;
 
import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;
 
import java.util.concurrent.atomic.AtomicInteger;
 
import com.anyun.exam.lib.MyLog;
 
public class MainViewModel extends ViewModel {
    private static final String TAG = "MainViewModel";
    AtomicInteger atomicInteger = new AtomicInteger(0);
    private MutableLiveData<String> json = new MutableLiveData<>();
    public LiveData<String> getJson(){
        //往json字符串写入数据
        loadJson();
        return json;
    }
 
    private void  loadJson() {
        MyLog.i("loadJson");
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Thread.sleep(10*1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
 
                Log.i(TAG, "run: "+   atomicInteger.incrementAndGet());
                json.postValue("我爱你JetPack!!"+atomicInteger.get());
            }
        }).start();
    }
 
}