2017-02-15 77 views
0

我正嘗試在onStartCommand()中將待處理意向傳遞給服務。但一旦設定了警報,它就會不斷觸發。我在凌晨4點設置兩個鬧鐘,下午8點設置一個鬧鐘。我希望實現這樣的邏輯,即當凌晨4點發出警報意圖時,應設置8點警報,並在8點意圖警報發出後,設置4點警報。但它出乎意料地工作。當下午8點的意圖被傳送時,然後在4秒內傳遞上午4點的意圖,該意圖再次設置爲下午8點的警報,並且該週期重複。 我在哪裏犯錯?請指導我。儘管時間不同,但報警管理器會不斷觸發

public class Alarmclass extends Service { 
@Nullable 
@Override 
public IBinder onBind(Intent intent) { 
    return null; 
} 

private String Stat; 

private boolean GetTypeofAlarm() { 
    Log.e("Getting type", "Getting alarm types"); 
    boolean alarmtype = false; 
    int hours; 
    Calendar c = Calendar.getInstance(); 
    hours = c.get(Calendar.HOUR_OF_DAY); 
    if (hours <= 5) { 
     alarmtype = false; 
    } else if (hours >= 20) { 
     alarmtype = false; 
    } else if (hours >= 5 && hours <= 20) 
     alarmtype = true; 
    return alarmtype; 
} 


@Override 
public void onCreate() { 
    super.onCreate(); 

    if (!GetTypeofAlarm()) { 
     StartLocAlarm(); 
    }else{ 
     StopLocAlarm(); 
    } 
    } 

    @Override 
public int onStartCommand(Intent intent, int flags, int startId) { 

    if (intent.hasExtra("START")) {    

    if(intent.getStringExtra("START").equals("START_ALR")) { 
      StopLocAlarm(); 
     } 
    }else if(intent.hasExtra("STOP")){ 
     if(intent.getStringExtra("STOP").equals("STOP_ALR")) { 
     StartLocAlarm(); 
    } 
    } 

    Intent Actint = new Intent(this, Startup_Activity.class); 
    Actint.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK 
      | Intent.FLAG_ACTIVITY_CLEAR_TASK); 
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 2422, Actint, 0); 

    Notification notification = new NotificationCompat.Builder(this) 
      .setContentTitle("Admin") 
      .setSmallIcon(R.mipmap.ic_launcher) 
      .setContentIntent(pendingIntent) 
      .setContentText(Stat) 
      .setOngoing(true).build(); 
    startForeground(201, notification); 

    return START_STICKY; 
} 

private void StartLocAlarm(){ 

    AlarmManager alrmMgr; 
    PendingIntent alarmIntent; 

    Calendar c = Calendar.getInstance(); 
    c.setTimeInMillis(System.currentTimeMillis()); 
    c.set(Calendar.HOUR_OF_DAY, 4); 
    alrmMgr = (AlarmManager)this.getSystemService(Context.ALARM_SERVICE); 
    Intent intent = new Intent(this, Alarmclass.class); 
    intent.putExtra("START", "START_ALR"); 
    alarmIntent = PendingIntent.getService(this, 0, intent, 0); 


    alrmMgr.set(AlarmManager.RTC_WAKEUP,c.getTimeInMillis(),alarmIntent); 
    Log.e("In onstart","Setting alarm at 5 AM"); 
    Stat = "Alarm set for 5 AM"; 

} 

private void StopLocAlarm(){ 

    AlarmManager alarmMgr; 
    PendingIntent alarmIntent; 

    Calendar c = Calendar.getInstance(); 
    c.setTimeInMillis(System.currentTimeMillis()); 
    c.set(Calendar.HOUR_OF_DAY, 20); 
    alarmMgr = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE); 
    Intent intent2 = new Intent(this, Alarmclass.class); 
    intent2.putExtra("STOP", "STOP_ALR"); 
    alarmIntent = PendingIntent.getService(this, 1, intent2, 0); 

    alarmMgr.set(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(), alarmIntent); 
    Log.e("In onstart", "Setting alarm at 8 PM"); 
    Stat = "Alarm set for 8 PM"; 
} 


} 

回答

0

您正在設置凌晨4點鬧鐘,您現在是在同一天。凌晨4點應設置爲第二天.. 例如:今天是星期三,晚上8點鬧鐘被觸發。你應該在週四凌晨4點設定下一次鬧鈴。每天

添加到您的日曆(這支持了月底一樣,所以這就是所有你需要做的。

c.add(Calendar.DAY_OF_MONTH, 1); //Adding 1 day 
+0

它仍不斷循環。 –

+0

我不明白...做你想只有當你的用戶按下通知,然後設置下一個鬧鐘嗎? – Dus

+0

否我想要在沒有任何用戶交互的情況下自動設置鬧鐘 –