2010-06-16 7 views
22

Tôi đã tìm kiếm rất nhiều địa điểm nhưng không thể tìm thấy giải thích tuần tự rõ ràng về cách bắt đầu dịch vụ (hoặc nếu không thể thực hiện hoạt động) tại một thời điểm cụ thể hàng ngày bằng AlarmManager ??Sử dụng Alarmmanager để bắt đầu dịch vụ tại thời điểm cụ thể

Tôi muốn đăng ký một số báo thức như vậy và kích hoạt chúng sẽ dẫn đến một dịch vụ được bắt đầu. Tôi sẽ có một đoạn mã nhỏ trong dịch vụ mà sau đó có thể thực hiện và tôi có thể hoàn thành dịch vụ cho tốt ....

Calendar cal = Calendar.getInstance(); 
Calendar cur_cal = Calendar.getInstance(); 
cur_cal.setTimeInMillis(System.currentTimeMillis()); 
Date date = new Date(cur_cal.get(Calendar.YEAR), cur_cal.get(Calendar.MONTH), cur_cal.get(Calendar.DATE), 16, 45); 
cal.setTime(date); 
Intent intent = new Intent(ProfileList.this, ActivateOnTime.class); 
intent.putExtra("profile_id", 2); 
PendingIntent pintent = PendingIntent.getService(ProfileList.this, 0, intent, 0); 
AlarmManager alarm = (AlarmManager)getSystemService(Context.ALARM_SERVICE); 
alarm.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), pintent); 
System.out.println("The alarm set!!"); 

tôi đã cố gắng mã này để kích hoạt báo động tại 4,45 ... nhưng nó không bắn dịch vụ ... tôi có phải giữ cho quá trình chạy không ?? Tôi có làm gì sai không ???

hơn Một điều, dịch vụ của tôi được hoàn toàn thực hiện trong trường hợp tôi sử dụng đoạn mã sau:

long firstTime = SystemClock.elapsedRealtime(); 
alarm.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, firstTime, 30*1000,pintent); 
+0

có thể trùng lặp của [Cách đặt báo thức để kích hoạt đúng vào thời gian cố định?] (Http://stackoverflow.com/questions/2992882/how-to-set-an-alarm-to-fire- đúng-tại-cố định-thời gian) – Pentium10

+1

tôi đã kiểm tra câu hỏi ur "Pentium10" và tôi spose tương tự của nó, nhưng bạn đã tìm thấy giải pháp cho cùng? – JaVadid

Trả lời

30

HI bạn bè, Sau rất nhiều nghiên cứu và có sự tham khảo từ "Pentium10" 's câu hỏi trên cùng chủ đề tôi quản lý để làm cho nó hoạt động. Mặc dù tôi vẫn không thể hiểu tại sao khái niệm "date" và đối tượng Lịch (không phải GregorianCalendar) mà tôi đã đề cập trong câu hỏi không hoạt động chính xác.

Calendar cur_cal = new GregorianCalendar(); 
cur_cal.setTimeInMillis(System.currentTimeMillis());//set the current time and date for this calendar 

Calendar cal = new GregorianCalendar(); 
cal.add(Calendar.DAY_OF_YEAR, cur_cal.get(Calendar.DAY_OF_YEAR)); 
cal.set(Calendar.HOUR_OF_DAY, 18); 
cal.set(Calendar.MINUTE, 32); 
cal.set(Calendar.SECOND, cur_cal.get(Calendar.SECOND)); 
cal.set(Calendar.MILLISECOND, cur_cal.get(Calendar.MILLISECOND)); 
cal.set(Calendar.DATE, cur_cal.get(Calendar.DATE)); 
cal.set(Calendar.MONTH, cur_cal.get(Calendar.MONTH)); 
Intent intent = new Intent(ProfileList.this, IntentBroadcastedReceiver.class); 
PendingIntent pintent = PendingIntent.getService(ProfileList.this, 0, intent, 0); 
AlarmManager alarm = (AlarmManager)getSystemService(Context.ALARM_SERVICE); 
alarm.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), 30*1000, pintent); 
+0

Trong báo thức mã này bắt đầu lúc 18:32 cùng ngày, phải không? – Sver

+0

Hãy thử sử dụng đối tượng Thời gian, và theo tài liệu, báo thức này sẽ bắt đầu lúc 18:32 UTC – gregm

+0

