0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

通知設定2

Posted at

AlarmManagerはAndroidアプリケーションでタイマーやアラームのような予定されたタスクを管理するためのクラスです。以下はAlarmManagerの基本的な使い方です:

  1. AlarmManagerのインスタンスを取得:
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
  1. PendingIntentを作成:

タイマーのタスクを実行するためにPendingIntentを作成します。これには、アラームが発生したときに実行されるコード(Intent)が含まれます。

Intent intent = new Intent(this, YourReceiver.class); // YourReceiverはアラームが発生したときに呼び出されるクラス
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, requestCode, intent, PendingIntent.FLAG_UPDATE_CURRENT);
  1. アラームを設定:

アラームを設定するには、AlarmManagerにアラームの種類(単発または繰り返し)、発生時刻、およびPendingIntentを指定します。

// 単発アラームの場合
alarmManager.set(AlarmManager.RTC, triggerTime, pendingIntent);

// 繰り返しアラームの場合
alarmManager.setRepeating(AlarmManager.RTC, triggerTime, interval, pendingIntent);
  1. アラームのキャンセル:

アラームをキャンセルする場合は、PendingIntentを使用してAlarmManagerから削除します。

alarmManager.cancel(pendingIntent);

これで、AlarmManagerを使用してアラームを設定し、タスクをスケジュールし、必要に応じてキャンセルできるようになります。アラームがトリガーされたときに指定されたPendingIntentが発動し、指定されたアクションが実行されます。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?