package com.example.raj.testview; import android.os.Handler; import android.os.Message; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.util.Log; import android.view.Menu; import android.view.MenuItem; import android.widget.TextView; import android.widget.ThemedSpinnerAdapter; import java.util.Random; import java.util.TimerTask; public class MainActivity extends AppCompatActivity { TextView tv; Handler handler; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); tv=(TextView)findViewById(R.id.tv); Thread th=new Thread(new MyThread()); th.start(); handler=new Handler(){ @Override public void handleMessage(Message msg) { //super.handleMessage(msg); int k=msg.arg1; tv.setText(Integer.toString(k)); } }; } class MyThread implements Runnable{ @Override public void run() { for(int i=0;i<100;i++) { Message message=Message.obtain(); Random r=new Random(); int y=r.nextInt(); message.arg1=y; handler.sendMessage(message); try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } } } } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.menu_main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); //noinspection SimplifiableIfStatement if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } }
Wednesday, 30 September 2015
print a random number every second
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment