如何在一定时间后隐藏自定义加载图标 (android)?
How to hide a custom loading icon after a certain amount of time (android)?
我目前正在实现自己的自定义进度对话框,一旦结果出现 in/error,我就会在其中调用显示和隐藏。但是,我想实现一个自定义方法,说明如果进度对话框在 10 秒后无论如何都没有隐藏,请将其隐藏并发出警报。
这是我的自定义进度对话框,我的方法有效但不完全。
public class CustomProgressDialog extends ProgressDialog {
private AnimationDrawable animation;
private CountDownTimer cTimer = null;
private Context mContext;
public CustomProgressDialog(Context context) {
super(context);
mContext = context;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.custom_progress_dialog);
ImageView la = (ImageView) findViewById(R.id.animation);
la.setBackgroundResource(R.drawable.custom_progress_dialog_animation);
animation = (AnimationDrawable) la.getBackground();
}
@Override
public void show() {
super.show();
animation.start();
startTimer();
}
@Override
public void dismiss() {
super.dismiss();
animation.stop();
if(cTimer != null) {
cTimer.cancel();
}
}
//timer added just in case progress dialog does not stop on its own
private void startTimer() {
cTimer = new CountDownTimer(10000, 1000) {
public void onTick(long millisUntilFinished) {
}
public void onFinish() {
dismiss();
AlertDialogManager alert = new AlertDialogManager();
alert.showAlertDialog(mContext, mContext.getString(R.string.loadingErr), mContext.getString(R.string.loadingErrTxt), 3);
}
}.start();
}
@Override
public void onDetachedFromWindow() {
super.onDetachedFromWindow();
animation.stop();
cTimer.cancel();
}
}
这就是我在 activity/fragment 中实现它的方式:
private void showProgressDialog() {
customProgressDialog = new CustomProgressDialog(this);
customProgressDialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
customProgressDialog.show();
//so it cannot be closed by user first one lets back button cancel it
//customProgressDialog.setCanceledOnTouchOutside(false);
customProgressDialog.setCancelable(false);
}
private void hideProgressDialog() {
if(customProgressDialog != null) {
//customProgressDialog.hide();
}
}
更新:这是我试过的第二个选项,它仍然没有阻止弹出警报,这让我觉得即使在取消时计时器仍在继续。
这是activity:
private void autoProgressShutdown() {
Runnable progressRunnable = new Runnable() {
@Override
public void run() {
customProgressDialog.cancel();
callAlert();
}
};
Handler pdCanceller = new Handler();
pdCanceller.postDelayed(progressRunnable, 10000);
}
private void callAlert() {
AlertDialogManager alert = new AlertDialogManager();
alert.showAlertDialog(this, getString(R.string.loadingErr), getString(R.string.loadingErrTxt), 3);
}
private void showProgressDialog() {
customProgressDialog = new CustomProgressDialog(this);
customProgressDialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
customProgressDialog.show();
//so it cannot be closed by user first one lets back button cancel it
//customProgressDialog.setCanceledOnTouchOutside(false);
customProgressDialog.setCancelable(false);
autoProgressShutdown();
}
private void hideProgressDialog() {
customProgressDialog.cancel();
if(customProgressDialog != null) {
customProgressDialog.hide();
}
}
在自定义对话框中编辑文件以删除所有计时器内容并添加:
@Override
public void setOnCancelListener(OnCancelListener listener) {
super.setOnCancelListener(listener);
dismiss();
}
可能的问题:
-不确定是否存在内存泄漏问题,因为我没有销毁它
明确的问题:
- 如果进度对话框被隐藏,警告在 10 秒后仍然弹出,这意味着取消没有被调用
-此外,如果我切换屏幕不确定取消是否足以破坏计时器
您可以使用 Handler#postDelayed
稍后在给定线程上执行某些操作,您可以使用 Handler#removeCallbacksAndMessages
取消挂起的任务。如果你用 null
调用它,如果你有一些你需要在发布的任务中阻止的东西,它只会取消处理程序上的任何挂起。
这是您的对话,但会自行终止:
class SuicideDialog extends Dialog{
private Handler mAutoTerminationHandler;
@Override
public void onShow(){
mAutoTerminationHandler = new Handler();
}
@Override
public void show(){
super.show();
mAutoTerminationHandler.postDelayed(new Runnable(){
dismiss();
}, 666L);
}
@Override
public void dismiss(){
mAutoTerminationHandler.removeCallbacksAndMessages(null);
super.dismiss();
}
}
或者,您可以将生命周期监听放入它自己的 class:
class ShownTaskListener implements OnShowListener, OnDismissListener {
private Handler mHandler;
@Override
public ShownTaskListener(Handler handler, Runnable showTask){
mHandler = handler;
mShowTask = showTask;
}
// from OnShowListener
@Override
public void onShow(){
mHandler.postDelayed(mShowTask, 666L);
}
// from OnDismissListener
@Override
public void onDismiss(){
// get rid of all pending actions in the Handler
mHandler.removeCallbacksAndMessages(null);
}
}
然后,您可以通过将此侦听器附加到 Dialog#setOnShowListener
和 Dialog#setOnDismissListener
.
来使用它自行关闭任何 Dialog
我目前正在实现自己的自定义进度对话框,一旦结果出现 in/error,我就会在其中调用显示和隐藏。但是,我想实现一个自定义方法,说明如果进度对话框在 10 秒后无论如何都没有隐藏,请将其隐藏并发出警报。
这是我的自定义进度对话框,我的方法有效但不完全。
public class CustomProgressDialog extends ProgressDialog {
private AnimationDrawable animation;
private CountDownTimer cTimer = null;
private Context mContext;
public CustomProgressDialog(Context context) {
super(context);
mContext = context;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.custom_progress_dialog);
ImageView la = (ImageView) findViewById(R.id.animation);
la.setBackgroundResource(R.drawable.custom_progress_dialog_animation);
animation = (AnimationDrawable) la.getBackground();
}
@Override
public void show() {
super.show();
animation.start();
startTimer();
}
@Override
public void dismiss() {
super.dismiss();
animation.stop();
if(cTimer != null) {
cTimer.cancel();
}
}
//timer added just in case progress dialog does not stop on its own
private void startTimer() {
cTimer = new CountDownTimer(10000, 1000) {
public void onTick(long millisUntilFinished) {
}
public void onFinish() {
dismiss();
AlertDialogManager alert = new AlertDialogManager();
alert.showAlertDialog(mContext, mContext.getString(R.string.loadingErr), mContext.getString(R.string.loadingErrTxt), 3);
}
}.start();
}
@Override
public void onDetachedFromWindow() {
super.onDetachedFromWindow();
animation.stop();
cTimer.cancel();
}
}
这就是我在 activity/fragment 中实现它的方式:
private void showProgressDialog() {
customProgressDialog = new CustomProgressDialog(this);
customProgressDialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
customProgressDialog.show();
//so it cannot be closed by user first one lets back button cancel it
//customProgressDialog.setCanceledOnTouchOutside(false);
customProgressDialog.setCancelable(false);
}
private void hideProgressDialog() {
if(customProgressDialog != null) {
//customProgressDialog.hide();
}
}
更新:这是我试过的第二个选项,它仍然没有阻止弹出警报,这让我觉得即使在取消时计时器仍在继续。
这是activity:
private void autoProgressShutdown() {
Runnable progressRunnable = new Runnable() {
@Override
public void run() {
customProgressDialog.cancel();
callAlert();
}
};
Handler pdCanceller = new Handler();
pdCanceller.postDelayed(progressRunnable, 10000);
}
private void callAlert() {
AlertDialogManager alert = new AlertDialogManager();
alert.showAlertDialog(this, getString(R.string.loadingErr), getString(R.string.loadingErrTxt), 3);
}
private void showProgressDialog() {
customProgressDialog = new CustomProgressDialog(this);
customProgressDialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
customProgressDialog.show();
//so it cannot be closed by user first one lets back button cancel it
//customProgressDialog.setCanceledOnTouchOutside(false);
customProgressDialog.setCancelable(false);
autoProgressShutdown();
}
private void hideProgressDialog() {
customProgressDialog.cancel();
if(customProgressDialog != null) {
customProgressDialog.hide();
}
}
在自定义对话框中编辑文件以删除所有计时器内容并添加:
@Override
public void setOnCancelListener(OnCancelListener listener) {
super.setOnCancelListener(listener);
dismiss();
}
可能的问题: -不确定是否存在内存泄漏问题,因为我没有销毁它
明确的问题: - 如果进度对话框被隐藏,警告在 10 秒后仍然弹出,这意味着取消没有被调用 -此外,如果我切换屏幕不确定取消是否足以破坏计时器
您可以使用 Handler#postDelayed
稍后在给定线程上执行某些操作,您可以使用 Handler#removeCallbacksAndMessages
取消挂起的任务。如果你用 null
调用它,如果你有一些你需要在发布的任务中阻止的东西,它只会取消处理程序上的任何挂起。
这是您的对话,但会自行终止:
class SuicideDialog extends Dialog{
private Handler mAutoTerminationHandler;
@Override
public void onShow(){
mAutoTerminationHandler = new Handler();
}
@Override
public void show(){
super.show();
mAutoTerminationHandler.postDelayed(new Runnable(){
dismiss();
}, 666L);
}
@Override
public void dismiss(){
mAutoTerminationHandler.removeCallbacksAndMessages(null);
super.dismiss();
}
}
或者,您可以将生命周期监听放入它自己的 class:
class ShownTaskListener implements OnShowListener, OnDismissListener {
private Handler mHandler;
@Override
public ShownTaskListener(Handler handler, Runnable showTask){
mHandler = handler;
mShowTask = showTask;
}
// from OnShowListener
@Override
public void onShow(){
mHandler.postDelayed(mShowTask, 666L);
}
// from OnDismissListener
@Override
public void onDismiss(){
// get rid of all pending actions in the Handler
mHandler.removeCallbacksAndMessages(null);
}
}
然后,您可以通过将此侦听器附加到 Dialog#setOnShowListener
和 Dialog#setOnDismissListener
.
Dialog