Friday 18 May 2012

Put Timer in android


import java.util.Timer;
import java.util.TimerTask;
import android.os.Handler.Callback;
import android.app.Activity;
import android.content.Intent;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.TextView;

public class Timerscreen extends Activity{

    Button   btn_end;

    TextView  tv_callDuration;
    String nm,num;
   
   
     long starttime = 0;
        //this  posts a message to the main thread from our timertask
        //and updates the textfield
       final Handler h = new Handler(new Callback() {
            public boolean handleMessage(Message msg) {
               long millis = System.currentTimeMillis() - starttime;
               int seconds = (int) (millis / 1000);
               int minutes = seconds / 60;
               seconds     = seconds % 60;

               tv_callDuration.setText(String.format("%d:%02d", minutes, seconds));
                return false;
            }
        });
       //runs without timer be reposting self
       Handler h2 = new Handler();
       Runnable run = new Runnable() {

            public void run() {
               long millis = System.currentTimeMillis() - starttime;
               int seconds = (int) (millis / 1000);
               int minutes = seconds / 60;
               seconds     = seconds % 60;

               tv_callDuration.setText(String.format("%d:%02d", minutes, seconds));

               h2.postDelayed(this, 500);
            }
        };

       //tells handler to send a message
       class firstTask extends TimerTask {

            @Override
            public void run() {
                h.sendEmptyMessage(0);
            }
       };

       //tells activity to run on ui thread
       class secondTask extends TimerTask {

            @Override
            public void run() {
                CallSecondScreen.this.runOnUiThread(new Runnable() {

                    public void run() {
                       long millis = System.currentTimeMillis() - starttime;
                       int seconds = (int) (millis / 1000);
                       int minutes = seconds / 60;
                       seconds     = seconds % 60;

                       tv_callDuration.setText(String.format("%d:%02d", minutes, seconds));
                    }
                });
            }
       };


       Timer timer = new Timer();
   
   
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.second_calling_screen);
       
        btn_end = (Button)findViewById(R.id.btn2_endCall);
        tv_callDuration = (TextView)findViewById(R.id.tv_callDuration);
       
       
        starttime = System.currentTimeMillis();
        timer = new Timer();
        timer.schedule(new firstTask(), 0,500);
        timer.schedule(new secondTask(),  0,500);
        h2.postDelayed(run, 0);
   
       
        btn_end.setOnClickListener(new OnClickListener() {
           
            public void onClick(View v) {
                // TODO Auto-generated method stub
               
            finish();
               
               
            }
        });
    }
}

No comments: