1

我有一个应用程序需要在一天内显示某些事件的通知,具体取决于事件。

我知道该怎么做,如果我需要一项服务在后台保持运行,或者它将在后台运行,在某些日子获得这些事件。

private void Notificar(String descricaoEvento){
    NotificationManager notifier = (NotificationManager)this.getSystemService(Context.NOTIFICATION_SERVICE);
    Notification notification = new Notification(R.drawable.ic_launcher, "Novo evento no The Point Club", System.currentTimeMillis()); 
    notification.flags |= Notification.DEFAULT_SOUND;

    PendingIntent pIntent = PendingIntent.getActivity(this, 0, null, 0); 
    notification.setLatestEventInfo(this, "Title", descricaoEvento.toString(), pIntent);        
    notifier.notify(0x007, notification);       
}

用这个方法来调用当合适的日期!


感谢您的收听,我将尝试解释我做对的事情:我正在申请一个俱乐部派对,当我打开应用程序时,应用程序会显示下一个庆祝活动,但我希望是在android通知栏中创建一个通知,其中有一个聚会,作为提醒。

当我打开活动以列出事件时,我使用此代码:

    Calendar c = Calendar.getInstance();
    c.setTimeInMillis(System.currentTimeMillis());
    int anoAtual= c.get(Calendar.YEAR);
    long time = c.getTimeInMillis();
    Intent it = new Intent("EXECUTAR_ALARME") 
    p = PendingIntent.getBroadcast(Eventos.this, 0, it, 0);
    AlarmManager alarme = (AlarmManager) getSystemService(ALARM_SERVICE);
    alarme.set(AlarmManager.RTC_WAKEUP, time, p);

线

    Intent it = new Intent("EXECUTAR_ALARME") 

将调用类:

    public class Alarm extends BroadcastReceiver{   @Override
       public void onReceive(Context contexto, Intent intent) {
           NotificationManager notifier =(NotificationManager) contexto.getSystemService(Context.NOTIFICATION_SERVICE);
           Notification notification = new Notification(R.drawable.ic_launcher, "New event in Club", System.currentTimeMillis()); 
           notification.flags |= Notification.DEFAULT_SOUND;
           //Intent i = new Intent(this, Eventos.class);
           PendingIntent pIntent = PendingIntent.getActivity(contexto, 0, null, 0); 
           notification.setLatestEventInfo(contexto, "Club", "Teste", pIntent);     
           notifier.notify(0x007, notification);  


       }

}

现在我想知道两件事: 1 - 我如何让它在后台运行而不用应用程序打开。2 这段代码只创建了一个警报,我需要创建多个。

谢谢您的帮助。

4

1 回答 1

0

使用 AlarmManager 在您的特定时间运行 PendingIntent。那时,如果您计划创建一个向用户显示的通知,它存储在 PendingIntent 中的意图应该是一项服务。如果您可以将其形成一个问题,我可能会提供更多帮助,但目前尚不清楚您要完成什么,以及您设法完成了什么。


额外细节

在您的代码中,您似乎正在为现在安排警报,而不是在将来的某个时间。我建议以下方法来创建警报:

    Calendar c = Calendar.getInstance();
    // don't do this:
    // c.setTimeInMillis(System.currentTimeMillis()); // This just sets the alarm time to "now"

    // set the calendar to 8:00 pm:
    c.set(Calendar.HOUR_OF_DAY, 20);
    c.set(Calendar.MINUTE, 0);

    long alarmTime = c.getTimeInMillis();
    Intent it = new Intent(ServiceClass.getClass());
    // Even though the docs say that the request code doesn't matter making it unique tells the alarmManager that it's a different PendingIntent
    PendingIntent pendingIntent = PendingIntent.getService(this, 0, it, 0);

    AlarmManager alarmMgr = (AlarmManager) getSystemService(ALARM_SERVICE);
    alarmMgr.set(AlarmManager.RTC_WAKEUP, alarmTime, pendingIntent); // set the first alarm for 8:00

    c.set(Calendar.MINUTE, 30);
    Intent it2 = new Intnt(ServiceClass.getClass());
    PendingIntent pi2 = PendingIntent.getService(this, 0, it2, 0);
    alarmMgr.set(AlarmManager.RTC_WAKEUP, alarmTime, pi2); // set the second alarm for 8:30

线

Intent it = new Intent(ServiceClass.getClass());

将启动类 ServiceClass:

class ServiceClass extends Service {

    @Override
    public IBinder onBind(Intent intent) {
        // TODO TRAVIS Auto-generated method stub
        return null;
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) { // onStartCommand is run when the service is started by the AlarmManager
        // build and set the notification here
        NotificationManager notifMgr = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        // build notification
        Notification notification = new Notification.Builder(getApplicationContext())
                .setContentTitle("New mail from " + sender.toString())
                .setContentText(subject)
                .setSmallIcon(R.drawable.new_mail)
                .setLargeIcon(aBitmap)
                .build();

        int ID = 0;

        notifMgr.notify(ID++, notification);   // use a unique id every time if you want multiple notifications (sounds like what you want, but not a best practice)

        notification = new Notification.Builder(getApplicationContext())
                .setContentTitle("New event from " + sender.toString())
                .setContentText(subject)
                .setSmallIcon(R.drawable.new_mail)
                .setLargeIcon(aBitmap)
                .build();

        notifMgr.notify(ID++, notification);  // add the second notification to the notification bar            
        return super.onStartCommand(intent, flags, startId);
    }
}

回答您的问题:

1 - 通过在未来的某个时间设置警报,您要求操作系统在指定的时间启动您的 PendingIntent(在您的情况下,发送广播;在我上面的示例中,服务)(这就是您需要设置的原因日历的时间到未来的某个时间!)

2 - 创建多个警报是通过在适当的时间安排不同的 PendingIntent 调用警报管理器(当您希望它们触发时)。Notification 也是如此,创建多个(具有唯一 ID)。

于 2012-11-19T17:22:07.883 回答