Skip to content

Instantly share code, notes, and snippets.

@linsea
Created June 17, 2017 07:42
Show Gist options
  • Save linsea/fca2c5bb68a15be776f9ae9b54a30f34 to your computer and use it in GitHub Desktop.
Save linsea/fca2c5bb68a15be776f9ae9b54a30f34 to your computer and use it in GitHub Desktop.
Looper Dump Demo
package com.github.linsea.exceptiontest;
//import android.support.v7.app.AppCompatActivity;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.util.LogPrinter;
import android.view.View;
public class MainActivity extends Activity {
private static final String TAG = "Main";
private MyHandler handler = new MyHandler();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
View dump = findViewById(R.id.dump);
dump.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.d(TAG, "onClick");
handler.dump(new LogPrinter(Log.DEBUG, "DumpTag"),"DumpPrefix ");
}
});
}
@Override
protected void onPostResume() {
super.onPostResume();
new Thread(){
@Override
public void run() {
for (int i = 0; i < 10; i++) {
Log.v(TAG, "Send Message to Target, index=" + i);
handler.obtainMessage(i, 10 * i, 100 * i).sendToTarget();
handler.dump(new LogPrinter(Log.DEBUG, "DumpTag"),"DumpPrefix ");
Log.v(TAG, "PostDelayed RunnableTask, index=" + i);
handler.postDelayed(new RunnableTask(i), i * 1000);
try {
Thread.sleep(1000L);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}.start();
}
static class MyHandler extends Handler{
@Override
public void handleMessage(Message msg) {
Log.i(TAG, "MyHandler handleMessage: " + msg.toString());
}
}
static class RunnableTask implements Runnable{
int id;
public RunnableTask(int id) {
this.id = id;
}
@Override
public void run() {
Log.i(TAG, "run RunTask:" + id);
}
}
}
// 输出case:
//06-17 15:25:55.620 6789-6819/com.github.linsea.exceptiontest V/DumpTag: DumpPrefix Handler (com.github.linsea.exceptiontest.MainActivity$MyHandler) {1bc81c68} @ 69897804
//06-17 15:25:55.620 6789-6819/com.github.linsea.exceptiontest V/DumpTag: DumpPrefix Looper (main, tid 1) {2d4bf881}
//06-17 15:25:55.620 6789-6819/com.github.linsea.exceptiontest V/DumpTag: DumpPrefix Message 0: { when=0 what=3 arg1=30 arg2=300 target=com.github.linsea.exceptiontest.MainActivity$MyHandler }
//06-17 15:25:55.620 6789-6819/com.github.linsea.exceptiontest V/DumpTag: DumpPrefix Message 1: { when=+1s0ms callback=com.github.linsea.exceptiontest.MainActivity$RunnableTask target=com.github.linsea.exceptiontest.MainActivity$MyHandler }
//06-17 15:25:55.620 6789-6819/com.github.linsea.exceptiontest V/DumpTag: DumpPrefix (Total messages: 2, idling=false, quitting=false)
// 输出case:
//06-17 15:25:58.610 6789-6819/com.github.linsea.exceptiontest V/DumpTag: DumpPrefix Handler (com.github.linsea.exceptiontest.MainActivity$MyHandler) {1bc81c68} @ 69900806
//06-17 15:25:58.610 6789-6819/com.github.linsea.exceptiontest V/DumpTag: DumpPrefix Looper (main, tid 1) {2d4bf881}
//06-17 15:25:58.610 6789-6819/com.github.linsea.exceptiontest V/DumpTag: DumpPrefix Message 0: { when=-1ms callback=com.github.linsea.exceptiontest.MainActivity$RunnableTask target=com.github.linsea.exceptiontest.MainActivity$MyHandler }
//06-17 15:25:58.610 6789-6819/com.github.linsea.exceptiontest V/DumpTag: DumpPrefix Message 1: { when=0 what=6 arg1=60 arg2=600 target=com.github.linsea.exceptiontest.MainActivity$MyHandler }
//06-17 15:25:58.610 6789-6819/com.github.linsea.exceptiontest V/DumpTag: DumpPrefix Message 2: { when=+1s999ms callback=com.github.linsea.exceptiontest.MainActivity$RunnableTask target=com.github.linsea.exceptiontest.MainActivity$MyHandler }
//06-17 15:25:58.610 6789-6819/com.github.linsea.exceptiontest V/DumpTag: DumpPrefix Message 3: { when=+4s0ms callback=com.github.linsea.exceptiontest.MainActivity$RunnableTask target=com.github.linsea.exceptiontest.MainActivity$MyHandler }
//06-17 15:25:58.610 6789-6819/com.github.linsea.exceptiontest V/DumpTag: DumpPrefix (Total messages: 4, idling=true, quitting=false)
//Message 1: { when=0 what=6 arg1=60 arg2=600 target=com.github.linsea.exceptiontest.MainActivity$MyHandler }
// when=0 表示这个message应当在当前时间运行,但目前由于调度原因还队列中还没有运行。
// when=-10ms 表示这个message应当在当前时间前10ms运行,但目前由于调度原因还队列中还没有运行。
// when=+10ms 表示这个message应当在当前时间过后10ms运行,目前还队列中等待调度没有运行。
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment