Android Count Down Timer Examples?

Here another nice topic am cover Countdown Timer , Here Is the Android Official Documentation is http://developer.android.com/reference/android/os/CountDownTimer.html ,

In this topic am fount two types of  count down timers.

FIRST ONE:


  1.   String oldFormat = "yyyy-MM-dd HH:mm:ss";

       SimpleDateFormat sdf1 = new SimpleDateFormat(oldFormat);
       TimeZone obj = TimeZone.getTimeZone("CST");
           sdf1.setTimeZone(obj);
         
           Date date = sdf1.parse(" your match time ");
           millis = date.getTime();


          long currentTimeInMili = new Date().getTime(); // Current time
          final CountDownTimer Counter1 = new CountDownTimer(millis - currentTimeInMili, 1 * 1000) {
             public void onTick(long millisUntilFinished) {
                  matchUpcomingText.setText("" + formatTime(millisUntilFinished) + "till match start");
        }

        public void onFinish() {
       matchUpcomingText.setText("Finished!");
           }
         }.start();

SECOND ONE:


  1. String oldFormat = "yyyy-MM-dd HH:mm:ss";

       SimpleDateFormat sdf1 = new SimpleDateFormat(oldFormat);
       TimeZone obj = TimeZone.getTimeZone("CST");
           sdf1.setTimeZone(obj);
         
           Date date = sdf1.parse(" your match time ");
           millis = date.getTime();

    long currentTimeInMili = new Date().getTime();
    MyCount counter = new MyCount(millis - currentTimeInMili, 1 * 1000);
    counter.start();

        public static class MyCount extends CountDownTimer {
    public MyCount(long millisInFuture, long countDownInterval) {
    super(millisInFuture, countDownInterval);
    }// MyCount
    public void onPause() {
    onPause();
    }// finish
    public void onTick(long millisUntilFinished) {
    matchUpcomingText.setText(""+ formatTime(millisUntilFinished)+ " till match start");
    Log.i(" formatTime ==> ", "" + formatTime(millisUntilFinished));
    Log.i(" millisUntilFinished ==> ", "" + millisUntilFinished);
    }// on tick
    @Override
    public void onFinish() {
    //onStop();
    }// finish
    }

    public static String formatTime(long millis) {
    String output = "00:00";
    try {
    long seconds = millis / 1000;
    long minutes = seconds / 60;
    long hours = seconds / 3600;
    long days = seconds / (3600 * 24);

    seconds = seconds % 60;
    minutes = minutes % 60;
    hours = hours % 24;
    days = days % 30;

    String sec = String.valueOf(seconds);
    String min = String.valueOf(minutes);
    String hur = String.valueOf(hours);
    String day = String.valueOf(days);

    if (seconds < 10)
    sec = "0" + seconds;
    if (minutes < 10)
    min = "0" + minutes;
    if (hours < 10)
    hur = "0" + hours;
    if (days < 10)
    day = "0" + days;

    output = day + "D " + hur + "H " + min + "M " + sec + "S";
    } catch (Exception e) {
    e.printStackTrace();
    }
    return output;
    } 
   

Audio file Save to SdCard and play using MediaPlayer?

 Android has a built in microphone through which you can capture audio and store it , or play it in your phone. There are many ways to do that but the most common way is through MediaRecorder class.
Android provides MediaRecorder class to record audio or video. In order to use MediaRecorder class ,you will first create an instance of MediaRecorder class. Its syntax is given below.
MediaRecorder myAudioRecorder = new MediaRecorder();
Now you will set the source , output and encoding format and output file. Their syntax is given below.
myAudioRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
myAudioRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
myAudioRecorder.setAudioEncoder(MediaRecorder.OutputFormat.AMR_NB);
myAudioRecorder.setOutputFile(outputFile);
After specifying the audio source and format and its output file, we can then call the two basic methods perpare and start to start recording the audio.
myAudioRecorder.prepare();
myAudioRecorder.start();
Apart from these methods , there are other methods listed in the MediaRecorder class that allows you more control over audio and video recording.
Sr.NoMethod & description
1setAudioSource()
This method specifies the source of audio to be recorded
2setVideoSource()
This method specifies the source of video to be recorded
3setOutputFormat()
This method specifies the audio format in which audio to be stored
4setAudioEncoder()
This method specifies the audio encoder to be used
5setOutputFile()
This method configures the path to the file into which the recorded audio is to be stored
6stop()
This method stops the recording process.
7release()
This method should be called when the recorder instance is needed.

