setAlarmClock とは?
setAlarmClock は AlarmClockInfo でスケジュールされた時間に指定のペンディングインテントを実行するクラスです。この setAlarmClock はシステムが低電力アイドル状態(dozeモード)になっていても正確にトリガーされるので、正確な時間を必要とするアプリを作ることができますが、消費電力が大きいので使い過ぎないように注意する必要があります。このクラスはAPIレベル21以上で使用可能です。
setAlarmClock の使い方
このメソッドには2つの引数が必要になります。
alarmmanager.setAlarmClock( ① , ② );
① alarmclockinfo・・・発生時間をセットする
② pendingintent・・・実行するペンディングインテント
サンプルコード①(MainActivity.java)
サンプルコードの詳しい説明はコード内のコメントを参照してください。
public class MainActivity extends AppCompatActivity {
//ボタンオブジェクトを作成
public static Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//アクティビティにレイアウトファイルをセット
setContentView(R.layout.activity_main);
//ボタンの生成とクリックイベントのセット
button = (Button)findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//ベースコンテキストを取得
Context context = getBaseContext();
//アラームマネージャーの作成と設定
AlarmManager manager = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
//インテントの作成
Intent intent = new Intent(context,SubActivity.class);
//ペンディングインテントの作成
PendingIntent pendingIntent = PendingIntent.getActivity(context,0,intent,0);
//カレンダーの作成
Calendar calendar = Calendar.getInstance(); //現在時間が取得される
calendar.setTimeInMillis(System.currentTimeMillis() + 5000); //カレンダーを5秒進める
long alarm_time = calendar.getTimeInMillis(); //カレンダーをミリ秒に変換して変数に格納
//アラームクロックインフォを作成してアラーム時間をセット
AlarmManager.AlarmClockInfo clockInfo = new AlarmManager.AlarmClockInfo(alarm_time,null);
//アラームマネージャーにアラームクロックをセット
manager.setAlarmClock(clockInfo,pendingIntent);
//ボタンのテキストを変更
button.setText("タイマー作動中…");
}
});
}
//ボタンのテキストを変更するメソッド
public void Btn_text(String moji) {
//ボタンのテキストを変更
button.setText(moji);
}
}
サンプルコード②(SubActivity.xml)
タイマーが終了した時に起動するアクティビティです。
public class SubActivity extends Activity {
//メインアクティビティのオブジェクトを作成
private MainActivity mainActivity;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//アクティビティにレイアウトファイルをセット
setContentView(R.layout.sub_activity);
}
@Override
protected void onDestroy() {
super.onDestroy();
//メインアクティビティをインスタンス化
mainActivity = new MainActivity();
//ボタンのテキストを変更
mainActivity.Btn_text("クリック");
}
}
サンプルコード③(activity_main.xml、sub_activity.xml)
レイアウトファイルはどちらも画面の中央に Button または TextView を配置しただけのシンプルなものです。
activity_main.xml
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
android:text="クリック"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
sub_activity.xml
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
android:text="時間です!"
android:textColor="@color/colorPrimary"
android:textSize="36sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
サンプルコード④(AndroidManifest.xml)
マニフェストファイルにはアラームが作動した時に表示するアクティビティを追加します。
<!-- アクティビティを追加登録 -->
<activity android:name=".SubActivity" />
サンプルの実行
① 起動すると画面の中央にボタンが表示されます。
② ボタンをクリックすると5秒のタイマーが作動して、通知エリアに「タイマーアイコン」が表示されます。
③ 5秒経過すると予備のアクティビティが起動して予定の時間が経過したことを知らせます。
備考・その他
より詳しい使い方は公式開発者サイトを参照ください。
END