Android Studio 目覚まし時計を作ってみる Part3
シロルです。
前回の続きで目覚まし時計を作っていきます。
今回はアラームを設定するのにAlarmmanagerを使ってみたいと思います。
1. まずAlarmmanagerとは(ここで使う分だけの説明です)
- Alarmmanagerは設定した時間に別のサービスを起動させてくれる機能があります。時間とサービスを指定するだけで後は勝手にやってくれるということですね。
- 起動するサービスの指定します。いきなりアクティビティを指定して起動させることはできないみたいです。
- 時間の指定にCalendarを使うことが出来ます。時間の計算をするプログラムを書かなければいけないみたいですが。
- 起動するときの流れは、AlarmManager起動 → サービス起動(自分で作る。AlarmManagerがサービスでないと指定できなかったため。) → レシーバーで受け取る(専用のjavaファイルを作る) → アクティビティ起動
なんか大変そう…。
とりあえずAlarmManargerの使い方はこんな感じです。
import android.app.AlarmManager;
AlarmManager am; am = (AlarmManager)getSystemService(Context.ALARM_SERVICE); am.set(AlarmManager.RTC_WAKEUP,calendar.getTimeInMillis(),pendingintent);
引数が謎すぎるので説明します。
getSystemServiceでは「アラームの機能を使うよ」と設定しています。AlarmManagerを使う際はこれを必ず書かなければいけないみたいです。
am.setでは一つ目の引数でどのように起動するかを指定しています。
二つ目の引数でClendarを指定します。この指定されたClendarの時刻にAlarmManagerが起動します。
三つ目の引数である時間、あるタイミングで実行されるIntentを指定しています(ここにサービスを作って指定してあげるみたいです)。
2つ目と3つ目の引数は事前に設定して用意しておく必要があります。
とりあえずjavaファイルが2つ必要になったのが分かりました。
Calendarの設定は放置でサービスを作ります。(javaファイルです)
2. AlarmManagerに設定するPendingIntent
コードです。Intent intent = new Intent(getApplicationContext(), messageService.class); Context ct = getApplication(); PendingIntent pendingIntent = PendingIntent.getService(ct, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
Intentの引数のgetApplicationContextはとりあえず指定しておけみたいな感じで指定しました。
第二引数は起動させるサービスを指定しています。(下で作ります)
二行目はコンテキストを取得。
PendingIntentはある時にIntentを起動させるために使います。
PendingIntent.getServiceで第二引数を「0」にしていますが、複数アラームの設定をする場合は各アラームごとにここの数値を変えなければならないみたいです。
3. MainActivityに追加
前回書いた「設定した時間を表示するメソッド(timeConf)」のしたに追加します。sironekosukaru.hatenablog.com
public void timerSet(Calendar calendar){ //実行するサービスを指定 Intent intent = new Intent(getApplicationContext(), messageService.class); Context ct = getApplication(); PendingIntent pendingIntent = PendingIntent.getService(ct, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); // AlarmManager の設定・開始 AlarmManager am = (AlarmManager)getSystemService(Context.ALARM_SERVICE); am.set(AlarmManager.RTC_WAKEUP,calendar.getTimeInMillis(),pendingIntent); }
4. AlarmManagerに設定するサービス
新しくjavaファイルをMainActivityと同じ場所に作ります。今回は「messageService.java」とします。
import android.app.Service; import android.content.Intent; import android.os.IBinder;
public class messageService extends Service{ //これがないとエラーになる(おまじない的な感じ) public IBinder onBind(Intent intent) { return null; } public void onCreate() { Thread thread = new Thread(null, task, "sentMessageService"); thread.start(); } Runnable task = new Runnable() { public void run() { Intent messageBroadcast = new Intent(); messageBroadcast.setAction("activityAction"); sendBroadcast(messageBroadcast); stopSelf(); } }; }
1つずつ説明します。
public class messageService extends Service{
サービスを作る場合はServiceクラスを継承させたクラスを作成しなければいけません。
public IBinder onBind(Intent intent) { return null; }
これがないとエラーになります。以上。勉強しておきます…
public void onCreate() { Thread thread = new Thread(null, task, "sentMessageService"); thread.start(); }
スレッドを作ります。"sentMessageService"のところはスレッドの名前なので何でもいいと思います。
thread.start();で下にあるコードの処理を実行します。
Runnable task = new Runnable() { public void run() { Intent messageBroadcast = new Intent(); messageBroadcast.setAction("activityAction"); sendBroadcast(messageBroadcast); stopSelf(); } };
スレッドの処理です。
MessageBroadcastという名前のIntentを作成しています。
setActionでメッセージを設定しています。
sendBroadcast();で先ほど設定したメッセージをブロードキャストします。(これを後で作るレシーバーで受け取る)
最後にサービスを止める処理を行う。
疲れました。今回はここまでにします。
次回はレシーバーを作って一応最低限完成になると思います。