2012-05-30 151 views
0

我有一个应用程序,当用户启动应用程序时,一个计时器启动。在10秒后,弹出一个AlertDialog,表示只有15秒的扩孔并显示一个计时器,并在14秒后消失。这适用于应用程序的第一个活动时正常工作。如果用户从第一个ActivtyTimedNotifyActivity通过,则计时器在10秒后停止。 onUserInteraction()TimedNotify定时器重新启动并且工作绝对正常。请协助我了解我的错在哪里。警报对话框不出现

public class FirstActivity extends TimedNotify{ 
    @Override 
    public void onCreate(Bundle savedInstanceState) 
    {  
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.timercheck);  
     final Button btnstart2 = (Button) findViewById(R.id.btn); 

     btnstart2.setOnClickListener(new OnClickListener() { 
      public void onClick(View v) { 
       Intent intent = new Intent(FirstActivity.this, 
             TimedNotify.class); 
       startActivity(intent); 
      } 
     }); 
    } 
} 

public class TimedAlert extends Activity 
{ 
    static CountDownTimer timer1, timer2; 
    int flag = 0; 
    protected static final String TAG = null; 
    public static AlertDialog alert, alertdialog; 
    private static Context context; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     final TextView mCounter1TextField = (TextView) findViewById (R.id.mCounter1TextField); 

     // first timer set for 10sec 
     timer1 = new CountDownTimer(10000, 1000) 
     { 
      @Override 
      public void onTick(long millisUntilFinished) 
      { 
       Log.v(TAG, "timer1 ticking"); 
       mCounter1TextField.setText("Seconds left: " 
              + formatTime(millisUntilFinished)); 
      } 

      public void onFinish() { 
       //after 10sec display alert box and show timer 
       Log.v(TAG, "timer1 finished"); 
       timer1.cancel(); 
       AlertDialog.Builder builder = new AlertDialog.Builder(
            TimedAlert.this); 
       builder.setTitle("Session Time Out"); 
       builder.setMessage("00:15"); 
       builder.setPositiveButton("Resume", new DialogInterface.OnClickListener() { 
        public void onClick(DialogInterface dialog2,int iwhich) 
        { 
         Intent in = new Intent(TimedAlert.this,FirstActivity.class); 
         //in case there are many events ..the intent should be passed to the last activity on clicking resume 
         in.setAction(Intent.ACTION_MAIN); 
         in.addCategory(Intent.CATEGORY_LAUNCHER); 
         onUserInteraction(); 
        } 
       }); 

       builder.setNegativeButton ("No", new DialogInterface.OnClickListener() { 
        public void onClick(DialogInterface dialog2,int iwhich) 
        { 
         timer2.cancel(); 
         timer1.start(); 
        } 
       }); 

       alert = builder.create(); 
       alert.show(); 

       timer2 = new CountDownTimer(15000, 1000) 
       { 
        @Override 
        public void onTick(long millisUntilFinished) 
        { 
         Log.v(TAG, "timer2 ticking"); 
         alert.setMessage("Your Session will expire in 5 minutes . Timleft00:"+ (millisUntilFinished/1000)); 
         mCounter1TextField.setText("Seconds left: "+ formatTime (millisUntilFinished)); 
        } 

        //after 15 sec dismiss alert box 
        public void onFinish() { 
         Log.v(TAG, "timer2 finished"); 
         timer2.cancel(); 
         alert.dismiss(); 
        } 
       }.start(); 
      } 
     }.start(); 
    } 

    @Override 
    public void onBackPressed() { 
     Intent in = new Intent(TimedAlert.this, FirstActivity.class); 
     startActivity(in); 
    } 

    public String formatTime(long millis) { 
     String output = "00:00"; 
     long seconds = millis/1000; 
     long minutes = seconds/60; 
     seconds = seconds % 60; 
     minutes = minutes % 60; 
     String secondsD = String.valueOf(seconds); 
     String minutesD = String.valueOf(minutes); 

     if (seconds < 10) 
      secondsD = "0" + seconds; 
     if (minutes < 10) 
      minutesD = "0" + minutes; 
     output = minutesD + " : " + secondsD; 
     return output; 
    } 

    public void onUserInteraction() { 
     super.onUserInteraction(); 
     // Remove any previous callback 
     try { 
      Log.v(TAG, "user interacted"); 
      timer1.start(); 
      timer2.cancel(); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
    } 

    @Override 
    protected void onPause() { 
     // TODO Auto-generated method stub 
     super.onPause(); 
     Log.v(TAG, "paused"); 
     onUserInteraction(); 
    } 

    @Override 
    protected void onResume() { 
     // TODO Auto-generated method stub 
     super.onResume(); 

     Log.v(TAG, "resumed"); 
     onUserInteraction(); 
    } 

    private void handleIntent(Intent intent) { 
     timer1.start(); 
    } 

    @Override 
    protected void onStop() { 
     // TODO Auto-generated method stub 
     super.onStop(); 

     Log.v(TAG, "stopped"); 
     timer1.start(); 
    } 

    @Override 
    protected void onStart() { 
     // TODO Auto-generated method stub 
     super.onStart(); 
     Log.v(TAG, "started"); 
     timer1.start(); 
    } 
} 
+0

该代码非常非常杂乱,所以我害怕但我无法检查它。不过,我认为你已经使问题比需要更加复杂;比如只使用一个25秒的定时器,而不是有两个嵌套定时器,只需在类中调用一个方法,而不是对自己调用一个'Intent'。 –

+0

我真的很抱歉这个乱七八糟的代码..会试着让它理解..我有两个计时器,因为我想在第二个计时器中调用onTick方法来显示留在警报框中的时间..cud你好建议如果有更好的方法..笃üü提出一个更好的方法skeliton。 – user1420943

回答

1

OK,这里有几件事我注意到,可能会帮助你:

public void onClick(DialogInterface dialog2, int iwhich) { 
    Intent in = new Intent(TimedAlert.this, 
      FirstActivity.class); 
    in.setAction(Intent.ACTION_MAIN); 
    in.addCategory(Intent.CATEGORY_LAUNCHER); 

    onUserInteraction(); 
} 

你没有startActivity(in);设置所有参数后。

为什么onPause()onResume()调用onUserInteraction(),但onStart()onStop()不?

实际上,您应该选择仅使用onPause()onResume()或使用onStart()onStop()。此外,onPause()onStop()不应重新启动定时器?

进一步思考你所报告的问题,你说当你在第二次活动时,你有问题。 Check out the lifecycle of an Activity - 我怀疑可能发生的事情是您启动了一个新的活动实例。尝试设置清单,以便使用android:launch mode="singleTask"进行活动。