Androidノート——Handler更新UI例


public class MainActivity extends ActionBarActivity {
	private TextView textView;
	private int i=0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        textView=(TextView) findViewById(R.id.textView1);
        final Handler handler=new Handler(){
        	public void handleMessage(android.os.Message msg) {
        		Bundle bundle=msg.getData();
        		String string=bundle.getString("name");
        		textView.setText(string);
        	};
        };
        Timer timer=new Timer();
        timer.schedule(new TimerTask() {
			
			@Override
			public void run() {
				// TODO Auto-generated method stub
				Message message=new Message();
				Bundle bundle =new Bundle();
				bundle.putString("name", "Hello"+i);
				i++;
				message.setData(bundle);
				handler.sendMessage(message);
			}
		}, 1000,1000);
        
        
    }
}