2010-12-11 32 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);