2015-05-07 69 views
0

我想在後臺運行一些任務而不告訴用戶將一些數據發送到服務器。使用AlarmManager運行頻繁的任務

要求是,每隔15分鐘後我必須發送一些數據。所以在每15分鐘後需要觸發警報。

我正在使用AlarmManager,但沒有實現。

Calendar cal = Calendar.getInstance(); 
cal.set(Calendar.HOUR_OF_DAY,5); 
cal.set(Calendar.MINUTE,10);// 
cal.set(Calendar.SECOND, 0); 
Intent intent = new Intent(this, TaskReceiver.class); 
pintent = PendingIntent.getService(this, 0, intent, 0); 
alarm = (AlarmManager) getSystemService(Context.ALARM_SERVICE); 
long repeatingTime = 2 * 60 * 1000; 

alarm.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(),repeatingTime, pintent); 

回答

0

完成使用此:

public static void registerAlarm(Context context) { 
    Intent i = new Intent(context, TaskReceiver.class); 

    PendingIntent sender = PendingIntent.getBroadcast(context, 147, i, 0); 

    // We want the alarm to go off 3 seconds from now. 
    long firstTime = SystemClock.elapsedRealtime(); 
    firstTime += 3 * 1000;//start 3 seconds after first register. 

    // Schedule the alarm! 
    AlarmManager am = (AlarmManager) context.getSystemService(ALARM_SERVICE); 
    am.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, firstTime, 
      600000, sender);//10min interval 
}