2013-07-17 34 views
3

我正在設置此鬧鐘在X處關閉,當前時間爲Y,並且在時間發送鬧鐘Y沒有等到時間X關閉...可以任何人都會告訴我爲什麼不等到正確的時間才能離開?樣品的logcat:鬧鐘管理器設置重複沒有等到時間關閉

07-17 16:27:13.779: I/PROJECTCARUSO(13305): Reminder Set 
07-17 16:27:13.789: I/PROJECTCARUSO(13305): Setting Alarm 
07-17 16:27:13.789: I/PROJECTCARUSO(13305): ZONE_OFFSET: 15 
07-17 16:27:13.789: I/PROJECTCARUSO(13305): sinceMidnight: 77233802 
07-17 16:27:13.789: I/PROJECTCARUSO(13305): test now: 21:27:13 
07-17 16:27:13.789: I/PROJECTCARUSO(13305): NOW: 77233802 
07-17 16:27:13.789: I/PROJECTCARUSO(13305): test send: 16:28:0 
07-17 16:27:13.789: I/PROJECTCARUSO(13305): SEND: 59280000 
07-17 16:27:13.789: I/PROJECTCARUSO(13305): When it will kick off: 68446198 
07-17 16:27:13.979: I/PROJECTCARUSO(13455): onReceive 
07-17 16:27:13.989: I/PROJECTCARUSO(13455): Current time: 21:27:14 
07-17 16:27:13.989: I/PROJECTCARUSO(13455): Current Mills: 1374096434004 

類:

public class Alarm extends BroadcastReceiver 
{  
    SharedPreferences mPreferences; 
    Boolean reminder; 
    String time_selected; 

    @Override 
    public void onReceive(Context context, Intent intent) 
    { 
     mPreferences = PreferenceManager.getDefaultSharedPreferences(context); 
     reminder = mPreferences.getBoolean("frequency", false); 
     time_selected = mPreferences.getString("alarm_time", ""); 

     if (reminder) { 
      Log.i("PROJECTCARUSO","onReceive"); 
      PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE); 
      PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, ""); 
      wl.acquire(); 

      // Put here YOUR code. 
      String second =""+ (System.currentTimeMillis()/1000) % 60; 
      String minute =""+ (System.currentTimeMillis()/(1000 * 60)) % 60; 
      String hour = ""+ (System.currentTimeMillis()/(1000 * 60 * 60)) % 24; 

      String time=hour+":"+minute+":"+second; 


      Log.i("PROJECTCARUSO", "Current time: " + time); 

      Log.i("PROJECTCARUSO","Current Mills: " + System.currentTimeMillis()); 
      Toast.makeText(context, "Alarm !!!!!!!!!!", Toast.LENGTH_LONG).show(); // For example 

      wl.release(); 
     } else { 
      CancelAlarm(context); 
     } 
    } 

