gpt4 book ai didi

java - 如何仅在 doinbackground 中的方法完成时调用 onPostExecute?

转载 作者:太空宇宙 更新时间:2023-11-04 09:32:32 25 4
gpt4 key购买 nike

我在 onClickListener 中调用它

AsyncTaskBindService asyncTaskBindService = new AsyncTaskBindService(position, songList);

asyncTaskBindService.execute();

异步任务类

public class AsyncTaskBindService extends AsyncTask<Void, Void, Void>{

private int songIndex;
private ArrayList<Song> songList;

public AsyncTaskBindService(int songIndex, ArrayList<Song> songList){
this.songIndex = songIndex;
this.songList = songList;
}

@Override
protected Void doInBackground(Void... voids) {
try {
if (getActivity() != null && !Main.mServiceIsBound) {
Main.bindMusicService(getActivity().getApplicationContext());
Log.i(TAG,"binding service!");
}
}catch (Exception e){
Log.e(TAG,"error binding Service!");
}
return null;
}

@Override
protected void onPostExecute(Void aVoid) {
if (getActivity() != null && Main.mServiceIsBound) {
Main.mediaPlayerService.startActionPlay(getActivity().getApplicationContext(), songList, songIndex);
Log.i(TAG,"start song!");
}
}

问题出在我的 doInBackground 中:

这一行:

Main.bindMusicService(getActivity().getApplicationContext());

此方法调用另一个类中的bindService,然后该类将一个实例返回到我的服务。

但问题是 Asynctask 完成得很快,当调用 onPostExecute 时,它会返回一个 NPE,因为 mediaPlayerService 仍然为 null,因为 onServiceConnected 比我的 Asynctask 慢。

如何解决这个问题?

现在我必须点击一首歌曲两次才能开始播放。

编辑

首先我尝试了这个,但我的第二个永远不会被调用,因为 onServiceConnected() 完成之前需要一些时间。

如果bindService完成并且我的服务已绑定(bind),我如何立即执行第二个方法?

点击歌曲

if (!Main.mServiceIsBound && getActivity() != null) {
Main.bindMusicService(getActivity().getApplicationContext());
Log.i(TAG,"Service is not bound yet!, binding Service...");
}
if (getActivity()!= null && Main.mServiceIsBound) {
Main.mediaPlayerService.startActionPlay(getActivity().getApplicationContext(), songList, position);
}

主类

/**This establishes the connection to the MediaPlayerService. */
public static ServiceConnection serviceConnection = new ServiceConnection() {

@Override
public void onServiceConnected(ComponentName name, IBinder service) {
MediaPlayerService.MusicBinder binder = (MediaPlayerService.MusicBinder)service;
mediaPlayerService = binder.getService();
mServiceIsBound = true;
Log.i("Main","MediaPlayerService Connected!");
}

@Override
public void onServiceDisconnected(ComponentName name) {
mServiceIsBound = false;
Log.i("Main","MediaPlayerService Disconnected!");
}
};

public static void bindMusicService(Context c){
/*mediaPlayerServiceIntent binds our connection to the MediaPlayerService. */
try{
mediaPlayerServiceIntent = new Intent(c, MediaPlayerService.class);
c.bindService(mediaPlayerServiceIntent, serviceConnection, Context.BIND_AUTO_CREATE);
}catch (Exception e){
Log.e("Main", "Service is not bound!");
}
Log.i("Main","Service is bound!");
}

最佳答案

这是因为 AsyncTaskdoInBackGround 是异步运行的,但是当您使用 bindService 将组件绑定(bind)到 Service 时,它也会异步运行,导致 Main.bindMusicService 立即返回。

AsyncTask 不适合此类工作。您应该绑定(bind)到主线程(UI 线程)中的服务,并监听 ServiceConnection 对象的 onServiceConnected 回调。

关于java - 如何仅在 doinbackground 中的方法完成时调用 onPostExecute?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56910729/

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