2011-11-29 38 views
3

好的,所以我做了Service,这样我就可以让我的音乐播放器在我所有的活动中工作。至少,这是计划。活动无法绑定到服务,完全相同的代码在不同的活动中工作

我能够在我的一些活动中绑定到我的服务,但在其他活动中,绑定似乎没有做任何事情。

设置 我的服务被开始了我的MainScreen:

startService(new Intent(this, MusicService.class)); 

我试图绑定到它在其他一些活动。活动清单如下:

MainScreen - > FillerActivity - >绑定服务 - >作品

MainScreen - > AlbumList - >的SongList - > SingleSong - >绑定服务 - >不工作

绑定在FillerActivity类中工作,但在SingleSong类中不起作用的原因可能是什么?

我的代码如下:

填料

public class Filler extends Activity { 
    private MusicService s; 


    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.filler); 
     doBindService(); 

     Button b = (Button) findViewById(R.id.terug); 
     b.setOnClickListener(new View.OnClickListener() { 
      public void onClick(View arg0) { 
       Intent intent = new Intent(); 
       setResult(RESULT_OK, intent); 
       finish(); 
      } 
     }); 

    } 

    public void startSong(View view){ 
     if(s != null){ 
     s.startSong(s.songPlaying); 
     System.out.println("Song started"); 
     } 
    } 

    public void stopSong(View view){ 
     if(s != null){ 
     s.stopSong(); 
     System.out.println("Song paused"); 
     } 
    } 


    private ServiceConnection mConnection = new ServiceConnection() { 

     public void onServiceConnected(ComponentName className, IBinder binder) { 
      s = ((MusicService.MyBinder) binder).getService(); 
      Toast.makeText(Filler.this, "Connected", 
        Toast.LENGTH_SHORT).show(); 
      System.out.println("s is gevuld"); 
     } 

     public void onServiceDisconnected(ComponentName className) { 
      s = null; 
     } 
    }; 


    void doBindService() { 
     bindService(new Intent(this, MusicService.class), mConnection, 
       Context.BIND_AUTO_CREATE); 
    } 
} 

SingleSong

public class SingleSongInfo extends Activity { 

    private MediaDetail mDetails; 
    private TextView textView; 
    boolean playing = false; 
    private String songURL; 
    private MusicService s; 
    Button b; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.album_song_info); 
     Bundle bun = getIntent().getExtras(); 

     doBindService(); 

     if (s != null) { 
      System.out.println("S filled"); 

     } else { 
      System.out.println("S empty"); 
     } 

     b = (Button) findViewById(R.id.playButtonSongPage); 

     mDetails = (MediaDetail) bun.getSerializable("details"); 
     if (mDetails != null) { 
      System.out.println("Details gevuld"); 
      songURL = mDetails.getSong_url(); 

      setUpTextViews(); 
      setUpImage(); 

      b.setOnClickListener(new View.OnClickListener() { 
       public void onClick(View arg0) { 

       } 
      }); 

     } 

    } 

    public void buttonPressed(View view) { 

     if (s != null) { 
      if (!s.isPlaying()) { 
       s.startSong(songURL); 
       b.setBackgroundResource(R.drawable.button_pause); 
      } else { 
       s.stopSong(); 
       b.setBackgroundResource(R.drawable.button_play); 
      } 
     } else { 
      System.out.println("S is leeg"); 
     } 
    } 

    private ServiceConnection mConnection = new ServiceConnection() { 

     public void onServiceConnected(ComponentName className, IBinder binder) { 
      s = ((MusicService.MyBinder) binder).getService(); 
      Toast.makeText(SingleSongInfo.this, "Connected", Toast.LENGTH_SHORT) 
        .show(); 
     } 

     public void onServiceDisconnected(ComponentName className) { 
      s = null; 
     } 
    }; 

    void doBindService() { 
     bindService(new Intent(this, MusicService.class), mConnection, 
       Context.BIND_AUTO_CREATE); 
    } 

} 

任何帮助是极大的赞赏。


做一些更多的测试后,一个有趣的发现。 当我直接从MainScreen调用SingleSong类时,MediaService对象s被填充并且可用。它甚至会打印一条消息,表明它已被填充。

但是,当SingleSong类通过SongList打开(它在其他类的tabview中打开SingleSong类)时,该对象没有被填充。但是,这是奇怪的地方,它不会打印出一个消息,即该对象没有被填充,即使它应该。这个代码可以在下面找到。

什么可能导致代码完全不在tabView中运行?使用

代码:

private ServiceConnection mConnection = new ServiceConnection() { 

     public void onServiceConnected(ComponentName className, IBinder binder) { 
      s = ((MusicService.MyBinder) binder).getService(); 
      Toast.makeText(SingleSongInfo.this, "Connected", Toast.LENGTH_SHORT) 
        .show(); 

      if (s != null) { 
       System.out.println("S filled"); // works when calling class directly 

      } else { 
       System.out.println("S empty"); // Does not print when object is empty when class is called through SongList (tabview) 
      } 
     } 

     public void onServiceDisconnected(ComponentName className) { 
      s = null; 
     } 
    }; 
+0

stacktrace上的任何警告或错误? logcat的?如果您也在此处提供有关服务的日志,这将会很有帮助。 –

+0

没有错误,没有任何东西。难道这是我的SingleSong活动在一个Tab小部件中的事实吗? –

+0

您可以通过从TabWidget中提取SingleSong活动并查看它是否有效来证明它。 –

回答

4

管理的最终解决这个问题。它发生是因为我在TabView中使用SingleSong类。由于这个原因,我不得不做出一个小而重要的调整。

private void doBindService() { 

     getApplicationContext().bindService(// getApplicationContext() fixed the problem here. 
       new Intent(getApplicationContext(), MusicService.class), 
       mConnection, Context.BIND_AUTO_CREATE); 

    }