2012年3月19日月曜日

Notificationのフラグによる動作の設定


Notificationクラスのflagフィールドに代入することで、下記設定が可能
フラグは OR で代入して、複数設定することも出来る。

Notification.FLAG_AUTO_CANCEL
 しばらくすると自動的に通知が消える。ポップアップでお知らせするために使える

Notification.FLAG_INSISTENT
 通知ウィンドウを開くか、キャンセルされるまで、音が鳴り続ける。かなりうざそう

Notification.FLAG_ONGOING_EVENT
 通常の通知ウィンドウの上位のバーの部分に表示する。常に常駐するアプリに使える。

Notification.FLAG_NO_CLEAR
 通知をクリアを押してもクリアできないようにする FLAG_ACTIVITY_NEW_TASK ?

2012年2月20日月曜日

Preferenceの使い方 2

preferences.xmlを使わずに直接書き込みを行う方法

 
     SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(this);
     Editor e = pref.edit();
     e.putString("ip", "保存するテキスト");
     e.putString("path",  "保存するテキスト" );
     e.commit();
こんな感じ。

2012年2月9日木曜日

PendingIntent

RemoteViewやNotificationクラスに用いるPendingIntentというもの
よく理解出来ていないため少し整理してみます。

これはタイミングを指定して発行することができるIntentのようです。
new したIntentを、直ちに発行するのではなく、ボタンを押されたタイミングで発行する等に
利用するようです。
引数でIntentのインスタンスを受けとって包むので、Wrapper的な役割ですかね。

PendingIntentは、以下のstatic メソッドから取得できる
どこに対して発行するかによって、作り方が違う

getActivity(Context, int, Intent, int)
→Activityへ投げる
getBroadcast(Context, int, Intent, int)
→ブロードキャストへ投げる
getService(Context, int, Intent, int);
→Serviceへ投げる

例えば、Notificationに使う場合は、
通知バーをタップしたタイミングでIntentが発行されるという感じのようです。

PendingIntent自身はOSが管理しているものなので
PendingIntentを起動した元のActivityが死んでも、それはOSによって残り続け
通知バーをタップしたタイミングで、Intentが投げられ、
元のAcitivyが起動されるという感じのようです(自信なし)

■参考にさせて頂いたサイト:
http://y-anz-m.blogspot.com/2011/07/androidappwidget-pendingintent-putextra.html

2012年2月6日月曜日

ProgressBarの使い方


プログレスバー(進捗)の表示には、ProgressBarウィジェットを用いる。

スタイルの指定(XMLレイアウト)
android:progressBarStyle        通常の円
android:progressBarStyleHorizontal   水平方向のバー
android:progressBarStyleLarge      大きい円
android:progressBarStyleSmall      小さい円

StyleHorizontal 使用時に限り、進捗状況の表示が可能になる。

メソッド:
setMax() プログレスバーの最大値の指定。上限はint型の範囲。
setProgress() 現在の進捗の値を設定(濃い色で表示される)
setSecondaryProgress() セカンダリの値の指定(薄い色で表示される)

使用例)
ProgressBar progress = (ProgressBar) findViewById(プログレスバーWidgetのID);
progress.setMax(100);
progress.setProgress(20);
progress.setSecondaryProgress(70);

2012年1月31日火曜日

RemoteViewsの使い方

別プロセス上で表示可能となる階層的なViewを記述するクラス
Viewは、レイアウトリソースファイルを指定してインフレートする。

// 生成
RemoteViews view = new RemoteViews(パッケージ名, レイアウトID)

// 以下の用に、指定したVIEWに対してリソースをセットする
view.setImageViewResource(VIEW_ID, リソースID);
view.setTextViewText(VIEW_ID, "text data");
view.setProgressBar(VIEW_ID, 100, progress, false);

// またPendingIntentをセットして、ViewからIntent発行をしたり出来るらしい★未検証
.setOnClickPendingIntent(VIEW_ID, pendingIntent);

このRemoteViews を用いて、Notificationクラスの、contentViewにセットすることで
独自のレイアウトをNotificationに表示させることが出来るようです。

AIDLによる Service ⇔ Activity間通信


●AIDLとは?
→android上でプロセス間通信を行うためのもの
 Service側に実装されたコールバック用のインターフェースを
 Activityが受け取ることで、Serviceの処理を呼び出すことが可能
 逆に ServiceからActivityのメソッドも呼び出せるらしい? ★ 要調査
 最初のテンプレートさえ覚えておけば、使い方は特に問題なさそう

1.AIDLファイルの作成
拡張子 .aidl のファイルを作成し、その中に下記のようにインターフェースを書く
javaソースと同じフォルダに置く

 
interface IServiceMethod
{
void CallServiceMethod();
}

2.サービスの実装

class MyService extends Service implements IServiceMethod
{
    @Override
    public IBinder onBind(Intent intent) {
        if(IServiceMethod.class.getName().equals(intent.getAction())){
            // IServiceMethodのインスタンスを返す
            return isvc;
        }
        return null;
    }

private IServiceMethod.Stub isvc = new IServiceMethod.Stub()
{
public void CallServiceMethod() throws RemoteException {
// Service側で実行する処理を実装
}
};
}

ビルドすると gen フォルダに自動生成された IServiceMethod.javaが出来る。
中身を見ると、Stub という名前の内部抽象クラスがあり、.aidl ファイルで宣言したメソッドが定義される。
このインターフェースを実装した Stabクラスのインスタンスを bind時に返せばOK。


3.Activityの実装
 
public class IPCMain extends Activity {

IServiceMethod aidl = null;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        // MyServiceにbindする
        Intent i = new Intent(this, android.study.kai.MyService.class);
        bindService(i, serviceConnection, BIND_AUTO_CREATE);
    }

    @Override
    public void onDestroy()
    {
    super.onDestroy();
    // MyServiceからアンバイド
    unbindService(serviceConnection);
    }

    private ServiceConnection serviceConnection = new ServiceConnection()
    {
    // サービスとの接続時に呼ばれる
        public void onServiceConnected(ComponentName name, IBinder ibinder)
        {
        // Ibinder インターフェースから、AIDLのインターフェースにキャストするメソッドらしい
        aidl = IServiceMethod.Stub.asInterface(ibinder);
        }

    // サービスとの切断時に呼ばれる
        public void onServiceDisconnected(ComponentName name)
        {
        aidl = null;
        }
    };
}

bindServiceと、unbindServiceを呼び出し、サービスと接続/切断する
Serviceのインターフェースは、bind成功時に呼ばれる
インターフェースから取得できる

●bindService
bindService(i, serviceConnection, BIND_AUTO_CREATE);

引数1: インデント。サービスのクラスを指定する?
引数2: ServiceConnectionの実装を指定する
    ServiceConnectionは、サービス接続、切断時に呼ばれる
    コールバック用のインターフェースである。
    接続時に、onBindで返したサービス側のインターフェースを取得できる
引数3: BIND_AUTO_CREATEを指定することでServiceが未起動の時に起動される

●unbindService
unbindService(serviceConnection);
引数1:ServiceConnectionの実装を指定する

2012年1月29日日曜日

Serviceの開始、停止

Activityからのサービスの開始と停止の指示。

 

// サービスを開始する
Intent i = new Intent(this, android2.study.yohei.MyService.class);
startService(i);

// サービスの停止する
Intent i = new Intent(this, android2.study.yohei.MyService.class);
stopService(i);
インテントを生成して、それを引数に起動するだけなので 意外とシンプルです。