package power.remote.control; //Example SeekBar - ProgressBar - Timer import java.util.Set; import java.util.Timer; import java.util.TimerTask; import android.os.Bundle; import android.os.Handler; import android.app.Activity; import android.content.Intent; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.ProgressBar; import android.widget.SeekBar; import android.widget.SeekBar.OnSeekBarChangeListener; import android.widget.Toast; public class RemoteControl extends Activity { private SeekBar bar; TimerTask mTimerTask; final Handler handler = new Handler(); Timer timer = new Timer(); ProgressBar progressbar1; public int Timemin=0; public int Timerem=1; public int nCounter; public long mStartTime; public int progress; public boolean fromUser; Button button1; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_remote_control); progressbar1 = (ProgressBar) findViewById(R.id.progressBar1); button1 = (Button)this.findViewById(R.id.button1); button1.setOnClickListener(new OnClickListener() { public void onClick(View v) { //Do something reset(); }); BarControls();//Call SeekBar function } private void BarControls(){ bar = (SeekBar)findViewById(R.id.seekBar1); // make seekbar object try{ bar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() { public void onStopTrackingTouch(SeekBar seekBar) { // TODO Auto-generated method stub seekBar.setSecondaryProgress(seekBar.getProgress()); doTimerTask(); Intent intent=new Intent( getApplicationContext(), MyScheduleReceiver.class); intent.putExtra("AlarmTime", Timemin); sendBroadcast(intent);//Call alarm manager // Toast.makeText(getApplicationContext(), "Timer start!!!" ,Toast.LENGTH_LONG).show(); } public void onStartTrackingTouch(SeekBar seekBar) { // TODO Auto-generated method stub sendBroadcast(new Intent(getApplicationContext(), MyStopReceiver.class)); stopTask(); } public void onProgressChanged(SeekBar seekBar, int progress,boolean fromUser) { // TODO Auto-generated method stub Timemin=progress; //t1.setTextSize(progress); Toast.makeText(getApplicationContext(), "TV OFF: "+String.valueOf(progress)+" minutes",Toast.LENGTH_LONG).show(); } }); } catch (Exception e) { e.printStackTrace(); } } public void doTimerTask(){ mTimerTask = new TimerTask() { public void run() { handler.post(new Runnable() { public void run() { if(Timerem > 0){ Timerem = Timemin - nCounter; //textTimeRemain.setText("Time remaining is : "+ Timerem +" minutes."); nCounter++; progressbar1.setProgress(Timerem); } } }); }}; // public void schedule (TimerTask task, long delay, long period) timer.schedule(mTimerTask, 0, 1000*60); // } public void stopTask(){ if(mTimerTask!=null){ mTimerTask.cancel(); Timerem = 1; nCounter=0; } } protected void onStart() { super.onStart(); } @Override public void onResume() { super.onResume(); } public void reset(){ sendBroadcast(new Intent(this,MyStopReceiver.class)); stopTask(); Toast.makeText(this, "Timer reset!!!" ,Toast.LENGTH_LONG).show(); } protected void onStop() { super.onStop(); sendBroadcast(new Intent(this,MyStopReceiver.class)); finish(); } @Override protected void onDestroy() { // TODO Auto-generated method stub super.onDestroy(); } }