这是我的代码。 从那里我无法以编程方式生成警报..
Calendar cal = Calendar.getInstance();
int id = (int) cal.getTimeInMillis();
Intent myIntent = new Intent(this,MyScheduledReceiver.class);
myIntent.putExtra("taskTitle", taskTitle.getText().toString());
myIntent.putExtra("taskDetails", taskDetails.getText().toString());
cal.setTimeInMillis(System.currentTimeMillis());
cal.set(year, mon, day,tp.getCurrentHour(),tp.getCurrentMinute());
PendingIntent sender = PendingIntent.getBroadcast(getApplicationContext(), id,
myIntent,PendingIntent.FLAG_UPDATE_CURRENT| Intent.FILL_IN_DATA);
AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
am.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), sender);
我在清单文件中声明了
<receiver android:name=".MyScheduledReceiver"></receiver>
广播接收器中的一个。
NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
NotificationManager manger = (NotificationManager)
context.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(R.drawable.ic_launcher, "Combi Note",
System.currentTimeMillis());
PendingIntent contentIntent = PendingIntent.getActivity(context,
NOTIFICATION_ID,
new Intent(context, MyScheduledReceiver.class), 0);
Bundle extras=intent.getExtras();
String title=extras.getString("taskTitle");
String note=extras.getString("taskDetails");
notification.setLatestEventInfo(context, note, title, contentIntent);
notification.flags = Notification.FLAG_INSISTENT;
notification.defaults |= Notification.DEFAULT_SOUND;
manger.notify(NOTIFICATION_ID++, notification);
答案 0 :(得分:0)
我有类似的东西并且有效。
首先你要声明一个Pending:
Intent intent = new Intent(Global.a, EventAlarmReceiver.class);
intent.putExtra("title", ""+cd.title);
intent.putExtra("desc", ""+cd.description);
PendingIntent pendingIntent = PendingIntent.getBroadcast(
Global.a.getApplicationContext(), (int) (cd.alarmTime), intent, PendingIntent.FLAG_CANCEL_CURRENT);
AlarmManager alarmManager = (AlarmManager) Global.a.getSystemService(Activity.ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC, cd.alarmTime, pendingIntent);
在EventAlarmReceiver类中我有:
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Activity.NOTIFICATION_SERVICE);
String text = String.valueOf(intent1.getCharSequenceExtra("title"));
Notification notification = new Notification(R.id.icon,
text, System.getTimeInMillis());
notification.vibrate = new long[]{100,250,300,330,390,420,500};
// Hide the notification after its selected
notification.flags |= Notification.FLAG_AUTO_CANCEL;
Intent intent = new Intent(context, ShowEventActivity.class);
intent.putExtra("title", String.valueOf(intent1.getCharSequenceExtra("title")));
intent.putExtra("desc", String.valueOf(intent1.getCharSequenceExtra("desc")));
String text1 = String.valueOf(intent1.getCharSequenceExtra("desc"));
PendingIntent activity = PendingIntent.getActivity(context, (int) System.getTimeInMillis() , intent, PendingIntent.FLAG_CANCEL_CURRENT);
notification.setLatestEventInfo(context, text, text1, activity);
notificationManager.notify((int)System.getTimeInMillis(), notification);
确保在Manifest中声明声明Receiver所在的包:例如在我的情况下,EventAlarmReceiver类是包com.app.name.notifications所以在清单中我有:
<receiver android:name=".notifications.EventAlarmReceiver"></receiver>