gpt4 book ai didi

android - Activity 无法绑定(bind)到服务,完全相同的代码适用于不同的 Activity

转载 作者:太空宇宙 更新时间:2023-11-03 13:35:05 25 4
gpt4 key购买 nike

好的,所以我创建了一个服务,这样我就可以让我的音乐播放器在我的所有 Activity 中正常工作。至少,这是计划。

我可以在我的一些 Activity 中绑定(bind)到我的服务,但在其他 Activity 中,绑定(bind)似乎没有做任何事情。

设置我的服务在主屏幕上启动:

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

我正尝试在其他一些 Activity 中绑定(bind)它。 Activity 列表如下所示:

MainScreen -> FillerActivity -> 绑定(bind)到服务 -> Works

MainScreen -> AlbumList -> SongList -> SingleSong -> 绑定(bind)到服务 -> 不工作

绑定(bind)在 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);
}
}

单曲

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 被填充并可用。它甚至会打印一条消息,表明它已被填满。

但是,当通过 SongList 打开 SingleSong 类时(它在带有其他一些类的选项卡 View 中打开 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;
}
};

最佳答案

最终设法解决了问题。发生这种情况是因为我在 tabView 中使用了 SingleSong 类。因此,我不得不进行一个小而重要的调整。

private void doBindService() {

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

}

关于android - Activity 无法绑定(bind)到服务,完全相同的代码适用于不同的 Activity ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8313082/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com