如何实现在Android的多个定时通知 [英] How to implement multiple timed notifications in Android

查看:330
本文介绍了如何实现在Android的多个定时通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我为我创造了这个应用程序的最终功能。把我所造的应用是可以节省的事件,并通知用户,当时间到达的日历。我遇到的问题是,当我创建多个通知(或多个事件),它只是通知被创造了最新的。我试图让该通知,但没有成功不同的ID。在这里,codeS,我修改。这是从我见过的教程。

I have this final feature for the app that I am creating. The app that I have made is a calendar that saves events and notifies the user when the time arrives. The problem that I encounter is that when I create multiple notifications (or multiple events), it only notifies the very latest that was created. I tried to make different IDs for the notifications but to no success. Here the codes that I modified. It was from a tutorial that I've seen.

AlarmTask.java

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);
}

}

NotifyService.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();
}
}

从我的code明白了,AlarmTask.java接收到报警日期,并设置它来通知在该日期。我传递的ID是我保存在手机的数据库事件的ID。但我无法得到它添加多个通知。它只接收我保存了最新的。我希望它获得所有为每个事件的事件和设置通知。有人可以帮我吧?

From what I understand with the code, the AlarmTask.java receives the alarm date and sets the it to notify on that date. The ID that I passed is the ID of the event that I saved in the phone's database. But I couldn't get it to add multiple notifications. It only receives the latest that I saved. I would want it to get all the events and set notification for each of those events. Can somebody help me with it?

推荐答案

低于code。创建待意图

Create Pending intent with below code

PendingIntent contentIntent = PendingIntent.getActivity(this, Integer.parseInt(String.valueOf(alarmID)), backToEventDetail, Intent.FLAG_ACTIVITY_NEW_TASK );

这篇关于如何实现在Android的多个定时通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