package com.example.day19_handler_demo1;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.widget.TextView;
public class MainActivity extends Activity {
private TextView tv;
private int count =0;
private Handler handler = new Handler(){
public void handleMessage(android.os.Message msg) {
Log.e("AAA", "=handleMessage=>"+Thread.currentThread().getName());
int num = msg.arg1;
tv.setText(num+"");
};
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv = (TextView) findViewById(R.id.tv);
new Thread(){
public void run() {
while(true){
if(count<100){
count++;
}
Log.e("AAA", "=Thread=>"+Thread.currentThread().getName());
Message msg = handler.obtainMessage();
msg.arg1 = count;
handler.sendMessage(msg);
try {
sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
};
}.start();
}
}
- 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
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
package com.qf.day19_handler_demo2;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
public class MainActivity extends Activity {
private Handler handler = new Handler(){
public void handleMessage(android.os.Message msg) {
switch (msg.what) {
case 1:
int a = msg.arg1;
int a2 = msg.arg2;
String str = (String) msg.obj;
int flag = msg.what;
Log.e("AAA", "==a=="+a);
Log.e("AAA", "==a2=="+a2);
Log.e("AAA", "==str=="+str);
Log.e("AAA", "==flag=="+flag);
break;
case 2:
Log.e("AAAA", "AAAAAAAAAAAAAAAA");
break;
default:
break;
}
};
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void OnMyClick(View v){
new Thread(){
public void run() {
Message msg = Message.obtain();
msg.arg1 = 100;
msg.arg2 = 200;
msg.obj = "我是大数据";
msg.what = 1;
handler.sendMessage(msg);
};
}.start();
}
}
- 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
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
package com.qf.day19_handler_demo4;
import android.app.Activity;
import android.app.ProgressDialog;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
public class MainActivity extends Activity {
private ImageView ivShow;
Handler handler = new Handler();
Handler handler2 = new Handler(){
public void handleMessage(android.os.Message msg) {
switch (msg.what) {
case 1:
pDialog.show();
break;
case 2:
Bitmap bitmap = (Bitmap) msg.obj;
ivShow.setImageBitmap(bitmap);
pDialog.dismiss();
break;
default:
break;
}
};
};
private ProgressDialog pDialog;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ivShow = (ImageView) findViewById(R.id.iv_show);
pDialog = new ProgressDialog(MainActivity.this);
pDialog.setTitle("下载图片");
pDialog.setIcon(R.drawable.ic_launcher);
pDialog.setMessage("正在下载中,请稍后...");
}
public void MyLoadClick(View v) {
new Thread(){
public void run() {
handler2.sendEmptyMessage(1);
byte[] data = HttpUtil.getHttpResult("https://ss2.baidu.com/6ONYsjip0QIZ8tyhnq/it/u=2507878052,3446525205&fm=58");
final Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
Message msg = Message.obtain();
msg.obj = bitmap;
msg.what = 2;
msg.setTarget(handler2);
msg.sendToTarget();
};
}.start();
}
}