Activity.onCreate 中的 AsyncTask.get() 导致加载时间过长,有时会崩溃
AsyncTask.get() in Activity.onCreate makes it take too much time to load and sometimes it crashes
当我启动这个 activity 时,加载时间太长,有时我的应用程序会崩溃。我想知道是否有什么我可以做的,所以 activity 将不会启动,直到主 activity 在前一个 activity 或类似的东西中完全加载所以我的 activity 加载时不会出现问题或崩溃。
AdvertisingActivity.java
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.vectordrawable.graphics.drawable.Animatable2Compat;
import android.app.ProgressDialog;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.os.Handler;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.DataSource;
import com.bumptech.glide.load.engine.GlideException;
import com.bumptech.glide.load.resource.gif.GifDrawable;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.target.Target;
import java.util.concurrent.ExecutionException;
public class AdvertisingActivity extends AppCompatActivity {
ImageView imageView;
ImageView imageButton;
String ImageClick = "";
String ImageLink = "";
private TextView Countdowner;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.advertising);
getSupportActionBar().hide();
imageView = findViewById(R.id.Advertisings);
imageButton = findViewById(R.id.closebuttons);
imageButton.setVisibility(View.GONE);
Countdowner = findViewById(R.id.Countdowns);
final ParseJson parsejson = new ParseJson();
try {
parsejson.execute().get();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
ImageLink = parsejson.getHouseAdsImage();
ImageClick = parsejson.getHouseAdsClick();
boolean ifGif = parsejson.getGIF();
imageButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
finish();
}
});
imageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(parsejson.getHouseAdsClick()));
AdvertisingActivity.this.startActivity(browserIntent);
}
});
if (ifGif == true) {
Glide.with(this).asGif().load(parsejson.getHouseAdsImage()).listener(new RequestListener<GifDrawable>() {
@Override
public boolean onLoadFailed(@Nullable @org.jetbrains.annotations.Nullable GlideException e, Object model, Target<GifDrawable> target, boolean isFirstResource) {
finish();
Toast.makeText(AdvertisingActivity.this, "Failed to load", Toast.LENGTH_SHORT).show();
return false;
}
@Override
public boolean onResourceReady(GifDrawable resource, Object model, Target<GifDrawable> target, DataSource dataSource, boolean isFirstResource) {
resource.setLoopCount(1);
resource.registerAnimationCallback(new Animatable2Compat.AnimationCallback() {
@Override
public void onAnimationEnd(Drawable drawable) {
Glide.with(AdvertisingActivity.this).load(parsejson.getAfterGif()).into(imageView);
Toast.makeText(AdvertisingActivity.this, "GIF stopped", Toast.LENGTH_SHORT).show();
}
});
new CountDownTimer(13000, 1000) {
public void onTick(long millisUntilFinished) {
Countdowner.setText("You can close ad after: " + millisUntilFinished / 1000);
}
public void onFinish() {
imageButton.setVisibility(View.VISIBLE);
Countdowner.setVisibility(View.GONE);
}
}.start();
Toast.makeText(AdvertisingActivity.this, "Success to load", Toast.LENGTH_SHORT).show();
return false;
}
}).into(imageView);
} else {
Glide.with(AdvertisingActivity.this).load(parsejson.getAfterGif()).into(imageView);
}
}
}
您的问题是您正在主线程上同步等待 AsyncTask
完成:
parsejson.execute().get();
你基本上不应该在主线程上调用 AsyncTask.get()
:它会阻塞线程直到它完成,这违背了 AsyncTask 的要点,即 not 阻塞主线程。
相反,您应该在 AsyncTask.onPostExecute
.
中做任何您想做的事
当我启动这个 activity 时,加载时间太长,有时我的应用程序会崩溃。我想知道是否有什么我可以做的,所以 activity 将不会启动,直到主 activity 在前一个 activity 或类似的东西中完全加载所以我的 activity 加载时不会出现问题或崩溃。
AdvertisingActivity.java
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.vectordrawable.graphics.drawable.Animatable2Compat;
import android.app.ProgressDialog;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.os.Handler;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.DataSource;
import com.bumptech.glide.load.engine.GlideException;
import com.bumptech.glide.load.resource.gif.GifDrawable;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.target.Target;
import java.util.concurrent.ExecutionException;
public class AdvertisingActivity extends AppCompatActivity {
ImageView imageView;
ImageView imageButton;
String ImageClick = "";
String ImageLink = "";
private TextView Countdowner;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.advertising);
getSupportActionBar().hide();
imageView = findViewById(R.id.Advertisings);
imageButton = findViewById(R.id.closebuttons);
imageButton.setVisibility(View.GONE);
Countdowner = findViewById(R.id.Countdowns);
final ParseJson parsejson = new ParseJson();
try {
parsejson.execute().get();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
ImageLink = parsejson.getHouseAdsImage();
ImageClick = parsejson.getHouseAdsClick();
boolean ifGif = parsejson.getGIF();
imageButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
finish();
}
});
imageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(parsejson.getHouseAdsClick()));
AdvertisingActivity.this.startActivity(browserIntent);
}
});
if (ifGif == true) {
Glide.with(this).asGif().load(parsejson.getHouseAdsImage()).listener(new RequestListener<GifDrawable>() {
@Override
public boolean onLoadFailed(@Nullable @org.jetbrains.annotations.Nullable GlideException e, Object model, Target<GifDrawable> target, boolean isFirstResource) {
finish();
Toast.makeText(AdvertisingActivity.this, "Failed to load", Toast.LENGTH_SHORT).show();
return false;
}
@Override
public boolean onResourceReady(GifDrawable resource, Object model, Target<GifDrawable> target, DataSource dataSource, boolean isFirstResource) {
resource.setLoopCount(1);
resource.registerAnimationCallback(new Animatable2Compat.AnimationCallback() {
@Override
public void onAnimationEnd(Drawable drawable) {
Glide.with(AdvertisingActivity.this).load(parsejson.getAfterGif()).into(imageView);
Toast.makeText(AdvertisingActivity.this, "GIF stopped", Toast.LENGTH_SHORT).show();
}
});
new CountDownTimer(13000, 1000) {
public void onTick(long millisUntilFinished) {
Countdowner.setText("You can close ad after: " + millisUntilFinished / 1000);
}
public void onFinish() {
imageButton.setVisibility(View.VISIBLE);
Countdowner.setVisibility(View.GONE);
}
}.start();
Toast.makeText(AdvertisingActivity.this, "Success to load", Toast.LENGTH_SHORT).show();
return false;
}
}).into(imageView);
} else {
Glide.with(AdvertisingActivity.this).load(parsejson.getAfterGif()).into(imageView);
}
}
}
您的问题是您正在主线程上同步等待 AsyncTask
完成:
parsejson.execute().get();
你基本上不应该在主线程上调用 AsyncTask.get()
:它会阻塞线程直到它完成,这违背了 AsyncTask 的要点,即 not 阻塞主线程。
相反,您应该在 AsyncTask.onPostExecute
.