下面是設置通知的通知時間的代碼...當應用程序關閉時也會通知您。
public class AlarmTiming {
static AlarmManager alarmManager;
static PendingIntent pendingIntent;
static DatabaseAdapter databaseAdapter;
static String[] pieces;
/*..duration time for alarm to notify you every 3 days ...
mean twice in a week.. you just have to set it again when notification is reached/alarmed
for the next duration*/
static int alarmDuration = 72;
public static void SetTwiceNotification(Context c) {
String formattedDate = new SimpleDateFormat("HH:mm:ss").format(Calendar.getInstance().getTime());
pieces = formattedDate.split(":");
Calendar calNow = Calendar.getInstance();
Calendar calSet = (Calendar)calNow.clone();
calSet.set(Calendar.HOUR_OF_DAY, Integer.parseInt(pieces[0])+alarmDuration);
calSet.set(Calendar.MINUTE, Integer.parseInt(pieces[1]));
calSet.set(Calendar.SECOND, 0);
calSet.set(Calendar.MILLISECOND, 0);
if(calSet.compareTo(calNow) <= 0) {
calSet.add(Calendar.DATE, 1);
}
setAlarm(calSet,c);
}
private static void setAlarm(Calendar targetCal,Context c) {
Intent intent = new Intent(c, AlarmBroadcaster.class);
//..
pendingIntent = PendingIntent.getBroadcast(c, 1, intent,PendingIntent.FLAG_ONE_SHOT);
alarmManager = (AlarmManager) c.getSystemService(Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, targetCal.getTimeInMillis(), pendingIntent);
}
這裏是廣播接收機類接收通知...
public class AlarmBroadcaster extends BroadcastReceiver {
Context context;
String typeFlag;
int flagId;
@Override
public void onReceive(Context context, Intent intent) {
this.context = context;
createNotification(context);
AlarmTiming.SetTwiceNotification(context);
}
public void createNotification(Context context) {
PendingIntent pendingIntent = PendingIntent.getActivity(context,0, new Intent(context,LoginActivity.class),0);
NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.notifi_righter_logo);
//...
NotificationCompat.InboxStyle inboxStyle =
new NotificationCompat.InboxStyle();
String[] events = {"You are reminded to review your APP"};
// Sets a title for the Inbox in expanded layout
inboxStyle.setBigContentTitle(" Notification !");
// Moves events into the expanded layout
for (int i=0; i < events.length; i++) {
inboxStyle.addLine(events[i]);
}
// Moves the expanded layout object into the notification object.
builder.setStyle(inboxStyle);
builder.setContentIntent(pendingIntent);
builder.setDefaults(NotificationCompat.DEFAULT_SOUND);
builder.setAutoCancel(true);
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(1,builder.build());
}
}
用戶報警管理器和設置在一週內不同的時間兩個報警。 –
告訴我怎麼做 –
好吧讓我爲你組裝代碼....看看我的回答 –