2014-02-18 58 views
0

每個人。確切時間運行PendingIntent

我有這段代碼片段。

pending_intent = PendingIntent.getActivity(context, 0, intent, 0); 
alarm = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE); 
alarm.set(AlarmManager.RTC, System.currentTimeMillis() + runDelay, pending_intent); 

我想擺脫System.currentTimeMillis的()+ runDelay的,並通過剛剛runDelay變量intofunction,沒有人知道我應該怎麼辦呢?

P.S.我需要的代碼是API8

非常感謝。

+2

爲什麼?如果這樣做,爲什麼要花費時間和精力?如果你真的很討厭它,寫一個函數來封裝那些邏輯。 –

+0

我想從當前時間獨立。 – theroom101

+0

「獨立於當前時間」是什麼意思?這並沒有太多意義。由Independing提供的 –

回答

1

使用TimePickerDialog從用戶獲得runDelay這樣做

類變量:

private TimePickerDialog time_picker; 
private Calendar targetCal; 

類構造函數

public YOurClassName() { 

     final Calendar c = Calendar.getInstance(); 
     mYear = c.get(Calendar.YEAR); 
     mMonth = c.get(Calendar.MONTH); 
     mDay = c.get(Calendar.DAY_OF_MONTH); 
     mHour = c.get(Calendar.HOUR_OF_DAY); 
     mMinute = c.get(Calendar.MINUTE); 

    } 

裏面的onCreate()或onActivityCreated( ):

time_picker = new TimePickerDialog(getActivity(), mTimeSetListener, 
      mHour, mMinute, false); 

裏面的類:

private TimePickerDialog.OnTimeSetListener mTimeSetListener = new TimePickerDialog.OnTimeSetListener() { 
     // the callback received when the user "sets" the TimePickerDialog in 
     // the dialog 
     public void onTimeSet(TimePicker view, int hourOfDay, int min) { 

      hour = hourOfDay; 
      minute = min; 

      Calendar calNow = Calendar.getInstance(); 
      Calendar calSet = (Calendar) calNow.clone(); 

      calSet.set(Calendar.HOUR_OF_DAY, hour); 
      calSet.set(Calendar.MINUTE, minute); 
      calSet.set(Calendar.SECOND, 0); 
      calSet.set(Calendar.MILLISECOND, 0); 

      if (calSet.compareTo(calNow) <= 0) { 
      // Today Set time passed, count to tomorrow 
      calSet.add(Calendar.DATE, 1); 
      } 
      targetCal = calSet; 
      } 
    }; 

在代碼中使用的targetCal這樣

alarm.set(AlarmManager.RTC, targetCal.getTimeInMillis(), pending_intent); 

我希望這會幫助你

+0

謝謝你,但這不正是我想要的,就像我在之前的評論中所說的那樣,我需要獨立於當前時間。我認爲應該有一些機制,這將在 – theroom101

+0

後自動觸發如果你沒有得到系統的當前時間,那麼你將如何在指定的時間設置鬧鐘......我沒有得到你想要的東西@ Jabberwocky –

+0

我問「有沒有其他方法可以啓動掛起的意圖,而不使用System.currentTime()?」 – theroom101