2012-12-08 38 views
0

我正在做一個鬧鐘應用程序,我正在使用一種方法在特定的時間開始一個活動, ,我在我的應用程序中有一個選項,名爲enable \ disable,我不想讓鬧鐘打開活動(當我檢查禁用框時,播放聲音並獲得我選擇的圖像! 所以我正在尋找一種方法來在開始之前關閉活動&不要在屏幕上進行任何顯示以顯示活動正在開始和結束! 我使用在特定的時間如何在開始之前強制關閉活動?

 Intent myIntent = new Intent(SharedPrefs.this,MainPhoto.class); 
    pendingIntent = PendingIntent.getActivity(MainPhoto.this, 0, myIntent, 0); 
    AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE); 
    Calendar calendar = Calendar.getInstance(); 
    calendar.setTimeInMillis(System.currentTimeMillis()+10000); 
    alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent); 

在任何幫助打開特定活動這種方法嗎?

+0

您可能希望以更好的方式來處理你的報警:http://blog.blundell-apps.com/notification-for-a-user-chosen-time/ – Blundell

回答

0

取消報警的可能是最好的解決方案在這裏。

可能有一些罕見的情況,直到活動已被調用後才知道。在這種情況下,只需要調用finish()之前setContentView

@Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     if (/* Check here if valid */) { 
      finish(); 
     } else { 
      setContentView(R.layout.some_layout); 
      /* Actually do your initialization. */ 
     } 
    } 
+0

dose finish();不要在屏幕上顯示活動打開並快速關閉? 因爲我不希望用戶感受到這一點! –

+0

@MostafaAbdElRazek活動的可見生命週期開始和結束於'onStart()'和'onStop()'...之前和之後的任何活動對用戶不可見。如果你在'onCreate()'完成()',然後''onStart()''onDestroy()'(即'onStart()','onResume()'等)的所有其他生命週期方法不會叫做。所以不,它不會被顯示。 –

1

cancelling the alarm怎麼樣,或者使用將被打開的服務而不是活動來檢查是否需要打開活動?

0

我會建議讓複選框作爲共享首選項,然後在啓動新活動的意圖之前,檢查它是否被禁用或不是......如果您確實想要啓動新活動,請選中複選框是否被選中,如果是,則調用finish();

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    if (Checkbox.checked()) 
     finish(); 
} 

希望這有助於:)

+0

非常感謝:) –