2012-09-22 16 views
0

對於以下代碼,如您所見,一個new IntentbindService()中,另一個new Intent發生在startService()中。我只是想知道最終會有兩個意圖嗎?或者兩個意圖還可以嗎?爲什麼有兩個「新意圖」?他們會做出兩種不同的意圖?

bindService(new Intent(this, MusicPlayerService.class), 
     mPlaybackConnection, Context.BIND_AUTO_CREATE); 
startService(new Intent(this, MusicPlayerService.class)); 

回答

4

此代碼等同於:

Intent intent = new Intent(this, MusicPlayerService.class); 
bindService(intent, mPlaybackConnection, Context.BIND_AUTO_CREATE); 
startService(intent); 

在你提供中,相同的對象Intent每次創建的代碼。

代碼是相同的,因爲它們都執行相同的操作。但是,始終使用一個Intent會非常​​快,因爲該對象只創建一次。除此之外,兩者都是正確的,兩者都做同樣的事情。

1

我不認爲這個代碼,

bindService(new Intent(this, MusicPlayerService.class), 
     mPlaybackConnection, Context.BIND_AUTO_CREATE); 
startService(new Intent(this, MusicPlayerService.class)); 

相當於,

Intent intent = new Intent(this, MusicPlayerService.class); 
bindService(intent, mPlaybackConnection, Context.BIND_AUTO_CREATE); 
startService(intent); 

在第一位的,有創造兩個不同的意圖。但在第二個中,只創建了一個意圖,所以更好地使用第二個代碼。

+0

你在這兩點上提供的代碼是相同的...... –

+0

糟糕!代碼更新.. –

+0

謝謝你的回答!但誰是對的? –

相關問題