警报对话框不会出现

时间:2012-05-30 03:22:03

标签: android alert timed

我有一个应用程序,当用户启动应用程序时,计时器启动。 10秒后,弹出AlertDialog只说15秒铰孔并显示计时器,14秒后它消失。这在应用程序的第一个活动时工作正常。如果用户从第一个Activty - >经过TimedNotify Activity计时器在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();
    }
}

1 个答案:

答案 0 :(得分:1)

好的,我注意到以下几点可能对你有帮助:

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"进行活动。