2013-03-06 88 views
0

我懇切需要您的幫助來捕獲日期操作。請注意,因爲您提到的所有時間戳都是表單系統時間,而不是來自互聯網時間,我不允許以任何方式使用互聯網時間。在30天內捕獲日期操作

本質上我有兩個時間戳(包括日期),一個是當前系統時間,第二個是從時間戳數據庫中獲取的日期,這是數據庫的下載時間戳。另外我的應用程序需要下載此文件每30天

因此,例如: TIMESTAMP從數據庫或最後下載日期:25/02/2012 CURRENT DATE:15/03/2012 一個下載日期將被於:27/03/2012。

我的挑戰是我想在這30天的窗口內停止/捕獲日期的操作,因此可以說用戶意識到應用程序將在2012年3月27日更新,他將進入他/她的日期和時間設置並且不停地倒帶以便不允許日期到達27/03/2012,例如將CURRENT DATE(即15/03/2012)倒帶到03/03/2012,意味着他「12天后返回到時間」 。

任何意見/建議?

任何援助感謝!

嗨級堆垛機,下面是我的嘗試創建計時器,但我有點不確定在哪裏放置支票,因爲你建議。我已經放置了僞代碼,我認爲我應該在計時器中運行檢查。

這裏是我的AlarmManagerActivity:

public class AlarmManagerActivity extends Activity { 

private AlarmManagerBroadcastReceiver alarm; 
@Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     alarm = new AlarmManagerBroadcastReceiver(); 
    } 

    @Override 
protected void onStart() { 
    super.onStart(); 
} 

public void startRepeatingTimer(View view) { 
    Context context = this.getApplicationContext(); 
    if(alarm != null){ 
     alarm.SetAlarm(context); 
    }else{ 
     Toast.makeText(context, "Alarm is null", Toast.LENGTH_SHORT).show(); 
    } 
} 
} 

這是我的廣播接收器:

public class AlarmManagerBroadcastReceiver extends BroadcastReceiver { 

final public static String ONE_TIME = "onetime"; 

@Override 
public void onReceive(Context context, Intent intent) { 
    PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE); 
    PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "AlarmManagerBroadcastReceiver"); 
    //Acquire the lock 
    wl.acquire(); 

    //You can do the processing here. 
    Bundle extras = intent.getExtras(); 
    StringBuilder msgStr = new StringBuilder(); 

    if(extras != null && extras.getBoolean(ONE_TIME, Boolean.FALSE)){ 
     //Make sure this intent has been sent by the one-time timer button. 
     msgStr.append("One time Timer : "); 
    } 
    Format formatter = new SimpleDateFormat("hh:mm:ss a"); 
    msgStr.append(formatter.format(new Date())); 

    Toast.makeText(context, msgStr, Toast.LENGTH_LONG).show(); 

    // SHOULD THE CODE TO CHECK SYSTEM TIME AGAINST THE CORRECT TIME BE PLACED HERE ?? 
    // CURRENTLY I'M DISPLAYING A TOAST 
    // THANKS FOR ANY HELP 

    //Release the lock 
    wl.release(); 
} 

public void SetAlarm(Context context) 
{ 
    AlarmManager am=(AlarmManager)context.getSystemService(Context.ALARM_SERVICE); 
    Intent intent = new Intent(context, AlarmManagerBroadcastReceiver.class); 
    intent.putExtra(ONE_TIME, Boolean.FALSE); 
    PendingIntent pi = PendingIntent.getBroadcast(context, 0, intent, 0); 
    //After after EVERY HR 
    am.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), 1000 * 30 , pi); 
} 

}

謝謝!!!!

回答

0

您可能希望通過一個BroadcastReceiver監測android.intent.action.DATE_CHANGEDandroid.intent.action.TIME_SET,但已經跟他們玩了一下週圍後,我不會依靠他們 - 如果您的應用程序移動到SD卡,也不會收到任何例如,在啓動之後立即啓動事件直到安裝SD卡。

運行計時器(例如每小時)通過AlarmManager並具有IntentService對陣當前系統時間的預期時間會比較安全,特別是如果你只需要知道是否它發生了。不過,我不會喚醒設備,因爲所有您需要知道的是在設備未睡眠時是否將時間設置爲過去。

如果您發現系統時間大於預期的安全時間。

只是我自發的想法。

+0

Hi Class Stacker,感謝您的快速回復。當我研究每次運行應用程序時提出的存儲內容,然後檢查每個存儲運行時間與當前系統時間之間的差是否爲零,因此如果CURRENT_SYSTEM_TIME - EACH_RUN_TIME_STORED_IN_DB等於零,則用戶嘗試操縱CURRENT_SYSTEM_TIME。我的問題是,如果用戶在同一天多次運行該應用程序,CURRENT_SYSTEM_TIME和EACH_RUN_TIME_STORED_IN_DB之間的差異將爲零,這意味着它看起來像用戶嘗試操縱,當這不是如此 – TokTok123 2013-03-06 13:53:20

+0

不知道我理解你。首先,請原諒我不夠清楚。我會讓IntentService始終運行,只是在應用程序運行的時候才運行。我忘了解釋如何開始它。但無論如何。 IntentService將被觸發,例如,每小時一次,它將訪問最後看到的時間戳,並查看系統時間的增量。如果超過一個小時(甚至包括負面結果),則系統時間已被設置爲過去的某個時間點。沒有? – 2013-03-06 14:05:19

+0

Hi Class Stacker,我已經調整了我的消息並創建了計時器,但對計時器中我應該放置檢查計算當前時間戳和上次運行時間戳之間的差異感到困惑 – TokTok123 2013-03-06 15:59:01