这是我有一个处理程序,当我触摸背景颜色为白色的区域时,它会在屏幕上显示一条消息。
else if (ct.closeMatch (Color.WHITE, touchColor, tolerance))
{ Random r = new Random();
int txt= r.nextInt(6-0) + 0;
if(txt==0){ variables.pointtxt = "Nothing interesting"; }
else if (txt==1){ variables.pointtxt = "There´s nothing there"; }
else if (txt==2){ variables.pointtxt = "I can´t do nothing with that"; }
else if (txt==3){ variables.pointtxt = "Wait... nop nothing"; }
else if (txt==4){ variables.pointtxt = "Nothing"; }
else if (txt==5){ variables.pointtxt = "More nothing"; }
LayoutInflater layoutInflater = (LayoutInflater)getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View popupView = layoutInflater.inflate(R.layout.popup, null);
final PopupWindow popupWindow = new PopupWindow(
popupView,
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT);
TextView text = (TextView) popupView.findViewById(R.id.popuptxt);
text.setText(variables.pointtxt);
popupWindow.showAtLocation(popupView, Gravity.CENTER, 0, 250) ;
new Handler().postDelayed(new Runnable()
{
public void run()
{ if (popupWindow.isShowing()== true)
popupWindow.dismiss();
}
}, 1000);
}
但是,如果我在不到 1000 毫秒的时间内运行一个新 Intent ,它就会崩溃,我很确定,因为它无法完成处理程序提示。
有什么方法可以告诉处理程序,如果应用程序正在关闭,则运行 popupWindow.dismiss(); ?
或者有没有办法告诉我什么时候打电话(触摸红色的东西时)
if (ct.closeMatch (Color.RED, touchColor, tolerance)) {
Intent game = new Intent(lvl2_1_0.this, lvl2_1_1.class);
game.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
game.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(game);
}
要完成所有处理程序提示吗?
我一直在寻找,但找不到一种方法可以在完成应用程序之前强制完成应用程序中的所有处理程序。
我知道我可能有点插入它,但在某些情况下,当我测试游戏时它会崩溃,当然也许在正常玩游戏时它不会发生,因为按下按钮来传递屏幕是不可能的弹出窗口出现后不到 1000 毫秒但是我手上出现这个错误让我很烦恼。
感谢您在其他线程中的回答,我学到了有关 Android 编程的最多知识,所以谢谢您!
这是已修复的代码,感谢 sam ! :P
public class lvl2_1_0 extends Activity implements View.OnTouchListener {
private PopupWindow popupWindow;
private Handler mHandler = new Handler();
private Runnable dismissPopup = new Runnable() {
public void run() {
if (popupWindow.isShowing())
popupWindow.dismiss();
}
};
@Override
public void onCreate(Bundle savedInstanceState) {
然后
调用弹出窗口。就我而言:
LayoutInflater layoutInflater = (LayoutInflater)getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View popupView = layoutInflater.inflate(R.layout.popup, null);
popupWindow = new PopupWindow(
popupView,
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT);
TextView text = (TextView) popupView.findViewById(R.id.popuptxt);
text.setText(variables.pointtxt);
popupWindow.showAtLocation(popupView, Gravity.CENTER, 0, 250) ;
mHandler.postDelayed(dismissPopup, 3000);
最终在不强制关闭的情况下关闭应用程序
Intent game = new Intent(lvl2_1_0.this, lvl2_1_1.class);
game.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
game.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
mHandler.removeCallbacks(dismissPopup);
startActivity(game);
最佳答案
But if I run a new intent in les than 1000 miliseconds it crashes, I´m pretty sure because it couldn finish the handler cue.
Handler#removeCallbacks(Runnable)
将取消传递的 Runnable f 它正在队列中等待。
创建两个字段变量来保存对 Handler 和 Runnable 的引用:
public class Example extends Activity {
private Handler mHandler = new Handler();
private Runnable dismissPopup = new Runnable() {
public void run() {
if (popupWindow.isShowing())
popupWindow.dismiss();
}
}
现在,您之前调用的位置 new Handler().postDelayed(new R...
使用:
mHandler.postDelayed(dismissPopup, 1000);
最后,在启动新的 Intent 调用之前:
...
mHandler.removeCallbacks(mRunnable);`
startActivity(game);
(您可能还想在 onPause()
中调用 removeCallbacks()
。)
关于android - 如何在完成应用程序之前完成处理程序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13666957/