Advertisement
Guest User

Untitled

a guest
Aug 21st, 2015
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.84 KB | None | 0 0
  1. package timer;
  2.  
  3. import android.support.v7.app.AppCompatActivity;
  4. import android.os.Bundle;
  5. import android.view.Menu;
  6. import android.view.MenuItem;
  7. import android.widget.TextView;
  8.  
  9. import java.util.Timer;
  10. import java.util.TimerTask;
  11.  
  12. public class MainActivity extends AppCompatActivity {
  13.  
  14.     int seconds=0;
  15.     TextView textView;
  16.  
  17.     Timer timer = new Timer();
  18.  
  19.     @Override
  20.     protected void onCreate(Bundle savedInstanceState) {
  21.         super.onCreate(savedInstanceState);
  22.         setContentView(R.layout.activity_main);
  23.  
  24.         MainActivity mojTimer = new MainActivity();
  25.         textView = (TextView)findViewById(R.id.txtView);
  26.         mojTimer.start();
  27.     }
  28.  
  29.     @Override
  30.     public boolean onCreateOptionsMenu(Menu menu) {
  31.         // Inflate the menu; this adds items to the action bar if it is present.
  32.         getMenuInflater().inflate(R.menu.menu_main, menu);
  33.         return true;
  34.     }
  35.  
  36.     @Override
  37.     public boolean onOptionsItemSelected(MenuItem item) {
  38.         // Handle action bar item clicks here. The action bar will
  39.         // automatically handle clicks on the Home/Up button, so long
  40.         // as you specify a parent activity in AndroidManifest.xml.
  41.         int id = item.getItemId();
  42.  
  43.         //noinspection SimplifiableIfStatement
  44.         if (id == R.id.action_settings) {
  45.             return true;
  46.         }
  47.  
  48.         return super.onOptionsItemSelected(item);
  49.     }
  50.  
  51.  
  52.     TimerTask timerTask = new TimerTask() {
  53.         @Override
  54.         public void run() {
  55.  
  56.             System.out.println(seconds++);
  57.  
  58.             try {
  59.  
  60.                 textView = (TextView)findViewById(R.id.txtView);
  61.  
  62.             } catch (Exception ex){
  63.  
  64.                 System.out.println(ex);
  65.             }
  66.         }
  67.     };
  68.  
  69.     public void start(){
  70.  
  71.         timer.schedule(timerTask, 1000, 1000);
  72.  
  73.     }
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement