2011年6月28日火曜日

Portable Wi-Fi Monitor Ver 1.3 アップデート

Portable Wi-Fi Monitor Ver 1.3 にアップデート
http://ow.ly/5r2ar

コメントで頂いていた要望に対応しました。
設定画面に以下の項目が追加されています。

・ステータスバー表示/非表示オプション
 通知エリアのアイコンを表示しない設定です。
 但し、バッテリー残量が閾値を下回ったときの通知は表示されます。
・バッテリーアイコン表示オプション
 バッテリー型のアイコンを表示する設定です。
 設定しない場合は、Portable Wi-Fiのアイコンが表示されます。
・バッテリーレベル表示オプション
 バッテリーレベルを表示する設定です。
 100, 90, 80, , , , 30, 20, 10, 5 単位に数値を表示します。
 正確な値はルーター画面かウィジェットで確認して下さい。
・ウィジェットで、バッテリーレベルが 20%以下の場合、赤字で数値を表示するようにしました。
・アプリアイコンをちょっと変えました。

2011年6月25日土曜日

AlarmManagerで定期的に繰り返しサービスを実行する

一定時間に処理を実行したり、定期的に処理を繰り返すにはAlarmManagerを使用します。
今回は、AlarmManagerで定期的にServiceを実行するメモです。


AlarmManager でサービスを登録
  1. ServiceのIntentを作成します。
  2. Serviceを開始するPendingIntentを取得します。
  3. AlarmManager#setInexactRepeatingでアラームをスケジュールします。
Intent serviceIntent = new Intent(this, MyService.class);
PendingIntent pendingIntent
= PendingIntent.getService(this, 0, serviceIntent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
am.setInexactRepeating (
AlarmManager.RTC,
System.currentTimeMillis(),
//AlarmManager.INTERVAL_HOUR,
AlarmManager.INTERVAL_FIFTEEN_MINUTES,
pendingIntent);

AlarmManager.RTC - wall-clock timew in UTC 要は現実世界での経過時間
AlarmManager.INTERVAL_FIFTEEN_MINUTES - 15分(900000秒) Intervalの定数


AlarmManager でサービスを解除
Intent serviceIntent = new Intent(this, MyService.class);
PendingIntent pendingIntent
= PendingIntent.getService(this, 0, serviceIntent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
am.cancel(pendingIntent);


adb コマンドで、AlarmManagerの登録状態を確認
# adb shell dumpsys alarm
Current Alarm Manager state:
Realtime wakeup (now=1308812542789):
RTC #2: Alarm{487c9590 type 1 net.granoeste.example.repeatedlyrunning}
type=1 when=1308813378896 repeatInterval=900000 count=1
operation=PendingIntent{487f4ac0: PendingIntentRecord{487bb340 net.granoeste.example.repeatedlyrunning startService}}
Elapsed realtime wakeup (now=620467383):
Broadcast ref count: 0
Alarm Stats:
net.granoeste.example.repeatedlyrunning
44ms running, 0 wakeups
1 alarms: flg=0x4 cmp=net.granoeste.example.repeatedlyrunning/.MyService
view raw dumpsys alarm hosted with ❤ by GitHub

2011年6月18日土曜日

HandlerThreadで、LooperとHandlerを使用する方法

メッセージキューの仕組みを実装しようとして、LooperやThread、Handlerを調べていたら、
HandlerThreadという便利なクラスがあったのでメモ

HandlerThread の概要
Handy class for starting a new thread that has a looper. The looper can then be used to create handler classes. Note that start() must still be called.

ルーパーを持つ新しいスレッドを開始するための便利なクラス。ルーパーは、ハンドラクラスを作成するために使用することができます。 start()を呼び出す必要があることに注意してください。

HandlerThreadはThreadを継承していて、内部にLooperを保持しています。
そのLooperを使って、Handlerのインスタンスを作成します。

android.app.IntentService のソースを参考にコードを記述してみました。

// With reference to the android.app.IntentService of the Android framework
// Member variable
private volatile Looper mServiceLooper;
private volatile ServiceHandler mServiceHandler;
// Handler Class
private final class ServiceHandler extends Handler {
public ServiceHandler(Looper looper) {
super(looper);
}
@Override
public void handleMessage(Message msg) {
// TODO
// Process the received message.
}
}
// Create the HandlerThread, and start
// Get the looper from the HandlerThread to the constructor of the Handler it.
public void onYourCreateMethod() {
HandlerThread thread = new HandlerThread("<Thread Name>");
thread.start();
mServiceLooper = thread.getLooper();
mServiceHandler = new ServiceHandler(mServiceLooper);
}
// Send a message using the Handler was created.
public void onYourMethod(Intent intent) {
Message msg = mServiceHandler.obtainMessage();
msg.what = <What>;
msg.obj = <Object>;
mServiceHandler.sendMessage(msg);
}
// To terminate the Handler
public void onYourDestroyMethod(Intent intent) {
mServiceLooper.quit();
}
view raw gistfile1.java hosted with ❤ by GitHub



参考サイト

2011年6月16日木曜日