public class MainActivity extends AppCompatActivity {
private Handler handler;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
nThread n = new nThread();
n.start();
Button bt = (Button) findViewById(R.id.Bub);
bt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Message msg = handler.obtainMessage();
msg.obj = "SandMessage";
handler.sendMessage(msg);
}
});
}
class nThread extends Thread {
@Override
public void run() {
Looper.prepare();
handler = new Handler() {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
System.out.println(msg.obj);
System.out.println(Thread.currentThread().getName());
}
};
Looper.loop();
}
}
}
0 件のコメント:
コメントを投稿