2014-09-27 48 views
2

我在Android中遇到了鬧鐘管理器的問題。鬧鐘管理器無法設置重複

裏面的onCreate方法:

 notificationCount = notificationCount + 1; 
     AlarmManager mgr = (AlarmManager) context 
       .getSystemService(Context.ALARM_SERVICE); 
     Intent notificationIntent = new Intent(context, 
       ReminderAlarm.class); 
notificationIntent.putExtra("NotifyCount", notificationCount); 
     PendingIntent pi = PendingIntent.getBroadcast(context, 
       notificationCount, notificationIntent, 
       PendingIntent.FLAG_UPDATE_CURRENT); 
     mgr.setInexactRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 
       60000+System.currentTimeMillis(), pi); 

     ComponentName receiver = new ComponentName(context, BootReceiver.class); 
     PackageManager pm = context.getPackageManager(); 

     pm.setComponentEnabledSetting(receiver, 
       PackageManager.COMPONENT_ENABLED_STATE_ENABLED, 
       PackageManager.DONT_KILL_APP); 

提醒報警類別:

public class ReminderAlarm extends BroadcastReceiver { 
private NotificationManager mNotificationManager; 
private Notification notification; 

@Override 
public void onReceive(Context context, Intent intent) { 
    mNotificationManager = (NotificationManager) context 
      .getSystemService(Context.NOTIFICATION_SERVICE); 
    PendingIntent contentIntent = PendingIntent 
      .getActivity(context, 0, new Intent(), 0); 
    notification = new Notification(
      R.drawable.ic_launcher, "Notification", 
      System.currentTimeMillis()); 
    notification 
      .setLatestEventInfo(context, "AlarmActivated", "Hello", contentIntent); 
    mNotificationManager.notify(
      Integer.parseInt(intent.getExtras() 
        .get("NotifyCount").toString()), 
      notification); }} 

在開機接收器類:

public void onReceive(Context context, Intent i) { 
    scheduleAlarms(context); 
} 

static void scheduleAlarms(Context context) { 
    Calendar calendar = Calendar.getInstance(); 
    calendar.setTimeInMillis(System.currentTimeMillis()); 
    calendar.set(Calendar.HOUR_OF_DAY, 0); 
    calendar.set(Calendar.MINUTE, 1); 
    AlarmManager mgr = (AlarmManager) context 
      .getSystemService(Context.ALARM_SERVICE); 
    Intent notificationIntent = new Intent(context, ReminderAlarm.class); 
    PendingIntent pi = PendingIntent.getService(context, 0, 
      notificationIntent, 0); 

    mgr.setInexactRepeating(AlarmManager.RTC_WAKEUP, 
      calendar.getTimeInMillis(), 
      60000+System.currentTimeMillis(), 
       pi); 
} 

在我的清單文件:

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" /> 
<receiver android:name=".ReminderAlarm" > 
    </receiver> 
    <receiver 
     android:name=".BootReceiver" 
     android:enabled="false" > 
     <intent-filter> 
      <action android:name="android.intent.action.BOOT_COMPLETED" > 
      </action> 
     </intent-filter> 
    </receiver> 

我想設置鬧鐘來重複每一分鐘並提示用戶通知。但它可以這樣工作:當我啓動應用程序時,它將鬧鐘設置一次。在它提示通知一次後,它會停止每分鐘的重複。

我不知道我的代碼哪部分出錯了。提前致謝。

編輯

所以基本上我要報警經理在上午12點每天重複。這裏是代碼:

 Calendar calendar = Calendar.getInstance(); 
    calendar.setTimeInMillis(System.currentTimeMillis()); 
    calendar.set(Calendar.HOUR_OF_DAY, 0); 
    calendar.set(Calendar.MINUTE, 1); 
     notificationCount = notificationCount + 1; 
     AlarmManager mgr = (AlarmManager) context 
       .getSystemService(Context.ALARM_SERVICE); 
     Intent notificationIntent = new Intent(context, 
       ReminderAlarm.class); 
    PendingIntent pi = PendingIntent.getBroadcast(context, 
       notificationCount, notificationIntent, 
       PendingIntent.FLAG_UPDATE_CURRENT); 
     mgr.setRepeating(AlarmManager.RTC_WAKEUP, 
       calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pi); 

     ComponentName receiver = new ComponentName(context, BootReceiver.class); 
     PackageManager pm = context.getPackageManager(); 

     pm.setComponentEnabledSetting(receiver, 
       PackageManager.COMPONENT_ENABLED_STATE_ENABLED, 
       PackageManager.DONT_KILL_APP); 

所以通過設置這樣的重複,它會在44年後仍然運行嗎?或者每天早上12點重複?

回答

2

setInexactRepeating()方法中的第三個參數是以毫秒爲單位的時間間隔。您將其設置爲自1970年1月1日00:00:00:00 UTC加60000後所經過的毫秒數,使未來計劃的下一次事件約44年。更改您的代碼如下:

mgr.setInexactRepeating(AlarmManager.RTC_WAKEUP, 
    calendar.getTimeInMillis(), 60000, pi); 
+0

比我快2秒鐘:D – joao2fast4u 2014-09-27 02:04:23

+0

@MikeM。非常感謝!有用!但我不知道如果我想將它每天運行一次,我應更換代碼:mgr.setInexactRepeating(AlarmManager.RTC_WAKEUP, \t \t \t \t calendar.getTimeInMillis(),AlarmManager.INTERVAL_DAY,PI); ? – 2014-09-27 02:12:08

+0

@ MikeM.I簡直不敢相信它就這麼簡單,直到我開始寫答案,然後......哦等等......:D – joao2fast4u 2014-09-27 02:20:46