2013-04-18 122 views
0

我想爲我的應用程序創建一個新的調度器,每天在用戶定義的時間運行。下面是代碼。問題是,調度程序未在運行組建議time.Please如何做到這一點報警管理器不工作

private void setAlarm(String targetCal){ 
    Toast.makeText(MainActivity.this,"Alarm Set", Toast.LENGTH_LONG).show(); 
    String[] Time=targetCal.split(":"); 
    Calendar timeOff = Calendar.getInstance(); 
    Intent intent = new Intent(getBaseContext(), AlarmReceiver.class); 
    PendingIntent pendingIntent = PendingIntent.getBroadcast(getBaseContext(), RQS_1, intent, 0); 
    AlarmManager alarmManager = (AlarmManager)getSystemService(Context.ALARM_SERVICE); 

    //int days = Calendar.SUNDAY + (7 - timeOff.get(Calendar.DAY_OF_WEEK)); // how many days until Sunday 
    timeOff.set(Calendar.HOUR,Integer.valueOf(Time[0].trim())); 
    timeOff.set(Calendar.MINUTE,Integer.valueOf(Time[1].trim())); 
    alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, timeOff.getTimeInMillis(), alarmManager.INTERVAL_DAY , pendingIntent); 

} 
+0

timeOff.getTimeInMillis()會在當前時間告訴它。然後你問它在一天後的同一時間運行。你的目標是做什麼? – 2013-04-18 07:37:44

+0

@ShobhitPuri目標是在設定的時間運行調度器 – Supreet 2013-04-18 07:39:47

回答

2

試試這個代碼

Intent i = new Intent("in.servicealarmdemo.demoactivity"); 

/** Creating a Pending Intent */ 
PendingIntent operation = PendingIntent.getActivity(getBaseContext(), count+2, i, Intent.FLAG_ACTIVITY_NEW_TASK); 

/** Getting a reference to the System Service ALARM_SERVICE */ 
AlarmManager alarmManager = (AlarmManager) getBaseContext().getSystemService(ALARM_SERVICE); 
/** Creating a calendar object corresponding to the date and time set by the user */ 
GregorianCalendar calendar = new GregorianCalendar(pYear,pMonth,pDay,pHour,pMinute); 

/** Converting the date and time in to milliseconds elapsed since epoch */ 
long alarm_time = calendar.getTimeInMillis(); 

/** Setting an alarm, which invokes the operation at alart_time */ 
alarmManager.set(AlarmManager.RTC_WAKEUP , alarm_time , operation); 

/** Alert is set successfully */ 
Toast.makeText(getBaseContext(), "Alarm is set successfully",Toast.LENGTH_SHORT).show(); 

或者你可以簡單地嘗試本教程

http://wptrafficanalyzer.in/blog/setting-up-alarm-using-alarmmanager-and-waking-up-screen-and-unlocking-keypad-on-alarm-goes-off-in-android/

+0

這是測試代碼? – Supreet 2013-04-18 07:37:31

+0

是的,我曾嘗試過它在我的一個提醒應用程序 – Geethu 2013-04-18 08:16:26

0

通過設置變量可能會有問題。請嘗試查找當前時間和新值之間的差異,然後執行以下操作:

timeOff.add(Calender.HOUR,diff); 
+0

我只是從日曆中得到小時和分鐘 – Supreet 2013-04-18 07:57:14