0
我试图在5秒后使用报警管理器显示通知。我尝试了很多网站,但无法理解,请举一个简单的例子来解释如何使用闹钟管理器并将通知与它连接。Android管理器在5秒后没有发出通知
我是新手。
这是我用来设置闹钟的功能,5秒后我没有收到通知,而不是在模拟器和android手机中。
public void setAlarm(View view)
{
Intent alertIntent = new Intent(this, AlertReciver.class);
Long alertTime = new GregorianCalendar().getTimeInMillis()+5*1000;
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, alertTime, PendingIntent.getBroadcast(this, 1, alertIntent, PendingIntent.FLAG_UPDATE_CURRENT));
tv.setText("completed");
}
这个类,使其工作
public class AlertReciver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
createNotification(context,"times Up", "5 SEcond has passed", "Alert");
}
public void createNotification(Context context,String msg, String msgText, String msgAlert){
PendingIntent notificIntent = PendingIntent.getActivity(context, 0, new Intent(context, MainActivity.class), 0);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.ne)
.setContentTitle(msg)
.setTicker(msgAlert)
.setContentText(msgText);
mBuilder.setContentIntent(notificIntent);
mBuilder.setDefaults(NotificationCompat.DEFAULT_SOUND);
mBuilder.setAutoCancel(true);
NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(context.NOTIFICATION_SERVICE);
mNotificationManager.notify(1, mBuilder.build());
}
}
这意味着我有安卓4.2.2意味着API 19所以它不会唤醒,所以我将使用16或更低,但我仍然有问题我usesed闪电还2.2.1 –
我试图puttint setExact它仍然没有工作SRY。我需要更清楚的解释,我只是增加了我的最小api到19。 –