1

そのため、一定数のアラームを作成する必要があります。ユーザーが 1 日を通して一定の間隔で 10 個のアラームを必要とする場合、10 個のアラームを作成できるように効率的にコードを作成するにはどうすればよいでしょうか?

または、1 つのアラームを複数回上書きすることはできますか?

これは Android アプリ開発に関するものです。

4

2 に答える 2

1

アラームの数の選択はユーザーに任されていますか? もし、そうなら、

  • ユーザー自身が新しいアラームを追加したり、アラームの数を指定したりするための (アクティビティ内の) ユーザー インターフェイスを作成します。
  • アラームを宣言し、アラームの合計数を維持しながらユーザー要求ごとに新しいオブジェクトをインスタンス化し、それに応じてそれぞれを常に変更します。

// context variable contains your `Context`
    AlarmManager mgrAlarm = (AlarmManager) context.getSystemService(ALARM_SERVICE);
    ArrayList<PendingIntent> intentArray = new ArrayList<PendingIntent>();
    int[] TimeForAlarm = new int[UserInput];
    // Set the time for each alarm according to your need and UserInput.
    for(i = 0; i < UserInput; ++i)
    {
       Intent intent = new Intent(context, OnAlarmReceiver.class);
       // Loop counter `i` is used as a `requestCode`
       PendingIntent pendingIntent = PendingIntent.getBroadcast(context, i, intent, 0);
       // Single alarms according to time we have in TimeForAlarm.
       mgrAlarm.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, 
                    TimeForAlarm[i], 
                    pendingIntent); 

       intentArray.add(pendingIntent);
    }

これにより、TimeForAlarm 配列に従って、時間として「UserInput」数のアラームが作成されます。最後に、intentArray にはすべての保留中のインテントが含まれます (必要な場合)。

于 2013-06-25T07:52:21.640 に答える