publicclassMainActivityextendsAppCompatActivity {
......
@OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//在自己的应用初始Activity中加入如下两行代码
RefWatcher refWatcher = ExampleApplication.getRefWatcher(this);
refWatcher.watch(this);
textView = (TextView) findViewById(R.id.tv);
textView.setOnClickListener(new View.OnClickListener() {
@OverridepublicvoidonClick(View v) {
startAsyncTask();
}
});
}
privatevoidasync() {
startAsyncTask();
}
privatevoidstartAsyncTask() {
// This async task is an anonymous class and therefore has a hidden reference to the outer// class MainActivity. If the activity gets destroyed before the task finishes (e.g. rotation),// the activity instance will leak.new AsyncTask<Void, Void, Void>() {
@Overrideprotected Void doInBackground(Void... params) {
// Do some slow work in background
SystemClock.sleep(20000);
returnnull;
}
}.execute();
}
}