public void SetAlarm(Context context) 
{ 
    mPreferences = PreferenceManager.getDefaultSharedPreferences(context); 
    reminder = mPreferences.getBoolean("frequency", false); 
    time_selected = mPreferences.getString("alarm_time", ""); 

    if (reminder) { 
     Log.i("PROJECTCARUSO","Setting Alarm"); 

     AlarmManager am=(AlarmManager)context.getSystemService(Context.ALARM_SERVICE); 
     Intent i = new Intent(context, Alarm.class); 
     PendingIntent pi = PendingIntent.getBroadcast(context, 0, i, 0); 

     // time selected from settings information 
     String[] separated = time_selected.split(":"); 
     long timeSelectedMillis = (Long.parseLong(separated[0]) *3600000) + (Long.parseLong(separated[1]) *60000); 
     Log.i("PROJECTCARUSO", "ZONE_OFFSET: " + (Calendar.ZONE_OFFSET)); 

     Calendar rightNow = Calendar.getInstance(); 
     long sinceMidnight = (System.currentTimeMillis()) % (24 * 60 * 60 * 1000); 

     Log.i("PROJECTCARUSO", "sinceMidnight: " + sinceMidnight); 

     String second =""+ (sinceMidnight/1000) % 60; 
     String minute =""+ (sinceMidnight/(1000 * 60)) % 60; 
     String hour = ""+ (sinceMidnight/(1000 * 60 * 60)) % 24; 

     String time=hour+":"+minute+":"+second; 


     Log.i("PROJECTCARUSO", "test now: " + time); 

     Log.i("PROJECTCARUSO", "NOW: " + sinceMidnight); 

     second =""+ (timeSelectedMillis/1000) % 60; 
     minute =""+ (timeSelectedMillis/(1000 * 60)) % 60; 
     hour = ""+ (timeSelectedMillis/(1000 * 60 * 60)) % 24; 

     time=hour+":"+minute+":"+second; 

     Log.i("PROJECTCARUSO", "test send: " + time); 

     Log.i("PROJECTCARUSO", "SEND: " + timeSelectedMillis); 



     long triggerAtMillis = timeSelectedMillis - sinceMidnight; 
     if (triggerAtMillis < 0) { 
      triggerAtMillis = (86400000 - sinceMidnight) + timeSelectedMillis; 
     } 
     //System.currentTimeMillis() 
     Log.i("PROJECTCARUSO", "When it will kick off: " + triggerAtMillis); 
     am.setRepeating(AlarmManager.ELAPSED_REALTIME, triggerAtMillis, 86400000, pi); // Millisec * Second * Minute 
    } else { 
     CancelAlarm(context); 
    } 
} 

public void CancelAlarm(Context context) 
{ 
    mPreferences = PreferenceManager.getDefaultSharedPreferences(context); 
    reminder = mPreferences.getBoolean("frequency", false); 
    time_selected = mPreferences.getString("alarm_time", ""); 

    if (reminder) { 
     Log.i("PROJECTCARUSO","CancelAlarm"); 
     Intent intent = new Intent(context, Alarm.class); 
     PendingIntent sender = PendingIntent.getBroadcast(context, 0, intent, 0); 
     AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); 
     alarmManager.cancel(sender); 
    } 
} 
} 

回答

1

這是改變報警代碼。它目前的工作,並根據我的知識工作正常。

public void SetAlarm(Context context) 
{ 
    mPreferences = PreferenceManager.getDefaultSharedPreferences(context); 
    reminder = mPreferences.getBoolean("frequency", false); 
    time_selected = mPreferences.getString("alarm_time", ""); 


    Time now = new Time(); 
    now.setToNow(); 

    if (reminder) { 
     Log.i("PROJECTCARUSO","Setting Alarm"); 

     AlarmManager am=(AlarmManager)context.getSystemService(Context.ALARM_SERVICE); 
     Intent i = new Intent(context, Alarm.class); 
     PendingIntent pi = PendingIntent.getBroadcast(context, 0, i, 0); 

     // time selected from settings information 
     String[] separated = time_selected.split(":"); 


     //Create an offset from the current time in which the alarm will go off. 
     Calendar cal = Calendar.getInstance(); 
     cal.setTimeInMillis(System.currentTimeMillis()); 
     cal.set(Calendar.HOUR_OF_DAY, (int) Long.parseLong(separated[0])); 
     cal.set(Calendar.MINUTE, (int) Long.parseLong(separated[1])); 

     Log.i("PROJECTCARUSO", "Time set to go off: " + cal.getTimeInMillis()); 
     am.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), 86400000, pi); // Millisec * Second * Minute 
    } else { 
     CancelAlarm(context); 
    } 
} 
1

只提「triggerAtMillis」的說法的概念:

你必須添加要等待到當前時間的時間量。

例如,下面的行第一觸發接收器類在十秒鐘後,並不斷重複它每20秒:

am.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + 10000, 20000, pi); 
相關問題