2017-07-02 31 views
0

我正在編寫代碼在我的應用程序啓動時播放小尺寸音調。 你能解釋一下我在這裏寫下的代碼的含義嗎?在MainActivity上運行Android基礎上的Android開發

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

在加載的前兩行很容易,然後播放聲音。 然後在線程中等待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 
} 
+0

爲什麼我們使用線程,什麼是mp.start的含義(?)。這是否意味着開始播放聲音。 – user8006058

+0

您可以使用處理程序而不是線程來暫停代碼塊。基本上,您在主線程繁忙時使用線程。和你自己提到的一樣,mp.start()只是表示播放加載的聲音。 – Koorosh