0

i am writing code to play a small size tone on starting of my app. can you please explain the meaning of the code which i have written down here?

Log.i("MY IIIT APP","MY SPLASH STATED");

mp=MediaPlayer.create(this, R.drawable.tone);
mp.start();
Thread t=new Thread()
{
    public void run() {
        try{
          sleep(3000);
          Intent i=
                  new Intent(MainActivity.this,JumpedTo.class);
          startActivity(i);
        }
        catch(Exception e)
        {

        }

    }
};
t.start();
}
Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62

1 Answers1

1

It is easy in the first two lines you are loading and then playing a sound. then in the thread you wait for 3 seconds and then start your other activity.

Line By Line Analysis:

Log.i("MY IIIT APP","MY SPLASH STARTED");   //It will give info in Logs as "MY SPLASH STARTED"

mp=MediaPlayer.create(this, R.drawable.tone); // Defines a MediaPlayer with audio(media) "tone"
mp.start(); //Starts playing mp in android framework
Thread t=new Thread() // Defines and initializes a new thread
{
    public void run() {
        try{
          sleep(3000); //Creates delay of 3000 milliseconds or 3 seconds
          Intent i=
                  new Intent(MainActivity.this,JumpedTo.class); //Defines an intent to switch from MainActivity to JumpedTo Activity
          startActivity(i); //Starts the intent
        }
        catch(Exception e)
        {

        }

    }
};
t.start(); // Starts the thread after definition and initialization
}
Nik
  • 173
  • 2
  • 17
Koorosh
  • 457
  • 5
  • 14
  • Why we are using thread?And what is the meaning of mp.start(). Does it means to start playing the sound. – user8006058 Jul 02 '17 at 16:09
  • you can use a handler instead of a thread to suspend a block of code.basically, you use a thread when the main thread is busy. and as you yourself mentioned mp.start() simply means playing the loaded sound. – Koorosh Jul 02 '17 at 16:14