0

こんにちは、プッシュ経由でアラームを作成しているので、通知を受け取ったらアラームを設定します。すべて正常に動作しますが、私が言った瞬間ではなく、プッシュした瞬間にアラームが表示されます。

これが私のコードです:

public void set_alarm(int year, int month, int day, int hour, int minute,
        String title) {

    Calendar cal = Calendar.getInstance();
    cal.set(Calendar.MONTH, 5);
    cal.set(Calendar.YEAR, 2012);
    cal.set(Calendar.DAY_OF_MONTH, 31);
    cal.set(Calendar.HOUR_OF_DAY, 9);
    cal.set(Calendar.MINUTE, 46);

    Intent intent = new Intent(c, AlarmActivity.class);
    intent.putExtra("title", title);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(
            c.getApplicationContext(), 1253, intent,
            PendingIntent.FLAG_UPDATE_CURRENT | Intent.FILL_IN_DATA);

    AlarmManager alarmManager = (AlarmManager) c
            .getSystemService(c.ALARM_SERVICE);

    alarmManager.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(),
            pendingIntent);
}

そして、通知を受け取るクラスは次のとおりです。

public class AlarmActivity extends BroadcastReceiver {

@Override
public void onReceive(Context c, Intent i) {
    Toast.makeText(c, "Alarm worked.", Toast.LENGTH_LONG).show();

    int icon = R.drawable.ic_dialog_alert;
    long when = System.currentTimeMillis();

    Bundle bundle = i.getExtras();
    String title = bundle.getString("title");

    final int NOTIF_ID = 1234;
    NotificationManager notofManager = (NotificationManager) c
            .getSystemService(Context.NOTIFICATION_SERVICE);

    Intent notificationIntent = new Intent(c, AnonymeActivity.class);
    PendingIntent contentIntent = PendingIntent.getActivity(c, 0,
            notificationIntent, 0);
    Notification notification = new Notification(icon, "Faraway", when);

    notification.setLatestEventInfo(c, "Faraway alarm", title,
            contentIntent);

    notification.flags = Notification.FLAG_INSISTENT;
    notification.defaults |= Notification.DEFAULT_SOUND;

    notofManager.notify(NOTIF_ID, notification);
}

}

ありがとう

4

1 に答える 1

1

現在の日付より前の 2012 年 5 月 31 日の日付で Calendar インスタンスを作成しています。

ドキュメントによると:

時間が過去に発生した場合、アラームはすぐにトリガーされます。

正しい日付と時刻を設定するだけです。実際には、set_alarm() 関数に渡すパラメーターを使用する必要があります。

(出典: http://developer.android.com/reference/android/app/AlarmManager.html#set(int, long, android.app.PendingIntent )

于 2012-07-31T08:00:00.663 に答える