@JaVadid: Tôi có cùng một Câu hỏi, hãy giúp tôi làm điều đó. Dưới đây là: http://stackoverflow.com/questions/10039593/android-alarm-to-be-play-every-30-minutes-and-it-start-from-1230/10039699#10039699 –

7
//Create alarm manager 
AlarmManager alarmMgr0 = (AlarmManager)getSystemService(Context.ALARM_SERVICE); 

//Create pending intent & register it to your alarm notifier class 
Intent intent0 = new Intent(this, AlarmReciever.class); 
PendingIntent pendingIntent0 = PendingIntent.getBroadcast(this, 0, intent0, 0); 

//set timer you want alarm to work (here I have set it to 7.20pm) 
Intent intent0 = new Intent(this, OldEntryRemover.class); 
Calendar timeOff9 = Calendar.getInstance(); 
timeOff9.set(Calendar.HOUR_OF_DAY, 19); 
timeOff9.set(Calendar.MINUTE, 20); 
timeOff9.set(Calendar.SECOND, 0); 

//set that timer as a RTC Wakeup to alarm manager object 
alarmMgr0.set(AlarmManager.RTC_WAKEUP, timeOff0.getTimeInMillis(), pendingIntent0); 

Sau đó, trong lớp AlarmReciever của bạn mà là một broadcastReciever, theo phương pháp onRecieve đặt logic của bạn. Điều này sẽ chăm sóc của những gì bao giờ logic bạn muốn xử lý khi thời gian đến 7.20 pm.

Nếu bạn cần đặt nhiều báo thức, hãy tạo một thể hiện Lịch khác & đặt giá trị thời gian phù hợp. Bạn cũng cần phải tạo một thể hiện khác cho pendingIntent nếu không thì các bộ đếm thời gian sẽ chồng lên nhau. Sau đó đặt nó thành cùng một AlarmManager với bộ đếm thời gian mới & pendingIntent.

+3

Logic của việc sử dụng bộ thu phát sóng ở đây là gì? Ý tôi là nếu bạn muốn làm điều gì đó vào một thời điểm nào đó (ví dụ như nhận được nhiệt độ), việc chạy một dịch vụ sẽ thực hiện công việc, đúng không? – MSaudi

+0

Thật vậy, một dịch vụ sẽ là một lựa chọn tốt hơn nếu "logic" nhiều hơn là hiển thị một bánh mì nướng. Ngoài ra, việc đặt tên biến có thể được cải thiện. – Entalpi

1

tôi đã cố gắng rất nhiều để bắt đầu dịch vụ trên Time Vì vậy, tôi đã có một giải pháp như

  1. Tính chênh lệch giữa thời điểm hiện tại và chọn thời gian kể từ ngày chọn "Return dài timeMileSec = Mili giây "Sự khác biệt

  2. sau khi điều này tạo một trình xử lý bên trong nó và Ngủ nếu" Mili giây "giây

    new Handler().postDelayed(new Runnable() { 
        @Override 
        public void run() { 
         CreateService(); 
         getActivity().startService(intentServiceObj); 
        } 
    }, timeMileSec); 
    
    
    // Below is the service Methods. 
    private void CreateService() { 
        Calendar cal = Calendar.getInstance(); 
        cal.setTimeInMillis(System.currentTimeMillis()); 
    
        cal.add(Calendar.DAY_OF_YEAR, year); 
        cal.set(Calendar.HOUR_OF_DAY, hourScreen); 
        cal.set(Calendar.MINUTE, minuteScreen); 
    
        // cal.setTimeInMillis(timeselectedmillisecond); 
    
        Intent intent = new Intent(getActivity(), 
         ServiceDailyLocationUpdate.class); 
        pintent = PendingIntent.getService(getActivity(), 0, intent, 0); 
    
        alarm = (AlarmManager) getActivity().getSystemService(
         Context.ALARM_SERVICE); 
    
        alarm.setRepeating(AlarmManager.RTC_WAKEUP, cal.HOUR, 86000 * 1000, 
         pintent); 
    
    } 
    
    // Return Differnce between two date 
    private long calculateDateDiffSecond(String firstDate, String secondDate) { 
    
        long numberOfDays = 0; 
    
        String dateStart = firstDate; 
        String dateStop = secondDate; 
    
        Date d1 = null; 
        Date d2 = null; 
    
        try { 
         d1 = sdfTime.parse(dateStart); 
         d2 = sdfTime.parse(dateStop); 
    
         // in milliseconds 
         long diff = d2.getTime() - d1.getTime(); 
    
         long diffSeconds = diff/1000 % 60; 
         long diffMinutes = diff/(60 * 1000) % 60; 
         long diffHours = diff/(60 * 60 * 1000) % 24; 
         long diffDays = diff/(24 * 60 * 60 * 1000); 
    
         System.out.print(diffDays + " days, "); 
         System.out.print("Hours::" + diffHours + " hours, "); 
         System.out.print("HoursMinute::" + diffMinutes + " minutes, "); 
         System.out.print(diffSeconds + " seconds."); 
         numberOfDays = diffDays; 
         numberOfDays = diff; 
        } catch (Exception e) { 
         e.printStackTrace(); 
        } 
        return numberOfDays; 
    
    } 
    
