2014-11-24 272 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); 
    } 

感谢 艾尔沙德