如何在完成申请之前完成处理程序?

时间:2012-12-02 06:23:18

标签: android

当我触摸背景颜色为白色的区域时,我有一个处理程序在屏幕上显示消息。

                  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毫秒的时间内运行一个新的意图它会崩溃,我很确定,因为它无法完成处理程序提示。

告诉处理程序,如果aplication正在关闭,那么运行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的编程了解最多,谢谢!

这是代码固定感谢山姆! :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);

1 个答案:

答案 0 :(得分:1)

  

但如果我在小于1000毫秒的时间内运行一个新的意图它会崩溃,我很确定,因为它无法完成处理程序提示。

Handler#removeCallbacks(Runnable)将取消正在队列中等待的传递的Runnable。

创建两个字段变量以保存对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);  

(您可能也想在removeCallbacks()中致电onPause()。)