4

Bạn có thể đọc tài liệu từ https://developer.android.com/training/scheduling/alarms.html

private AlarmManager alarmMgr; 
private PendingIntent alarmIntent; 

alarmMgr = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE); 
Intent intent = new Intent(context, AlarmReceiver.class); 
alarmIntent = PendingIntent.getBroadcast(context, 0, intent, 0); 

// Cài báo thức để bắt đầu vào lúc 8:30 am

Calendar calendar = Calendar.getInstance(); 
calendar.setTimeInMillis(System.currentTimeMillis()); 
calendar.set(Calendar.HOUR_OF_DAY, 8); 
calendar.set(Calendar.MINUTE, 30); 

// setRepeating() cho phép bạn chỉ định một khoảng thời gian tùy chỉnh chính xác - trong trường hợp này là // 20 phút.

alarmMgr.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 
     1000 * 60 * 20, alarmIntent); 
1

mã không hoạt động đối với tôi.

và mã này hoạt động đối với tôi.

moth nghị định 1. và giờ 0-11.

int day = ff.getGregorianDay() ; 
int month = ff.getGregorianMonth() ; 
int year = ff.getGregorianYear(); 
int hour = TimePicker1.getCurrentHour(); 
int minute = TimePicker1.getCurrentMinute(); 



Calendar cal_alarm = Calendar.getInstance(); 
cal_alarm.setTimeInMillis(System.currentTimeMillis()); 
cal_alarm.set(Calendar.YEAR, year); 
cal_alarm.set(Calendar.MONTH, month-1); 
cal_alarm.set(Calendar.DAY_OF_MONTH, day); 


cal_alarm.set(Calendar.AM_PM, Calendar.AM); 
cal_alarm.set(Calendar.HOUR, hour); 

if(hour >= 12){ 
    cal_alarm.set(Calendar.AM_PM, Calendar.PM); 
    cal_alarm.set(Calendar.HOUR, hour-12); 
} 
cal_alarm.set(Calendar.MINUTE, minute); 
cal_alarm.set(Calendar.SECOND, 0); 

Intent myIntent = new Intent(YadavariNewActivity.this, Alarm_Sag.class); 
PendingIntent pendingIntent = PendingIntent.getBroadcast(YadavariNewActivity.this, 0, myIntent,0); 

AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE); 
alarmManager.set(AlarmManager.RTC_WAKEUP, cal_alarm.getTimeInMillis(), pendingIntent); 
+0

ff là gì? –

+0

là lớp tùy chỉnh. có năm tháng và ngày. @ umar-ata –

+0

tại sao bạn không thêm nó vào câu trả lời của bạn –

0

Mã sau đây sẽ hoạt động tốt và khởi động dịch vụ @ 7:40 PM mỗi ngày. Ngoài ra, nếu thiết bị tắt thì tất cả báo thức của bạn sẽ bị hủy.

Đảm bảo thiết lập tất cả các báo thức sau khi BOOT hoàn tất.

Intent slIntent = new Intent(this, x.class); 

Calendar calendar = Calendar.getInstance(); 
calendar.setTimeInMillis(System.currentTimeMillis()); 
calendar.set(Calendar.HOUR_OF_DAY, 19); 
calendar.set(Calendar.MINUTE, 40); 
calendar.set(Calendar.SECOND, 0); 

PendingIntent slPendingIntent = PendingIntent.getService(this, 1, slIntent, PendingIntent.FLAG_ONE_SHOT); 

AlarmManager alarmManager=(AlarmManager) getSystemService(Context.ALARM_SERVICE); 

alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 
          AlarmManager.INTERVAL_DAY, slPendingIntent);