1

イベントに応じて、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);  


       }

}

ここで、2 つのことを知りたいと思います。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 - 将来のアラームを設定することにより、指定された時間に OS に PendingIntent を開始するように要求します (この場合、Broadcast を送信します。上記の例では、サービスを送信します) (そのため、設定する必要があります)カレンダーの時刻を未来のいつかに!)

2 - 複数のアラームを作成するには、適切な時間にスケジュールされた異なる PendingIntent を使用してアラーム マネージャーを呼び出す必要があります (アラームを発生させたい場合)。同じことが Notification にも当てはまり、(一意の ID を持つ) 複数の通知を作成します。

于 2012-11-19T17:22:07.883 に答える