0

予定をリストに追加できるアプリケーションを作成しています。予定が近くにある場合、予定の日の特定の時間にステータス バーに通知を表示するアプリが必要です。

http://developer.android.com/guide/topics/ui/notifiers/notifications.htmlのコードを使用し て通知を作成しました。

ただし、スクリプトの「when」パラメーターは、呼び出されるとステータスバー通知が常にトリガーされるため、多少混乱します。Notification notification = new Notification(icon, tickerText, when);

そのような通知をスケジュールする最良の方法は何ですか? 簡単な方法はないようです。スレッドでリスナーアクティビティを開始して、予定日をループし、日付が現在の日付に一致したときに通知を表示するサービスを作成する必要がありますか?

4

1 に答える 1

0

ただし、スクリプトの「when」パラメータは、呼び出されたときにステータスバー通知が常にトリガーされるため、やや混乱を招きます。通知通知=newNotification(icon、tickerText、when);

正確に-呼び出されると通知がトリガーされます。例のようにwhen変数をSystem.currentTimeMilis()に設定すると、-今すぐ通知を表示することを意味します。通知をトリガーするものとして、それを処理するのはあなた次第です。アクティビティは良い選択ではないようですが、サービスは良い選択です。アプリケーションの開始時にサービスを初期化し(アプリケーションの終了時に停止することを忘れないでください)、通知の「リスニング」とトリガーを実行させます。次のようになります。

public class NotifyService extends Service {

    private NotificationManager mNM;


    @Override
    public void onCreate() {
    mNM = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
    //do some work, listen for change
    if (triggerSatisfied) showNotification();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        return START_STICKY;
    }

    @Override
    public void onDestroy() {
        // Cancel the persistent notification.
        mNM.cancelAll();
    }


    @Override
    public IBinder onBind(Intent arg0) {
        return mBinder;
    }

     private final IBinder mBinder = new LocalBinder();


     private void showNotification() {
        //code for notification goes here
     }


     public class LocalBinder extends Binder {
            NotifyService getService() {
                return NotifyService.this;
            }
        }
于 2011-10-13T07:25:03.510 に答える