在MainActivity上运行基调的Android开发

时间:2017-07-02 15:41:13

标签: android android-mediaplayer eclipse-adt android-thread

我正在编写代码,以便在启动我的应用时播放小音调。 你能解释一下我在这里写下的代码的含义吗?

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();
}

1 个答案:

答案 0 :(得分:1)

在前两行中加载然后播放声音很容易。 然后在线程中等待3秒然后开始其他活动。

逐行分析:

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
}