2010-12-11 33 views
11

我的应用程序是一个闹钟类型的应用程序的计时器。 当计时器到期时,如果用户已离开,但该应用程序仍在运行,我希望将应用程序的主要活动重新放入视图中。带回应用程序

我告诉我哪里想放置必要的代码:

public class MyCount extends CountDownTimer { 
     public MyCount(long millisInFuture, long countDownInterval) { 
      super(millisInFuture, countDownInterval); 
     } 

     public void onFinish() { 

     /** code here to bring the app back to the front/top of stack, in its current state */ 


     timeDisplay.setText("Expired.!"); 
     ReminderFlashingScreen.setVisibility(View.GONE); 
     statustab.setBackgroundResource(R.drawable.redtab); 
     seeker.setProgress(0); 
     reset.setBackgroundResource(R.drawable.resetbtnoff); 
     playExpiredAlarm(); 
     alarmAnimation.start(); 
     flasher.setVisibility(View.VISIBLE); 
     StopAlarmButtonAnimation.start(); 
     StopAlarmButton.setVisibility(View.VISIBLE); 
     expired = true; 
     } 

     public void onTick(long millisUntilFinished) { 
      remindertimepref = prefs.getString("reminderTime", "<unset>"); 
      remindtime = Integer.parseInt(remindertimepref.trim()); 
      timeDisplay.setText(formatTime(millisUntilFinished)); 
      seeker.setProgress((int) (millisUntilFinished/1000/60) + 1); 
      if (used == true){ 
       reminder_time = ((int) (millisUntilFinished/1000)); 
       if (reminder_time == remindtime){ 
        reminderalarm(); 
        used = false; 
        } 
       } 

      } 
    } 
+1

?我的观点是,我想你想把你的计时器变成一项服务。然后,您可以发送您的应用程序在定时器完成时正在监听的广播。更简单,更清洁! – codinguser 2010-12-11 11:16:59

+1

我需要阅读的东西..新的android,我的第一个想法是:如果应用程序被杀害,什么保持计数..?我知道如何保存当前状态,但没有任何用处,因为定时器会暂停。你的解决方案听起来像我应该探索的路线,任何我应该寻找/寻找的指针,赞赏。 – 11Monkeys 2010-12-11 11:27:38

+1

考虑使用android AlarmManager http://developer.android.com/intl/de/reference/android/app/AlarmManager.html,而不是跟踪自己的时间,至少在您收到onPause呼叫后使用它。这将为用户节省大量的电池和CPU使用量。 – Janusz 2010-12-11 12:56:07

回答

0

http://developer.android.com/guide/appendix/faq/framework.html#4

我如何检查是否在活动开始前已经在运行?

如果启动一个新活动(如果该活动未处于运行状态,或者如果活动堆栈已在后台运行),则启动一个新活动的一般机制是在startActivity()调用中使用NEW_TASK_LAUNCH标志。

+0

它应该如上所述发生,但它仍然不能正常工作。我开始我的应用程序,然后点击主页按钮,然后应用程序去背景,但仍然运行,在2-3分钟后,我用NEW_TASK_LAUNCH标志触发了一个意图,但它从主入口点再次启动应用程序,而不是从它离开的位置 – Zoombie 2012-08-28 05:44:26

3

对我来说是这样的:

Intent intent = new Intent(Application.getContext(), AbstractFragmentActivity.instance.getClass()); 
intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT); 
intent.addFlags(Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED); 
intent.addFlags(Intent.FLAG_ACTIVITY_PREVIOUS_IS_TOP); 
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
Application.getContext().startActivity(intent); 
0

你应该把你的计时器在Service,你放心,Android将不会杀了你的应用程序,同时它在后台的方式。 (你可以把你的整个MyCount类放入一个服务中)。

从服务,您可以随时把你的应用程序(活动)的面前:如果用户浏览什么远的东西更多的内存密集型和系统决定杀死你的应用程序

Intent intent = new Intent(getBaseContext(), TimerActivity.class); 
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
intent.addFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT); 
startActivity(intent);