내가 만드는 앱에 대해이 마지막 기능이 있습니다. 내가 만든 앱은 일정을 저장하고 시간이 도착하면 사용자에게 알리는 캘린더입니다. 내가 마주 치게되는 문제는 여러 알림 (또는 여러 이벤트)을 만들 때 생성 된 최신 알림 만 표시한다는 것입니다. 나는 통보를 위해 다른 ID를 만들려고했으나 성공하지 못했습니다. 여기에 수정 된 코드가 있습니다. 그것은 제가 본 튜토리얼에서였습니다.Android에서 여러 시간에 지정한 알림을 구현하는 방법
AlarmTask.java NotifyService.java
내가 코드를 이해하는 것과public class NotifyService extends Service {
/**
* Class for clients to access
*/
public class ServiceBinder extends Binder {
NotifyService getService() {
return NotifyService.this;
}
}
// Unique id to identify the notification.
private static final int NOTIFICATION = 143;
// Name of an intent extra we can use to identify if this service was started to create a notification
public static final String INTENT_NOTIFY = "com.gpplsmje.mac.calendar.utils.INTENT_NOTIFY";
// The system notification manager
private NotificationManager mNM;
@Override
public void onCreate() {
Log.i("NotifyService", "onCreate()");
mNM = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.i("LocalService", "Received start id " + startId + ": " + intent);
// If this service was started by out AlarmTask intent then we want to show our notification
if(intent.getBooleanExtra(INTENT_NOTIFY, false)){
showNotification(intent.getLongExtra("alarmID", 0));
}
// We don't care if this service is stopped as we have already delivered our notification
return START_NOT_STICKY;
}
@Override
public IBinder onBind(Intent intent) {
return mBinder;
}
// This is the object that receives interactions from clients
private final IBinder mBinder = new ServiceBinder();
/**
* Creates a notification and shows it in the OS drag-down status bar
*/
@SuppressWarnings("deprecation")
private void showNotification(long alarmID) {
SaveEvent event = new SaveEvent(this);
event.open();
Log.d("Notification: ID", alarmID + "");
// This is the 'title' of the notification
CharSequence title = event.getEventName(alarmID);
// This is the icon to use on the notification
int icon = R.drawable.icon_reminder;
// This is the scrolling text of the notification
CharSequence text = event.getEventDesc(alarmID);
// What time to show on the notification
long time = System.currentTimeMillis();
event.close();
Intent backToEventDetail = new Intent(this, CalendarEventDetail.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, backToEventDetail, 0);
Notification notify = new Notification.Builder(this)
.setContentTitle(title)
.setContentText(text)
.setSmallIcon(icon)
.setContentIntent(contentIntent).getNotification();
notify.defaults = Notification.DEFAULT_SOUND;
notify.flags = Notification.FLAG_AUTO_CANCEL;
// Send the notification to the system.
mNM.notify(Integer.parseInt(String.valueOf(alarmID)), notify);
// Stop the service when we are finished
stopSelf();
}
}
에서, AlarmTask.java 알람 날짜를 수신하기 위해 그것을 설정
public class AlarmTask implements Runnable{
// The date selected for the alarm
private final Calendar date;
// The android system alarm manager
private final AlarmManager am;
// Your context to retrieve the alarm manager from
private final Context context;
private final long alarmID;
public AlarmTask(Context context, Calendar date, long id) {
this.context = context;
this.am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
this.date = date;
this.alarmID = id;
}
@Override
public void run() {
// Request to start are service when the alarm date is upon us
// We don't start an activity as we just want to pop up a notification into the system bar not a full activity
Intent intent = new Intent(context, NotifyService.class);
intent.putExtra(NotifyService.INTENT_NOTIFY, true);
intent.putExtra("alarmID", alarmID);
PendingIntent pendingIntent = PendingIntent.getService(context, 0, intent, 0);
// Sets an alarm - note this alarm will be lost if the phone is turned off and on again
am.set(AlarmManager.RTC, date.getTimeInMillis(), pendingIntent);
}
}
그 날짜에 알리십시오. 전달한 ID는 휴대 전화의 데이터베이스에 저장 한 이벤트의 ID입니다. 하지만 여러 알림을 추가 할 수 없었습니다. 그것은 내가 구한 최신 정보 만받습니다. 모든 이벤트를 가져 와서 각 이벤트에 대한 알림을 설정하는 것이 좋습니다. 누군가가 도와 줄 수 있습니까? 이
PendingIntent contentIntent = PendingIntent.getActivity(this, (int)(Math.random() * 100), backToEventDetail, PendingIntent.FLAG_UPDATE_CURRENT);