2

それが言う方法をチェックするSystem.currentTimeMilllis()

This method shouldn't be used for measuring timeouts or other 
elapsed time measurements, as changing the system time can affect the results.

システムがリセットされると、 SystemClock.elapsedRealTime() のような他のメソッドがリセットされます

ユーザーがシステム時刻を変更するかどうかに関係なく、特定のアクションを 2 日に 1 回実行するために時間を測定したい場合、どうすれば測定できますか?

4

3 に答える 3

1

に関してはMeasure long time intervals-あなたは秒のカウンターを更新するタイマーを実行することができます、それはシステム時間に依存しません

于 2013-02-12T12:54:43.693 に答える
1

これを試して:

private long difference ;

//This should be saved from when 2 days is to be checked
SharedPreferences myPrefs = context.getSharedPreferences("myPrefs",MODE_WORLD_READABLE);
        syncdate = myPrefs.getLong("difference", System.currentTimeMillis());

    String olddate = changeFormat(syncdate);
    String newdate = changeFormat(System.currentTimeMillis());//This is the new date
    difference = getDate(olddate, newdate);


        public static String changeFormat(long date){
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");

            Date resultdate = new Date(date);
            String format = sdf.format(resultdate);
            return format;
        }

        public static long getDate(String firstdate,String SecondDate)
        {
            Calendar calendar1 = Calendar.getInstance();
            Calendar calendar2 = Calendar.getInstance();
            String arr[] =firstdate.split("/");
            String arr1[] = SecondDate.split("/");
            int sty =Integer.parseInt(arr[0]);
            int stm = Integer.parseInt(arr[1]);
            int std = Integer.parseInt(arr[2]);
            int sty1 = Integer.parseInt(arr1[0]);
            int stm1 = Integer.parseInt(arr1[1]);
            int std1 = Integer.parseInt(arr1[2]);

            calendar1.set(sty, stm, std);
            calendar2.set(sty1, stm1, std1);
            long milliseconds1 = calendar1.getTimeInMillis();
            long milliseconds2 = calendar2.getTimeInMillis();
            long diff = milliseconds2 - milliseconds1;
            long diffDays = diff / (24 * 60 * 60 * 1000);

            return diffDays;

        }
于 2013-02-12T12:55:59.727 に答える
1

メソッドのようなものを使用してAlarmManager.setInexactRepeating(int, long, long, android.app.PendingIntent)、定期的にアクションを起動する必要があります

于 2013-02-12T12:48:48.937 に答える