2012年2月26日日曜日

[Android SDK] ステータスバーに通知する

ステータスバーを使いユーザに通知する機能があります。実物を見てもらった方が分かりやすいのです。
①ステータスバーに表示する
②ステータスバーに通知が蓄積される

これは、標準で用意されているNotificationManagerで簡単に、任意のメッセージやアイコンを持った通知を発行できます。
まずは、通知内容をNotificationに設定します。
  1. // Context context;  
  2. // String message, item_title, item_message;  
  3. Notification n = new Notification();  
  4. n.icon = R.drawable.ic_launcher;// アイコン(drawable指定)ここではアプリのアイコン  
  5. n.tickerText = message;// ステータスバーに表示する文字列(CharSequence指定)  
  6. n.when = System.currentTimeMillis();  
  7. // 通知をタップしたときに発行するインテント  
  8. Intent i = new Intent(context, RpNotifyActivity.class);  
  9. n.setLatestEventInfo(context, item_title, item_message, PendingIntent.getActivity(context, 0, i, 0));  
あとは、NotificationManagerに渡すとステータスバーに表示されます。
  1. NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);  
  2. manager.notify(1,n);  

2012年2月25日土曜日

[Android SDK] Android起動時にサービス等を起動する

Android起動時には"ACTION_REBOOT"インテントが発行されますので、BroadcastReceiverで受け取ることで任意の処理を実行できます。
1.BroadcastReceiverの準備
2.Intent Filterの登録
3.Permissionの登録

1.BroadcastReceiverの準備
参考に下記ように作成します。
  1. package com.runpeta.android.notify;  
  2.   
  3. import android.content.BroadcastReceiver;  
  4. import android.content.Context;  
  5. import android.content.Intent;  
  6. import android.util.Log;  
  7.   
  8. public class RpNotifyReceiver extends BroadcastReceiver {  
  9.  private static final String TAG = "RpNotifyReceiver";  
  10.   
  11.  @Override  
  12.  public void onReceive(Context context, Intent intent) {  
  13.   Log.d(TAG, "onReceive");  
  14.   String action = intent.getAction();  
  15.   if (action != null) {  
  16.    if (action.equals(Intent.ACTION_BOOT_COMPLETED)) {  
  17.     // システム起動完了時  
  18.     Log.d(TAG, "ACTION_BOOT_COMPLETED");  
  19.     context.startService(new Intent(context, RpNotifyService.class));  
  20.    }  
  21.   }  
  22.  }  
  23. }  
2.Intent Filterの登録
AndroidManifest.xmlを開き、BroadcastReceiverのIntent Filterに"ACTION_REBOOT"を登録します。

3.Permissionの登録
AndroidManifest.xmlを開き、Permissionに"RECEIVE_BOOT_COMPLETED"を登録します。

以上で、Android起動時にBroadcastReceiverのonReceiveが呼ばれるようになります。

[Android SDK] 画面ON/OFFを検知する

画面の電源ON/OFF時にはインテントが発行されますので、BroadcastReceiverを利用して受け取るようにします。 まずは、受け取るためにBroadcastReceiverを作成します。作成したActivityもしくはService内に作成すると良い。
  1. // ブロードキャストリスナー  
  2. BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {  
  3.  @Override  
  4.  public void onReceive(Context context, Intent intent) {  
  5.   String action = intent.getAction();  
  6.   if (action != null) {  
  7.    if (action.equals(Intent.ACTION_SCREEN_ON)) {  
  8.     // 画面ON時  
  9.     Log.d(TAG, "SCREEN_ON");  
  10.    } else if (action.equals(Intent.ACTION_SCREEN_OFF)) {  
  11.     // 画面OFF時  
  12.     Log.d(TAG, "SCREEN_OFF");  
  13.    }  
  14.   }  
  15.  }  
  16. };  
作成したBroadcastReceiverでインテントを受け取るには、IntentFilterを使い対象を登録する必要があります。 画面の電源ON/OFFでは次の二つを登録すると良い。
  1. // リスナーの登録  
  2. Context c;  
  3. c.registerReceiver(broadcastReceiver, new IntentFilter(Intent.ACTION_SCREEN_ON));  
  4. c.registerReceiver(broadcastReceiver, new IntentFilter(Intent.ACTION_SCREEN_OFF));  
また、受信するActivityまたはServiceのIntentFilterにも追加する。

なお、不要になったときは登録を削除します。
  1. // リスナーの登録削除  
  2. Context c;  
  3. c.unregisterReceiver(broadcastReceiver);  

[Android SDK] 電話の着信を監視する

TelephonyManagerを利用すると、電話の着信/通話中/待機時を検知することができる。
まずはシステムサービスからTelephonyManagerを取得し、listenでリスナーを登録する。
※Permissonに"android.permission.READ_PHONE_STATE"の登録が必要。
  1. // 電話状態監視リスナー  
  2. PhoneStateListener mListener = new PhoneStateListener() {   
  3.  public void onCallStateChanged(int state, String number) {  
  4.   switch(state) {  
  5.   case TelephonyManager.CALL_STATE_RINGING:// 着信時  
  6.    Log.d(TAG, "PhoneState: CALL_STATE_RINGING");  
  7.    break;  
  8.   case TelephonyManager.CALL_STATE_OFFHOOK:// 通話時  
  9.    Log.d(TAG, "PhoneState: CALL_STATE_OFFHOOK");  
  10.    break;  
  11.   case TelephonyManager.CALL_STATE_IDLE:// 待機時(リスナー登録時も呼ばれるようだ)  
  12.    Log.d(TAG, "PhoneState: CALL_STATE_IDLE");  
  13.    break;  
  14.   }  
  15.  }  
  16. };  
  17. TelephonyManager m = (TelephonyManager) this.getSystemService(Context.TELEPHONY_SERVICE);  
  18. m.listen(mListener, PhoneStateListener.LISTEN_CALL_STATE);  
不在着信の検知は、前回の電話状態を記録することで可能です。
・通話終了=CALL_STATE_RINGING -> CALL_STATE_OFFHOOK -> CALL_STATE_IDLE
・不在着信=CALL_STATE_RINGING -> CALL_STATE_IDLE

2012年2月24日金曜日

[マルチタイマー ver.1.1.1] 初Androidアプリを公開

Androidアプリの開発を始め2ヶ月ほどでやっと、簡単ですがタイマーアプリが完成しましたので、Android Marketで公開しました。試しに使ってみてください。

【対応言語】英語、日本語
【動作確認】MEDIAS N-04C、Xperia SX

ストップウォッチ、キッチンタイマーの2つのタイマーが使えます。
タイマーの切替はタブでき同時に使えます。

■ストップウォッチ
ラップ機能付きのストップウォッチです。


■キッチンタイマー
アラーム通知は、他アプリ操作中でも、画面ロック中でも通知されます。


■設定変更
 ・タイマー使用時のバックライトを常時点灯できます。
 ・キッチンタイマーのアラーム音、アラーム時間など変更できます。
  など