2014-11-24 277 views
1

我是新來的,所以我有點迷路。鬧鐘管理器沒有觸發

我已經構建了一個通知,並將其作爲pendingIntent傳遞給了警報管理器,但不是等待間隔觸發警報並顯示通知,即時顯示通知。 我做了什麼錯誤,那是不是允許鬧鐘設置正確?

public class NotificationController{ 

Context context; 

public void createNotification(Context context){ 

    this.context = context; 
    Notification notification = getNotification(); 

    //creates notification 
    Intent intent = new Intent(context, NotificationReceiver.class); 
    intent.putExtra(NotificationReceiver.NOTIFICATION_ID, 1); 
    intent.putExtra(NotificationReceiver.NOTIFICATION, notification); 
    PendingIntent pIntent = PendingIntent.getBroadcast(context, 0, intent, 0); 

    //schedules notification to be fired. 
    AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); 
    alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, 10000 , pIntent); 

} 


private Notification getNotification(){ 

    Intent intent = new Intent(context, MainActivity.class); 
    PendingIntent pIntent = PendingIntent.getActivity(context, 0, intent, 0); 

    Notification.Builder builder = new Notification.Builder(context) 
      .setContentTitle("Reminder") 
      .setContentText("Car service due in 2 weeks") 
      .setSmallIcon(R.drawable.ic_launcher) 
      .setContentIntent(pIntent); 
    return builder.build(); 
} 
} 

我的接收機

public class NotificationReceiver extends BroadcastReceiver { 

public static String NOTIFICATION_ID = "notification-id"; 
public static String NOTIFICATION = "notification"; 

public void onReceive(Context context, Intent intent) { 

    NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); 

    Notification notification = intent.getParcelableExtra(NOTIFICATION); 
    int id = intent.getIntExtra(NOTIFICATION_ID, 0); 

    notification.flags |= Notification.FLAG_AUTO_CANCEL; 
    notificationManager.notify(id, notification); 

} 
} 

我也跟在AndroidManifest <receiver android:name=".NotificationReceiver" >註冊。

+0

請發表您的清單,讓用戶檢查所有的權限,在那裏,正確的.. – Makwana 2014-11-24 11:33:50

回答

3

在這一行

alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, 10000 , pIntent); 

您指定的報警有火在10秒裝置已經啓動後(這是在過去,這樣的警報立即發生)。如果你將它10秒後設定鬧鈴想要的,你用的毫秒數,因爲該設備已經啓動加10秒時:

alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, System.currentTimeMillis() + 10000 , pIntent); 
+2

謝謝!出於某種原因,我不得不使用'AlarmManager.RTC_WAKEUP'來代替,但我的主要問題是過去的時間間隔就像你說的那樣。 – 2014-11-24 22:00:22

+0

對於'AlarmManager.RTC [_WAKEUP]'''AlarmManager.ELAPSED_REALTIME [_WAKEUP]'alram和'System.currentTimeMillis()''應該使用'SystemClock.elapsedRealtime()'' – 2015-05-26 10:34:49

1

試試這個:

alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, System.currentTimeMillis()+10000 , pIntent); 
0

對於我的老方法是幾個小時後殺死。請參閱我用於不同操作系統版本的代碼。可能會有所幫助。

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { 

     AlarmManager.AlarmClockInfo alarmClockInfo = new AlarmManager.AlarmClockInfo(alarmTime, pendingIntent); 
     alarmManager.setAlarmClock(alarmClockInfo, pendingIntent); 
    } 
    else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { 

     alarmManager.setExact(AlarmManager.RTC_WAKEUP, alarmTime, pendingIntent); 
    } 
    else { 

     alarmManager.set(AlarmManager.RTC_WAKEUP, alarmTime, pendingIntent); 
    } 

感謝 艾爾沙德