[toc]
/* loaded from: classes.dex */
public class MainActivity extends Activity {
@Override // android.app.Activity
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TelephonyManager telephonyManager = (TelephonyManager) getApplicationContext().getSystemService("phone");
MyAsyncTask async = new MyAsyncTask(this, null);
async.execute(telephonyManager.getDeviceId());
// create a new class, explicit call this method. Just find instance who call `execute` is `AsyncTask`
}
/* loaded from: classes.dex */
private class MyAsyncTask extends AsyncTask<String, String, String> {
private MyAsyncTask() {
}
/* synthetic */ MyAsyncTask(MainActivity mainActivity, MyAsyncTask myAsyncTask) {
this();
}
/* JADX DEBUG: Method merged with bridge method */
/* JADX INFO: Access modifiers changed from: protected */
@Override // android.os.AsyncTask
public String doInBackground(String... params) {
Log.d("DroidBench", params[0]);
return "Done";
}
}
@Override // android.app.Activity
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
- async.execute(telephonyManager.getDeviceId()); // line 9
+ new dummyAsyncTask<MyAsyncTask>().init();
+ dummyAsyncTask.doInBackground(telephonyManager.getDeviceId());
- superclass is
Thread
- Just call
Thread
// first condition
public class MainActivity extends Activity {
@Override // android.app.Activity
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TelephonyManager telephonyManager = (TelephonyManager) getApplicationContext().getSystemService("phone");
new MyThread(telephonyManager.getDeviceId()).start();
// change start() -> run()
// find instance who called `start`.
}
/* loaded from: classes.dex */
private class MyThread extends Thread {
private final String deviceId;
public MyThread(String deviceId) {
this.deviceId = deviceId;
}
@Override // java.lang.Thread, java.lang.Runnable
public void run() {
Log.d("DroidBench", this.deviceId);
}
}
@Override // android.app.Activity
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
// second condition
public class MainActivity extends Activity {
@Override // android.app.Activity
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TelephonyManager telephonyManager = (TelephonyManager) getApplicationContext().getSystemService("phone");
final String deviceId = telephonyManager.getDeviceId();
new Thread(new Runnable() { // from class: de.ecspride.MainActivity.1
@Override // java.lang.Runnable
public void run() {
Log.d("DroidBench", deviceId);
}
}).start();
}
@Override // android.app.Activity
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
- a.start();
+ a.run(); // Runable / subClass of Thread
+ a.call(); // Callable
public class MainActivity extends Activity {
//定义切换的图片的数组id
int imgids[] = new int[]{
R.drawable.s_1, R.drawable.s_2,R.drawable.s_3,
R.drawable.s_4,R.drawable.s_5,R.drawable.s_6,
R.drawable.s_7,R.drawable.s_8
};
int imgstart = 0;
final Handler myHandler = new Handler()
{
@Override
//重写handleMessage方法,根据msg中what的值判断是否执行后续操作
public void handleMessage(Message msg) {
if(msg.what == 0x123)
{
imgchange.setImageResource(imgids[imgstart++ % 8]);
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final ImageView imgchange = (ImageView) findViewById(R.id.imgchange);
//使用定时器,每隔200毫秒让handler发送一个空信息
new Timer().schedule(new TimerTask() {
@Override
public void run() {
myHandler.sendEmptyMessage(0x123);
// Need find all send Message api !!!!
// the find instance who called these api and this instance must be `Handler`
}
}, 0,200);
}
}
myHandler.*Message(0x123);
+ myHandler.handleMessage(0x123);
// api
dispatchMessage
sendEmptyMessage
sendEmptyMessageAtTime
sendEmptyMessageDelayed
sendMessage
sendMessageAtFrontOfQueue
sendMessageAtTime
sendMessageDelayed
protected void onCreate(Bundle savedInstanceState) {
.......
mHandler = new Handler();
new Thread(new Runnable() {
@Override
public void run() {
try {
Thread.sleep(1000);//在子线程有一段耗时操作,比如请求网络
mHandler.post(new Runnable() {
// find instance who called `post`, and arg is `Runnable`
@Override
public void run() {
mTestTV.setText("This is post");//更新UI
}
});
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}).start();
}
- mHandler.post(RunnableInstance)
+ RunnableInstance.run();
// Runnable
post
postAtFrontOfQueue
postAtTime
postDelayed
/**
* 在这个栗子中将展示使用rxjava执行一个最简单的异步任务
* 栗子将会完成如下工作
* 1. 在任务执行前显示进度条
* 2. 线程休眠1秒,模拟异步任务的执行,然后返回一段字符串作为执行结果
* 3. 关闭进度条,处理异步任务的执行结果
*/
private void executeAsyncTask() {
//Observable#create方法创建一个异步任务
Observable.create(new Observable.OnSubscribe<String>() {
@Override
public void call(Subscriber<? super String> subscriber) {
//在call方法中执行异步任务
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
//注意:如果异步任务中需要抛出异常,在执行结果中处理异常。需要将异常转化未RuntimException
throw new RuntimeException(e);
}
count++;
//调用subscriber#onNext方法将执行结果返回
subscriber.onNext("成功执行异步任务" + count + "次");
//调用subscriber#onCompleted方法完成异步任务
subscriber.onCompleted();
}
})
.subscribeOn(Schedulers.io())//指定异步任务在IO线程运行
.observeOn(AndroidSchedulers.mainThread())//制定执行结果在主线程运行
.subscribe(new Subscriber<String>() {
ProgressDialog progressDialog;
@Override
public void onStart() {
//在异步任务执行前打开进度条
super.onStart();
Log.i(TAG, "onStart");
if (progressDialog == null)
progressDialog = ProgressDialog.show(MainActivity.this, "", "正在执行异步任务");
}
@Override
public void onCompleted() {
//在异步任务执行完成后关闭进度条
Log.i(TAG, "onCompleted");
if (progressDialog != null) {
progressDialog.dismiss();
progressDialog = null;
}
}
@Override
public void onError(Throwable e) {
//如果异步任务执行失败则关闭进度条,并打印错误日志
if (progressDialog != null) {
progressDialog.dismiss();
progressDialog = null;
}
Log.e(TAG, "onError: execute async task fail", e);
}
@Override
public void onNext(String s) {
//处理异步任务的执行结果
Log.i(TAG, "onNext");
tvContent.setText(s);
}
});
}
+ subscribeSubscriberInstance.onStart();
+ callSubscriberInstance.call();
Executor
: super class forScheduledExecutorService
andExecutorService
// api
execute
ExecutorService
// api, `submit` can get a `Callable`/`Runnable` instance, `execute` can get a `Runnable` instance.
submit
execute
ScheduledExecutorService
// api, `submit` and `schedule` can get a `Callable`/`Runnable` instance, `execute` can get a `Runnable` instance.
submit
schedule
execute
scheduleWithFixedDelay
scheduleAtFixedRate
- api(a);
+ a.call(); // Callable;
+ a.run(); // Runable;
import static java.util.concurrent.TimeUnit.*;
class BeeperControl {
private final ScheduledExecutorService scheduler =
Executors.newScheduledThreadPool(1);
public void beepForAnHour() {
final Runnable beeper = new Runnable() {
public void run() { System.out.println("beep"); }
};
final ScheduledFuture<?> beeperHandle =
scheduler.scheduleAtFixedRate(beeper, 10, 10, SECONDS);
scheduler.schedule(new Runnable() {
// Just find this api, then, find the arg instance type, then add the new statement.
public void run() { beeperHandle.cancel(true); }
}, 60 * 60, SECONDS);
}
}
Need to model three situations:
AsyncTask
Runable
/Callable
RxJava