Java onPostExecute有时仅在AsyncTask中调用
我的AsyncTask在我的MainActivity中执行,但它不调用onPostExecute。方法doInBackground在返回语句(通过System.out检查)之前完成 电话:Java onPostExecute有时仅在AsyncTask中调用,java,android,android-asynctask,Java,Android,Android Asynctask,我的AsyncTask在我的MainActivity中执行,但它不调用onPostExecute。方法doInBackground在返回语句(通过System.out检查)之前完成 电话: @Override protected void onStart() { super.onStart(); if (playIntent == null) { playIntent = new Intent(this, MusicService.class);
@Override
protected void onStart() {
super.onStart();
if (playIntent == null) {
playIntent = new Intent(this, MusicService.class);
if (CheckPermissions()) {
Prepare();
}
}
}
private void Prepare() {
MusicStore musicStore = new MusicStore(getApplicationContext());
sSongs = musicStore.getSongs();
StoreParcel storeParcel = new StoreParcel(StoreParcel.StoreAction.READ_PLAYLISTS, this);
musicStore.execute(storeParcel);
if (sSongs.length < 1) {
fabPlayPause.hide();
snbInformation = Snackbar.make(recyclerView, getString(R.string.snb_Information) + "Music.", Snackbar.LENGTH_INDEFINITE);
snbInformation.show();
}
else {
fabPlayPause.show();
}
bindService(playIntent, musicServiceConn, BIND_AUTO_CREATE);
startService(playIntent);
}
@覆盖
受保护的void onStart(){
super.onStart();
如果(playIntent==null){
playIntent=新的意图(这是MusicService.class);
if(CheckPermissions()){
准备();
}
}
}
私人文件{
MusicStore MusicStore=新的MusicStore(getApplicationContext());
sSongs=musicStore.getSongs();
StoreParcel StoreParcel=新的StoreParcel(StoreParcel.StoreAction.READ_播放列表,此文件);
musicStore.execute(storeParcel);
如果(长度小于1){
暂停。隐藏();
snbInformation=Snackbar.make(recyclerView,getString(R.string.snb_信息)+“Music.”,Snackbar.LENGTH_不确定);
snbInformation.show();
}
否则{
暂停。显示();
}
bindService(playIntent、musicServiceConn、BIND_AUTO_CREATE);
startService(playIntent);
}
异步任务:
public class MusicStore extends AsyncTask<StoreParcel, Void, StoreParcel> {
private Context mContext;
public MusicStore(Context context) {
mContext = context;
}
//region AsyncTask
@Override
protected StoreParcel doInBackground(StoreParcel... params) {
StoreParcel parcel = params[0];
StoreParcel storeParcel = new StoreParcel(parcel.getAction(), parcel.getPlaylistInterface());
switch (parcel.getAction()) {
case WRITE_PLAYLISTS:
WritePlaylists(parcel.getPlaylists());
break;
case READ_PLAYLISTS:
storeParcel.setPlaylists(ReadPlaylists());
break;
}
return storeParcel;
}
@Override
protected void onPostExecute(StoreParcel storeParcel) {
if (storeParcel.getAction() == StoreParcel.StoreAction.READ_PLAYLISTS) {
storeParcel.getPlaylistInterface().SyncPlaylists(storeParcel.getPlaylists());
}
super.onPostExecute(storeParcel);
}
//region Methods
private void WritePlaylists(Playlist[] playlists) {
File dir = new File(mContext.getFilesDir() + Preferences.dirPlaylists);
if (!dir.exists()) {
dir.mkdirs();
}
for (File f : dir.listFiles()) {
f.delete();
}
if (playlists == null) return;
String sFilename;
File file;
for (int i = 0; i < playlists.length; i++) {
sFilename = playlists[i].getName();
try {
file = new File(dir, sFilename + ".json");
file.createNewFile();
Writer writer = new OutputStreamWriter(new FileOutputStream(file));
Gson gson = new GsonBuilder().create();
gson.toJson(playlists[i], writer);
writer.close();
}
catch (IOException ioe) {
ioe.printStackTrace();
}
}
}
private Playlist[] ReadPlaylists() {
Playlist[] playlists;
File dir = new File(mContext.getFilesDir() + Preferences.dirPlaylists);
File[] files = dir.listFiles();
if (files == null) return null;
playlists = new Playlist[files.length];
Reader reader = null;
try {
for (int i = 0; i < files.length; i++) {
reader = new InputStreamReader(new FileInputStream(files[i]));
Gson gson = new GsonBuilder().create();
playlists[i] = gson.fromJson(reader, Playlist.class);
}
if (reader != null) reader.close();
}
catch (IOException ioe) {
ioe.printStackTrace();
}
return playlists;
}
//endregion
//endregion
}
公共类MusicStore扩展异步任务{
私有上下文;
公共音乐存储(上下文){
mContext=上下文;
}
//区域异步任务
@凌驾
受保护的StoreParcel doInBackground(StoreParcel…参数){
StoreParcel地块=参数[0];
StoreParcel StoreParcel=新的StoreParcel(parcel.getAction(),parcel.getPlayInterface());
开关(parcel.getAction()){
案例写入播放列表:
WritePlaylists(parcel.getPlaylists());
打破
案例阅读播放列表:
setPlaylists(ReadPlaylists());
打破
}
归还包裹;
}
@凌驾
PostExecute时受保护的无效(StoreParcel StoreParcel){
if(storeParcel.getAction()==storeParcel.StoreAction.READ_播放列表){
storeParcel.getplaylist().SyncPlaylists(storeParcel.getPlaylists());
}
super.onPostExecute(storeParcel);
}
//区域方法
专用void WritePlaylists(播放列表[]播放列表){
File dir=新文件(mContext.getFilesDir()+Preferences.dirPlaylists);
如果(!dir.exists()){
dir.mkdirs();
}
对于(文件f:dir.listFiles()){
f、 删除();
}
if(playlists==null)返回;
字符串sFilename;
文件;
for(int i=0;i
StoreParcel是我创建的一个类!它只计算一个接口和一个枚举值 我认为您应该在onResume中运行任务,这样您就可以在ui存在时使用它了。您可以在onStart中执行UI操作,onPostExecute必须在UI线程上运行
@Override
protected void onStart() {
super.onStart();
if (playIntent == null) {
playIntent = new Intent(this, MusicService.class);
}
}
@Override
protected void onStart() {
super.onStart();
if (CheckPermissions()) {
Prepare();
}
}
你能用
类发布完整的AsyncTask
吗?所以我添加了完整的MusicStore类!我可以保证总是调用onPostExecute
,但是storeParcel.getAction()
有时不等于READ\u播放列表
,为什么onPostExecute中的system.out没有调用?因此,我的应用程序无法加载!可能是因为在(!bPlaylistsLoaded){}
等待MainActivity中的onPostExecute时,UI冻结了吗。不需要这样做,因为AsyncTask为您处理UI和工作线程。太棒了!很高兴你明白了。