Example

 Create MainActivity.java


  1.   public class MainActivity extends Activity {

    private MediaRecorder myAudioRecorder;
    private String outputFile = null;
    private Button start, stop, play, pause;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    start = (Button) findViewById(R.id.button1);
    stop = (Button) findViewById(R.id.button2);
    play = (Button) findViewById(R.id.button3);
    pause = (Button) findViewById(R.id.button4);

    stop.setEnabled(false);
    play.setEnabled(false);
    pause.setEnabled(false)
    ; outputFile = Environment.getExternalStorageDirectory().getAbsolutePath() + "/myrecording.3gp";  

    myAudioRecorder = new MediaRecorder();
    myAudioRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
    myAudioRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
    myAudioRecorder.setAudioEncoder(MediaRecorder.OutputFormat.AMR_NB);
    myAudioRecorder.setOutputFile(outputFile);
    }

    public void start(View view) {
    try {
    myAudioRecorder.prepare();
    myAudioRecorder.start();
    } catch (IllegalStateException e) {
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    }
    start.setEnabled(false);
    stop.setEnabled(true);
    pause.setEnabled(false);
    Toast.makeText(getApplicationContext(), "Recording started", Toast.LENGTH_LONG).show();

    }

    public void stop(View view) {
    myAudioRecorder.stop();
    myAudioRecorder.release();
    myAudioRecorder = null;
    stop.setEnabled(false);
    play.setEnabled(true);
    Toast.makeText(getApplicationContext(), "Audio recorded successfully", Toast.LENGTH_LONG).show();
    }
    public void pause(View view) throws IllegalArgumentException,
    SecurityException, IllegalStateException, IOException  {
    MediaPlayer media = new MediaPlayer();
    media.setDataSource(outputFile);
    media.prepare();
    media.pause();
    Toast.makeText(getApplicationContext(), "Audio pause ", Toast.LENGTH_LONG).show();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
    }

    public void play(View view) throws IllegalArgumentException, SecurityException, IllegalStateException, IOException {

    MediaPlayer media = new MediaPlayer();
    if (media.isPlaying()) {
    media.setDataSource(outputFile);
         media.prepare();
      media.pause();
            } else {
             media.setDataSource(outputFile);
         media.prepare();
         media.start();         
            }  
    pause.setEnabled(true);
    Toast.makeText(getApplicationContext(), "Playing audio", Toast.LENGTH_LONG).show();
    }
    }

main.xml

  1.   <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity" >

        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentRight="true"
            android:layout_alignParentTop="true"
            android:layout_marginTop="32dp"
            android:text="Recording"
            android:textAppearance="?android:attr/textAppearanceMedium" />

        <ImageView
            android:id="@+id/imageView1"
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:layout_below="@+id/textView1"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="37dp"
            android:scaleType="fitXY"
            android:src="@android:drawable/presence_audio_online" />

        <LinearLayout
            android:id="@+id/linear"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/imageView1"
            android:orientation="horizontal" >

            <Button
                android:id="@+id/button1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@+id/imageView1"
                android:layout_toLeftOf="@+id/imageView1"
                android:onClick="start"
                android:text="start" />

            <Button
                android:id="@+id/button2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignBaseline="@+id/button1"
                android:layout_alignBottom="@+id/button1"
                android:layout_alignRight="@+id/textView1"
                android:onClick="stop"
                android:text="stop" />

            <Button
                android:id="@+id/button3"
                style="?android:attr/buttonStyleSmall"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@+id/button2"
                android:onClick="play"
                android:text="play" />

            <Button
                android:id="@+id/button4"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignBottom="@+id/button2"
                android:layout_alignRight="@+id/textView1"
                android:onClick="pause"
                android:text="pause" />
        </LinearLayout>

    </RelativeLayout>

Send Data to Gmail useing Android Application?


Following section explains different parts of our Intent object required to send an email.

Intent Object - Action to send Email

You will use ACTION_SEND action to launch an email client installed on your Android device. Following is simple syntax to create an intent with ACTION_SEND action
Intent emailIntent = new Intent(Intent.ACTION_SEND);

Intent Object - Data/Type to send Email

To send an email you need to specify mailto: as URI using setData() method and data type will be totext/plain using setType() method as follows:
emailIntent.setData(Uri.parse("mailto:"));
emailIntent.setType("text/plain");

Intent Object - Extra to send Email

Android has built-in support to add TO, SUBJECT, CC, TEXT etc. fields which can be attached to the intent before sending the intent to a target email client. You can use following extra fields in your email:
S.N.Extra Data & Description
1EXTRA_BCC
A String[] holding e-mail addresses that should be blind carbon copied.
2EXTRA_CC
A String[] holding e-mail addresses that should be carbon copied.
3EXTRA_EMAIL
A String[] holding e-mail addresses that should be delivered to.
4EXTRA_HTML_TEXT
A constant String that is associated with the Intent, used with ACTION_SEND to supply an alternative to EXTRA_TEXT as HTML formatted text.
5EXTRA_SUBJECT
A constant string holding the desired subject line of a message.
6EXTRA_TEXT
A constant CharSequence that is associated with the Intent, used with ACTION_SEND to supply the literal data to be sent.
7EXTRA_TITLE
A CharSequence dialog title to provide to the user when used with a ACTION_CHOOSER.
Here is an example showing you how to assign extra data to your intent:
emailIntent.putExtra(Intent.EXTRA_EMAIL  , new String[]{"recipient@example.com"});
emailIntent.putExtra(Intent.EXTRA_SUBJECT, "subject of email");
emailIntent.putExtra(Intent.EXTRA_TEXT   , "body of email");

Example


MainActivity.java

  1.  public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Button button = (Button) findViewById(R.id.button1);
    button.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {
    // TODO Auto-generated method stub
    Intent i = new Intent(Intent.ACTION_SEND);
    i.setType("message/rfc822");
    i.putExtra(Intent.EXTRA_EMAIL,new String[] { "" });
    i.putExtra(Intent.EXTRA_SUBJECT," \n\n www.google.com"); // your url
    i.putExtra(Intent.EXTRA_TEXT,"new test"); // your text in place of contentStr
    try {
    MainActivity.this.startActivity(Intent.createChooser(i,"Send mail..."));
    } catch (android.content.ActivityNotFoundException ex) {
    ex.printStackTrace();
    }
    }
    });
    }

    }

main.xml

  1.  <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity" >

        <Button
            android:id="@+id/button1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignLeft="@+id/textView1"
            android:layout_below="@+id/textView1"
            android:text="Send data to gmail" />

    </RelativeLayout>

Select DateRange UsingRangePicker.

  /* * This Method is for select range from picker. * */ private fun selectDateRangeUsingRangePicker () { pageNumber = 1 val displ...