我是Android开发的新手。我正在开发一个涉及创建“待办事项”列表的项目,用户可以在其中添加即将发生的事件的详细信息。然后,这些事件将显示在列表视图中。现在我希望在事件发生之日创建通知&显示。
我有以下问题。
答案 0 :(得分:2)
尝试此方法一旦通过。这适用于我在特定日期和时间发送事件通知。请使用以下方法。
Calendar calSet = Calendar.getInstance();
calSet.setTimeInMillis(System.currentTimeMillis());
calSet.clear();
calSet.set(Eventyear,Eventmonth,DayEvent,EventTimeHrs,EventTimeMin, 0);
setAlarm(context,calSet);
private void setAlarm(Context context, Calendar targetCal) {
//SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss aa");
Intent intent = new Intent(context, com.newme.main.AlarmReceiver.class);
intent.putExtra("title", title);
intent.putExtra("des", des);
intent.putExtra("startTime", StartTime);
intent.putExtra("date", dateStr);
intent.putExtra("venue", venue);
intent.putExtra("time", timeStr);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, RQS_1, intent, 0);
AlarmManager alarmManager = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
alarmManager.cancel(pendingIntent);
alarmManager.set(AlarmManager.RTC_WAKEUP, targetCal.getTimeInMillis(), pendingIntent);
}
public class AlarmReceiver extends BroadcastReceiver {
int mNotificationId = 001;
private String des,title,StartTime,date,venue,time;
@SuppressWarnings({ "deprecation", "static-access" })
@Override
public void onReceive(Context context, Intent intent) {
//Toast.makeText(context, "Show Event", Toast.LENGTH_LONG).show();
int icon = 0;
long notificationTime = 0;
icon = R.drawable.app_icon_client;
des = intent.getStringExtra("des");
String decStr =Html.fromHtml(des).toString();
title = intent.getStringExtra("title");
StartTime = intent.getStringExtra("startTime");
date = intent.getStringExtra("date");
venue = intent.getStringExtra("venue");
time = intent.getStringExtra("time");
notificationTime = System.currentTimeMillis();
PendingIntent resultPendingIntent =
PendingIntent.getActivity(
context,
0,
intent,
PendingIntent.FLAG_CANCEL_CURRENT
);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
context);
Notification notification = mBuilder.setSmallIcon(icon).setTicker(title).setWhen(0)
.setAutoCancel(true)
.setContentTitle(AppConstants.APP_NAME)
.setStyle(new NotificationCompat.BigTextStyle().bigText("Reminder"+" "+title+"\ntomorrow. Hope to see you there."))
.setContentIntent(resultPendingIntent)
.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
.build();
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(mNotificationId, notification);
/**
* vibration
* */
notification.vibrate=new long[] {100L, 100L, 200L, 500L};
PushNotificationUtils.notificationReceived=true;
PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
WakeLock wl = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP, "TAG");
wl.acquire();
}
}
答案 1 :(得分:0)
您只需使用AlarmManger
即可。
请参阅此链接以了解如何设置警报。
然后,您可以使用notificationManager
对其进行自定义,以在AlertDemo类中创建通知。