2014-02-23 66 views
0

這真的是一個音樂播放器?他們的行爲有什麼不同?請讓我明白!我對使用什麼感到困惑。我嘗試使用綁定服務製作音樂播放器,但問題是每當我退出我的應用程序並再次輸入時,就會啓動一個新的serviceConnection。我期待着每次回到我的應用程序時,它都會意識到音樂已經被激活了。對於我有玩和暫停功能的實例。當我播放音樂時,退出應用程序,然後返回,我期待着我可以阻止播放器。但相反,它會播放另一種音樂。所以現在我很困惑,我應該真的使用綁定?或開始服務?已啓動還是已綁定服務?

public class MyService extends Service{ 

public IBinder myBinder = new MyPlaylistBinder(); 
@Override 
public IBinder onBind(Intent arg0) { 
    // TODO Auto-generated method stub 
    return myBinder; 
} 
public class MyPlaylistBinder extends Binder{ 
    MyService getService(){ 
     return MyService.this; 
    } 
} 

上面的代碼是我MyService.java

public ServiceConnection myConnection = new ServiceConnection(){ 

    @Override 
    public void onServiceConnected(ComponentName name, IBinder service) { 
     // TODO Auto-generated method stub 
     MyPlaylistBinder binder = (MyPlaylistBinder) service; 
     myService = binder.getService(); 
     Toast.makeText(getApplicationContext(), "Service is connected", Toast.LENGTH_SHORT).show(); 
    } 

    @Override 
    public void onServiceDisconnected(ComponentName name) { 
     // TODO Auto-generated method stub 
     Toast.makeText(getApplicationContext(), "Service is disconnected", Toast.LENGTH_SHORT).show(); 
    } 

}; 

而這一次是我MainActivity.java

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    btnPrevious = (ImageButton)findViewById(R.id.btnPrevious); 
    btnBackward = (ImageButton)findViewById(R.id.btnBackward); 
    btnPlay = (ImageButton)findViewById(R.id.btnPlay); 
    btnForward = (ImageButton)findViewById(R.id.btnForward); 
    btnNext = (ImageButton)findViewById(R.id.btnNext); 

    ComponentName myService = startService(new Intent(this, MyService.class)); 
    Intent intent = new Intent(this, MyService.class); 
    bindService(intent, myConnection, Context.BIND_AUTO_CREATE); 


    listview = (ListView)findViewById(R.id.listView1); 
    ContentResolver resolver = getContentResolver(); 
    Uri uri = android.provider.MediaStore.Audio.Media.EXTERNAL_CONTENT_URI; 
    cursor = resolver.query(uri, null, null, null, null); 

上方的部分是在MainActivity的onCreate的一部分() 正如你可以看到我沒有使用AIDL,因爲我沒有嘗試過,恐怕我可能會變得更糟。請原諒,我只是在安卓:(

+0

請用您當前的代碼說明這篇文章。沒有這一點就很難提供幫助。 – ben75

+0

我有一個問題,我不能發佈圖像,因爲我的聲譽太低:( – user3062224

+0

有沒有需要的圖像,只是相關部分的代碼 – ben75

回答

1

Documentation初學者:

開始

服務的「啓動」,當一個應用程序組件(如 活動)通過調用startService()啓動它一旦啓動, 服務可以無限期地在後臺運行,即使啓動它的組件 已被銷燬通常,啓動的服務執行 單個操作並且不會將結果返回給調用者。例如,對於 ,它可能通過網絡下載或上傳文件。當 操作完成時,服務應該自行停止。

束縛

服務是 「結合」,當一個應用程序組件通過調用 bindService結合它()。綁定服務提供客戶端 - 服務器接口,允許組件與服務交互,發送請求,獲得結果,甚至在進程間通過進程間通信(IPC)進行。一個綁定服務只在另一個 應用程序組件綁定到它時運行。 多個組件可以立即綁定到 服務,但是當它們全部解除綁定時,該服務被銷燬 。

基於此,您可能想要啓動服務,因爲您打算在啓動服務的組件不再運行之後繼續運行服務。爲了方便用戶,只要服務正在運行,您應該考慮持續發出通知,並且包括允許用戶直接暫停,恢復或完全停止播放的音樂控件(最後一個可以調用stopService,在此時你取消通知)。

+0

所以你說我應該使用「啓動」服務而不是「綁定」。你能否給我一個使用綁定服務的現有應用程序? – user3062224

+0

也許您可以通過Google搜索快速找到教程或示例應用程序。除了我寫的應用程序之外,我不知道開發人員在應用程序內部做了什麼。但基於你的原始問題,它聽起來像你應該使用啓動的服務。 – Karakuri

+0

我也在想。但是我想知道組件綁定到服務的情況? – user3062224

相關問題